Question

I want to retrieve those data which belongs to today's date, i tried below thing

select * from tablename where fieldname = dateof(now());

This query gives date and system-time both, so result will not be proper.

How can I get only current(today's) date?

Was it helpful?

Solution

Based on your question the column family tablename is using the timestamp fieldname as primary key. In that case, each row is identified by the timestamp and not the date so you can not query by the date.

Based on this, you have to change your data model to define the primary key on another way. A possibility is use a table like created like this:

USE test;

create table posts(username varchar, 
    time timeuuid, 
    post_text varchar, 
    primary key(username, time)
);

insert into posts(username, time, post_text) 
values ('me', now(), 'Example');

insert into posts(username, time, post_text) 
values ('me', now(), 'Example');

select username, dateOf(time), post_text from posts;

In this case you obtain two results like this

enter image description here

So if you want to query on a specific date you can use:

select username, dateOf(time), post_text from posts where time >= minTimeuuid('2014-04-23 00:00') and time < minTimeuuid('2014-04-24 00:00') and username = 'me';


Sources:

http://cassandra.apache.org/doc/cql3/CQL.html#usingdates

http://christopher-batey.blogspot.nl/2013/05/time-series-based-queries-in-cassandra.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top