conditional query is not working in cql 3.0.0 - cassandra

i am trying to execute conditional query in cassandra CQL but it is giving me error like
cqlsh:events> select * from standardevents where name=ActivityPg_view;
i am executing above query, it is giving me below error
Bad Request: line 1:55 no viable alternative at input ';'
Perhaps you meant to use CQL 2? Try using the -2 option when starting cqlsh.
cqlsh:events> select * from standardevents where name='ActivityPg_view';
i am executing above query, it is giving me below error
Bad Request: No indexed columns present in by-columns clause with
Equal operator
when i am trying to execute using CQL 2
cqlsh:events> select * from standardevents where name=ActivityPg_view;
it is giving me below error,
Bad Request: No indexed columns present in by-columns clause with "equals" opera
tor
cqlsh:events> select * from standardevents where name='ActivityPg_view';
it is giving me below error,
Bad Request: No indexed columns present in by-columns clause with "equals" opera
tor
Perhaps you meant to use CQL 3? Try using the -3 option when starting cqlsh.
so can somebody suggest the problem and how to solve this thing and execute the conditional query?
cqlsh> DESCRIBE COLUMNFAMILY events.standardevents;
CREATE TABLE standardevents (
uuid timeuuid PRIMARY KEY,
data text,
name text,
time text,
tracker text,
type text,
userid text
) WITH bloom_filter_fp_chance=0.010000
AND caching='KEYS_ONLY'
AND comment=''
AND dclocal_read_repair_chance=0.000000
AND gc_grace_seconds=864000
AND read_repair_chance=0.100000
AND replicate_on_write='true'
AND populate_io_cache_on_flush='false'
AND compaction={'class': 'SizeTieredCompactionStrategy'}
AND compression={'sstable_compression': 'SnappyCompressor'};

Your table (CF) has no field called courseid (hence the error "Undefined name courseid in where clause"). You can't query on something that doesn't exist.
Were you expecting that field there? (Your PK for that table is called 'uuid' if that helps) Are you querying the right table? Not much else to suggest.
Edit: After update
Your CQL3 attempt is missing the quotes around the name, but that aside ... In Cassandra you can't randomly query by non key columns. Cassandra is a partitioned row store, and it is not really designed to do query's in the manner you are trying.
You could add a secondary index to fix this, but you should be aware it not like in traditional SQL. Having a 2ndary index will need to hit all nodes in a cluster do to the query. It's also not ideal if your data has high cardinality.
In Cassandra the general premise is that storage is cheap, and you should base your model on your query rather than your data. Denormalise everything. For example, if you need to pull events by name, then you should make a table that is key'd by name, and includes all the event data you need. That way reading them is (essentially) and O(1) operation.

To be able to restrict by your name column you would need to add a secondary index to it.
Example:
CREATE INDEX standardevents_index ON standardevents (name);

Related

Databricks SQL nondeterministic expressions using DELETE FROM

I am trying to execute the following SQL clause using Databricks SQL:
DELETE FROM prod_gbs_gpdi.bronze_data.sapex_ap_posted AS HISTORICAL_DATA
WHERE
HISTORICAL_DATA._JOB_SOURCE_FILE = (SELECT MAX(NEW_DATA._JOB_SOURCE_FILE) FROM temp_sapex_posted AS NEW_DATA)
The intention of the query is to delete a set of rows in a historical data table based on a value present in a column of new data table.
For reasons that I cannot understand it is raising an error like:
Error in SQL statement: AnalysisException: nondeterministic expressions are only allowed in
Project, Filter, Aggregate, Window, or Generate, but found:
(HISTORICAL_DATA._JOB_SOURCE_FILE IN (listquery()))
in operator DeleteCommandEdge
It seems it is not accepting a subquery inside the where clause. That's odd for me, as in the Databricks documentation Link it is acceptable.
I even tried other types of predicates, like:
(SELECT FIRST(NEW_DATA._JOB_SOURCE_FILE) FROM temp_sapex_posted AS NEW_DATA)
(SELECT DISTINCT NEW_DATA._JOB_SOURCE_FILE FROM temp_sapex_posted AS NEW_DATA)
IN (SELECT NEW_DATA._JOB_SOURCE_FILE FROM temp_sapex_posted AS NEW_DATA)
None of them seems to take effect in executing the query successfully.
What's even odd for me is that I was able to accomplish a similar case with a slightly different query, as it can be seen in this link.
I have created demo_table1 & demo_table2 for querying purpose. I have created the following query carrying the similar purpose. I haven’t considered double aliases and have given straight query using subquery, it also depends on data frame in usage use a normal pandas data frame. it works fine for me.
delete from demo_table1 as t1 where t1.age = (select min(t2.age) from demo_table2 as t2);

How to use tuple <timestamp,text> in cql where clause?

I am using tuple<timestamp, text> to store timestamp and zone information in the Cassandra database.
I want to filter data based on timestamps.
Is there any way I can use this tuple in where clause for comparison in cql?
I have tried following cql query but it is not giving me proper results
SELECT extid,time_created_ from d_account where time_created_>=('2021-04-06 7:09:06', '+05:30') allow filtering;
Thanks in advance.
Posting this answer so that someone might get help in the future.
The following query worked for me and returned the expected results.
SELECT extid,time_created_ from d_account where time_created_ < ('2021-04-06 07:24:10.347+0000', '+05:30') allow filtering;

How can i describe table in cassandra database?

$describe = new Cassandra\SimpleStatement(<<<EOD
describe keyspace.tablename
EOD
);
$session->execute($describe);
i used above code but it is not working.
how can i fetch field name and it's data type from Cassandra table ?
Refer to CQL documentation. Describe expects a table/schema/keyspace.
describe table keyspace.tablename
Its also a cqlsh command, not an actual cql command. To get this information query the system tables. try
select * from system.schema_columns;
- or for more recent versions -
select * from system_schema.columns ;
if using php driver may want to check out http://datastax.github.io/php-driver/features/#schema-metadata
Try desc table keyspace.tablename;

Cassandra CQL 3 - Prefix Select

is there a way to perform a select based on a string prefix using CQL3?
For example, consider the following table:
Key | Value
------------
ABC | 0x01
ABD | 0x02
BBB | 0x03
I want to select all the keys with the prefix 'AB'.
The database will be used to store spacial information, using a geohash approach.
That is not possible "out of the box"... However, there are some "tricks" people came up with, see these two posts:
Cassandra (Pycassa/CQL) Return Partial Match
Is there any query for Cassandra as same as SQL:LIKE Condition?
...another (somehow similar) approach could be to define "composite key", where you define some prefixes as "partition key", e.g.: {key1,key2}, where key1 = ABand key2 = ABC... in these situations you could query by "key1" only and get a set of rows (like you want to do), or by "key1" and "key2" (in case you want a specific entry). You can also query only by "key2" (if you add "allow filtering" to your "select" query, however this can lead to "problems" if you have too many rows). Not sure if you can do this with your data...
HTH.
Not built-in in C* but possible with cassandra-lucene-index C* plugin. You can create a lucene index on the column and search the text using prefix search.
UPDATE: Since v3.4 Cassandra introduced SASI indices that offer the required functionality.

Syntax error at position 7: unexpected "*" for `Select * FROM mytable;`

I write because I've a problem with cassandra; after have imported the data from pentaho as show here
http://wiki.pentaho.com/display/BAD/Write+Data+To+Cassandra
when I try to execute the query
Select * FROM mytable;
cassandre give me an error message
Syntax error at position 7: unexpected "*" for Select * FROM mytable;.
and don't show the results of query.Why? what does it mean that error?
the step that i make are the follow:
start cassandra cli utility;
use keyspace added from pentaho; (use tpc_h);
select to show the data added (Select * FROM mytable;)
The cassandra-cli does not support any CQL version. It has its own syntax which you can find on datastax's website.
Just for clarity, in cql to select everything from a table (aka column-family) called mytable stored in a keyspace called myks you would use:
SELECT * FROM myks.mytable;
The equivalent in cassandra-cli would *roughly be :
USE myks;
LIST mytable;
***** In the cli you are limited to selecting the first 100 rows. If this is a problem you can use the limit clause to specify how many rows you want:
LIST mytable limit 10000;
As for this:
in cassandra i have read that isn't possible make the join such as sql, ther isn't a shortcut to issue this disadvantage
There is a reason why joins don't exist in Cassandra, its for the same reason that C* isn't ACID compliant, it sacrifices that functionality for it's amazing performance and scalability, so it's not a disadvantage, you just need to re-think your model if you need joins. Also take a look at this question / answer.

Resources