Request to Cassandra with a condition on a column that is not a Primary Key works in DBeaver but doesn't work in Python - cql

My query has a WHERE on a column that is not a primary key.
SELECT * FROM table WHERE id_order IN ('2177579','2177130','2177072')
The query works fine in DBeaver, but when using the Cassandra library in Python:
from cassandra.cluster import Cluster string = '''SELECT * FROM table WHERE id_order IN ('2177579','2177130','2177072')''' session.execute(string)
I am getting an exception:
InvalidRequest: Error from server: code=2200 [Invalid query] message="IN predicates on non-primary-key columns (id_order) is not yet supported"
Any help would be highly appreciated

Related

unconfigured table error with Capitalized table name

All my Cassandra column family names are capitalized (like FunTable, SomeOtherTable, etc.) -- I'm switching from the Thrift API to CQL, and whenever I make a query (like SELECT * FROM FunTable) it fails with cassandra.InvalidRequest: Error from server: code=2200 [Invalid query] message="unconfigured table funtable"
What's going on?
It turns out, as I should have figured out from the table name being lower-cased in the error message, that CQL is case-insensitive, except inside quoted strings (similar to other SQL dialects). So the solution is just to put double quotes around the table name, like so: SELECT * FROM "FunTable"
If you are specifying the keyspace, you need to put the quote just around the table name, not around the combination of keyspace and table name, e.g. SELECT * FROM good_keyspace."FunTable"

Cassandra update multiple rows based on condition

I want to update multiple rows in Cassandra if they satisfy my condition with same value(changing flag to false) and my condition field is part of my composite key. But I cannot use whole composite key.
Unfortunately, this is not possible with Cassandra. UPDATEs and INSERTs are all considered write operations. And write operations require a complete PRIMARY KEY.
Consider this example where I have a partition key of username and a clustering key of transaction_time: PRIMARY KEY (username,transaction_time). If I just try to UPDATE based on username, this fails.
cassdba#cqlsh:stackoverflow> UPDATE rest_transactions_by_user
SET http_result = 200 WHERE username='Aaron';
InvalidRequest: Error from server: code=2200 [Invalid query]
message="Some clustering keys are missing: transaction_time"

Are all values in a Primary Key Indexed?

Following a Tutorial on Cassandra, it was mentioned that if I do the following:
PRIMARY KEY(id, name) that id is the partition key and hence it is indexed. The name is the clustering column and hence it is also indexed. This means I can do a query such as:
SELECT * FROM my_table WHERE id = 'id_abc'; //this works!
I can also do a query such as:
SELECT * FROM my_table WHERE id = 'id_abc' AND name = 'name_123'; // this works!
However, I cannot do the following query:
SELECT * FROM my_table WHERE name = 'name_123'; // this does not work
Why does the last statement not work if the clustering column is indexed? Why does the first query work and not the second?
The error I get for the last query is the following:
InvalidRequest: code=2200 [Invalid query] message="Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING"
Thanks in advance!
Just because it is named primary key there is no index on it in cassandra. ìd is your partition key - it defines which node in cassandra is responsible for your id. The clustering column name defines the order inside the partition.
Therefore SELECT * FROM my_table WHERE name = 'name_123'; // this does not work whould require all partitions to be scanned, which cassandra by default refuses.

How to perform ordering in cassandra

I have table in Cassandra which includes a field id. I have set that field as partition key. Now I want to query that table order by id.
How can I select all rows from that table in the order of id using Cql?
In Cassandra you cannot order by partition key.
You can only order within a partition key
Error from cql:
code=2200 [Invalid query] message="Order by is currently only supported on the clustered
columns of the PRIMARY KEY, got id"
You will have to create a table with id as clustering key and then order by id.

error when add new UDT type field to Cassandra table

When I try to add a new UDT type field to my cassandra table, I executed commands like:
CREATE TYPE price(
micros bigint,
currency_code varchar,
formatted_amount varchar
);
ALTER TABLE table_name ADD price frozen;
But I got an exception:
<ErrorMessage code=2000 [Syntax error in CQL query] message="line 1:38 missing EOF at '<' (...table metadata_m add price type [<]price...)">
So how to solve this problem?
I am using c* 2.1.2, cql3.2.0, cqlsh 5.0.1.
First of all, it usually helps if you provide the exception you received in your question. Fortunately for you, my sandbox has the exact same C* specs you mentioned above, so I did manage to see this exception when pasting in your code:
aploetz#cqlsh:stackoverflow> CREATE TYPE price( micros bigint, currency_code varchar, formatted_amount varchar );
aploetz#cqlsh:stackoverflow> ALTER TABLE table_name ADD price frozen;
<ErrorMessage code=2000 [Syntax error in CQL query] message="Failed parsing statement: [ALTER TABLE some_data ADD price frozen;] reason: ArrayIndexOutOfBoundsException -1">
I deducted that it was because you specified the type price, but did not actually provide a name for the column. Also, frozen needs to be used with the type following it in angle brackets. This worked for me:
ALTER TABLE table_name ADD myprice frozen<price>;

Resources