Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a value to TTL in INSERT, Cassandra C++ driver

Tags:

c++

cassandra

I'm using prepared statements in the Cassandra Datastax C++ Driver. How do I bind an integer-value to the "USING TTL ?" part of a prepared statement?

My statement would be something like

INSERT INTO table (column1, column2, column3)  VALUES (?, ?, ?) USING TTL ?

In other words, If I'm using the position to bind to TTL, what is its position? (In this example, is it 4?) If I'm using bind by column name, what is its column name?

It looks like this can be done in CQL, but I couldn't find any documentation about the C++ driver API for doing this.

like image 717
Posco Grubb Avatar asked Jun 22 '18 20:06

Posco Grubb


People also ask

What is default TTL in Cassandra?

To create a new table with default Time to Live (TTL) settings enabled using CQL. Enable TTL when you're creating a new table with the default TTL value set to 3,024,000 seconds, which represents 35 days.

Is Cassandra TTL in seconds?

TTL is measured in seconds. If the field is not updated within the TTL it is deleted. The TTL can be set when defining a Table (CREATE), or when using the INSERT and UPDATE queries.


1 Answers

In Cassandra CQL 2.0 you can have:

Cassandra 1.2 doesn't allow you to use a bind marker for the TIMESTAMP and TTL properties of update statements, nor for the LIMIT property of SELECT statements. This is now fixed and you can for instance prepare statements like:

SELECT * FROM myTable LIMIT ?;
UPDATE myTable USING TTL ? SET v = 2 WHERE k = 'foo';

See their blog for more.

Edit:

I found this pdf and it tells you more:

Bound parameters:

The driver supports two kinds of bound parameters: by marker and by name. Binding parameters The ? marker is used to denote the bind variables in a query string. This is used for both regular and prepared parameterized queries. In addition to adding the bind marker to your query string, your application must also provide the number of bind variables to cass_statement_new() when constructing a new statement. If a query doesn’t require any bind variables then 0 can be used. The cass_statement_bind_*() functions are then used to bind values to the statement’s variables. Bind variables can be bound by the marker index or by name.

Bind by marker index example

CassString query = cass_string_init("SELECT * FROM table1 WHERE column1
 = ?");
/* Create a statement with a single parameter */
CassStatement* statement = cass_statement_new(query, 1);
cass_statement_bind_string(statement, 0, cass_string_init("abc"));
/* Execute statement */
cass_statement_free(statement);

Bind by marker name example

Variables can only be bound by name for prepared statements. This limitation exists because query metadata provided by Cassandra is required to map the variable name to the variable’s marker index.

/* Prepare statement */
/* The prepared query allocates the correct number of parameters
 automatically */
CassStatement* statement = cass_prepared_bind(prepared);
/* The parameter can now be bound by name */
cass_statement_bind_string_by_name(statement, "column1",
 cass_string_init("abc"));
/* Execute statement */
cass_statement_free(statement);

To answer your question you can use bind by index (works at least for sure):

CassString query = cass_string_init("INSERT INTO table (column1, column2, column3)  VALUES (?, ?, ?) USING TTL ?");
/* Create a statement with a single parameter */
CassStatement* statement = cass_statement_new(query, 4); // Bind 4 variables.
cass_statement_bind_string(statement, 0, cass_string_init("abc")); // Bind abc to first column.
cass_statement_bind_string(statement, 1, cass_string_init("bcd")); // Bind bcd to second column.
cass_statement_bind_string(statement, 2, cass_string_init("cde")); // Bind cde to third column.
cass_statement_bind_string(statement, 3, cass_string_init(50)); // Bind 50 to TTL.   
/* Execute statement */
cass_statement_free(statement);

Edit:

See https://docs.datastax.com/en/cql/3.3/cql/cql_using/useExpireExample.html where you see that in case of INSERT we have USING TTL as last part of query, as seen above.

like image 115
mico Avatar answered Oct 20 '22 01:10

mico