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

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;

Related

Cassandra - SyntaxException: line ... no viable alternative at input in select statement

I am trying to select data from cassandra db using below query but it is failing-
SELECT id from keyspace.table where code=123 and toTimestamp(now()) >= some_date;
Error- SyntaxException: line 1:103 no viable alternative at input '(' (...table where code=123 and [toTimestamp](...)
Looks like toTimestamp(now()) is causing the issue.
Can someone plz suggest what is the issue and solution to this?
Thanks.
You can't use functions in the WHERE statement. So the only workaround is to get current time inside your application, and pass it to the query. This request is tracked as CASSANDRA-8488.
But in reality, your query should have condition on some column, not on the calculated value.

Presto - static date and timestamp in where clause

I'm pretty sure the following query used to work for me on Presto:
select segment, sum(count)
from modeling_trends
where segment='2557172' and date = '2016-06-23' and count_time between '2016-06-23 14:00:00.000' and '2016-06-23 14:59:59.000';
group by 1;
now when I run it (on Presto 0.147 on EMR) I get an error of trying to assigning varchar to date/timestamp..
I can make it work using:
select segment, sum(count)
from modeling_trends
where segment='2557172' and date = cast('2016-06-23' as date) and count_time between cast('2016-06-23 14:00:00.000' as TIMESTAMP) and cast('2016-06-23 14:59:59.000' as TIMESTAMP)
group by segment;
but it feels dirty...
is there a better way to do this?
Unlike some other databases, Presto doesn't automatically convert between varchar and other types, even for constants. The cast works, but a simpler way is to use the type constructors:
WHERE segment = '2557172'
AND date = date '2016-06-23'
AND count_time BETWEEN timestamp '2016-06-23 14:00:00.000' AND timestamp '2016-06-23 14:59:59.000'
You can see examples for various types here: https://prestosql.io/docs/current/language/types.html
Just a quick thought.. have you tried omitting the dashes in your date? try 20160623 instead of 2016-06-23.
I encountered something similar with SQL server, but not used Presto.

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;

conditional query is not working in cql 3.0.0

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);

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