Dynamic TTL in prepared statements

2,114 views
Skip to first unread message

Roshan Pradeep

unread,
Jul 15, 2014, 8:02:31 PM7/15/14
to java-dri...@lists.datastax.com
Hi All

Could someone please help me how to use dynamic TTL values in a single prepared statement?

I have a one keyspace (use as a cache) and some rows in that keyspace are need to remain for two days and some for for 30 mins.

When inserting the data, I want to keep one single prepared statement, so the TTL want to define as dynamic.

Thanks.

Roshan

Olivier Michallat

unread,
Jul 16, 2014, 5:52:43 AM7/16/14
to java-dri...@lists.datastax.com
Hi,

Starting with Cassandra 2.0, you can bind the TTL like any other variable:

            PreparedStatement ps = session.prepare("insert into foo (i) values (1) using ttl ?");
            session.execute(ps.bind(10));



To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.

Roshan Pradeep

unread,
Jul 16, 2014, 10:18:10 PM7/16/14
to java-dri...@lists.datastax.com
Thanks Oliver. It worked.

Rajesh Sindhu

unread,
Aug 18, 2014, 4:06:36 PM8/18/14
to java-dri...@lists.datastax.com

I tried same way but not working .

Do we need to update java driver version to any specific version ,we are using cassandra 2.0 .

Roshan , can you please post any sample example ,how are you creating bound statement and prepared statement . 

Rajesh Sindhu

unread,
Aug 18, 2014, 6:06:51 PM8/18/14
to java-dri...@lists.datastax.com
Got it solved :

Maximum value for a TTL can be set is 20 years in Cassandra .

I was assuming that Cassandra store TTL in milliseconds , in fact its in seconds , i put TTL for more than 20 years which caused this problem .

But weird thing is this that java driver is not throwing nay error or display any warning .

I ran same command through Cqlsh which says:

Bad Request: ttl is too large. requested (756864000) maximum (630720000).

Java driver should throw error in such situation ?


--
Thanks
Rajesh Sindhu
Junior Software Engineer
Zscaler Inc.

Olivier Michallat

unread,
Aug 19, 2014, 4:56:55 AM8/19/14
to java-dri...@lists.datastax.com
Hi Rajesh,

It works for me, I get the same error as in cqlsh when running this:

            PreparedStatement ps = session.prepare("insert into foo (i) values (1) using ttl ?");
            session.execute(ps.bind(756864000));

I'm testing with C* 2.0.9.

Could you check your code and share it if you reproduce the issue?

Olivier Michallat
Drivers & Tools Engineer, DataStax

Rajesh Sindhu

unread,
Aug 19, 2014, 2:02:10 PM8/19/14
to java-dri...@lists.datastax.com
Olivier ,


We are using Cassandra 2.0.8.39 with java driver version 2.0.2 .

I don't see any error or warning from java driver ,although record do not get inserted in cassandra.

I am using prepared statements like this :

insertTopnAndStatsAggregatesPreparedStatement = syncQueryExecuter.createPreparedStatement(String

.format(QueryConstants.INSERT_TOPN_STATS_AGGREGATES_CF_WITH_TTL,

configService

.getTTLForDnsTopnStatsLogsInSec()));




TTL value attached with record :  756864000

 dnslog glob messages count: 1

19/08/2014 10:55:41,663 main       INFO  com.zscaler.zpath.cassandra.writepath.impl.WriteDataServiceImpl.insertObject(WriteDataServiceImpl.java:85) | dnslog glob Total execution time in milliseconds: 112

19/08/2014 10:55:41,663 main       INFO  com.zscaler.zpath.cassandra.writepath.GlobInsertSingleMessageTest.insertDataIntoCassandra(GlobInsertSingleMessageTest.java:54) | done cassandra processing


Olivier Michallat

unread,
Aug 20, 2014, 4:29:53 AM8/20/14
to java-dri...@lists.datastax.com
I still get the error with driver 2.0.2 and Cassandra 2.0.8.

Your code doesn't look familiar to me at all, I imagine you're using a custom layer on top of the driver. Try running the simple example I sent, that might help to better target the issue.
Reply all
Reply to author
Forward
0 new messages