Cassandra: inserting timeuuid error - cassandra

I have following table
create table test(
userId varchar,
notifId timeuuid,
notification varchar,
time bigint,read boolean,
primary key(userId, notifId)) with clustering order by (notifId desc);
I am running following query:
PreparedStatement pstmt = session.prepare("INSERT INTO notifications(userId, notifId, notification, time, read) VALUES(?, now(), ?, ?, ?)");
BoundStatement boundStatement = new BoundStatement(pstmt);
session.execute(boundStatement.bind("123", "hello", new Date().getTime(), false));
I am getting following error:
Exception in thread "main" com.datastax.driver.core.exceptions.InvalidQueryException: Type error: cannot assign result of function now (type timeuuid) to notifid (type 'org.apache.cassandra.db.marshal.ReversedType(org.apache.cassandra.db.marshal.TimeUUIDType)')
at com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:35)
at com.datastax.driver.core.ResultSetFuture.extractCause(ResultSetFuture.java:277)
at com.datastax.driver.core.Session.toPreparedStatement(Session.java:281)
at com.datastax.driver.core.Session.prepare(Session.java:172)
at com.example.cassandra.SimpleClient.loadData(SimpleClient.java:130)
at com.example.cassandra.SimpleClient.main(SimpleClient.java:214)
Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Type error: cannot assign result of function now (type timeuuid) to notifid (type 'org.apache.cassandra.db.marshal.ReversedType(org.apache.cassandra.db.marshal.TimeUUIDType)')
at com.datastax.driver.core.ResultSetFuture.convertException(ResultSetFuture.java:307)
I am using cassandra 1.2.2.

You should insert the timeuuid generated via datastax driver. In your case , since you are using version 1 timeuuid, you must use
UUIDs.timeBased()
You can find the above in following reference
http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/utils/UUIDs.html

It looks like a bug (similar or the same as https://issues.apache.org/jira/browse/CASSANDRA-5472) , and you're using pretty old version of Cassandra. I recommend you to upgrade to the latest 1.2.x release, retest your scenario and file an issue, if the problem still persists

Related

SyntaxException: while creating Cassandra Table

I'm trying to create a simple table in cassandra, this is the command I run,
create table app_instance(app_id int, app_name varchar, proc_id varchar, os_priority int, cpu_time int, num_io_ops int, primary_key (host_id, proc_id)) with clustering order by (proc_id DESC) ;
I get the following error,
SyntaxException: line 1:132 no viable alternative at input '(' (...int, num_io_ops int, primary_key [(]...)
What am I doing wrong here?
It should be primary key, with a space, not primary_key, as ernest_k already noted in a comment.
The way you wrote it,
...cpu_time int, num_io_ops int, primary_key (host_id, proc_id)
the CQL parser thinks that "primary_key" is the name of yet another column, just as num_io_ops was, and now expects to see the name of the type - and doesn't expect an open parenthesis after the "primary_key", and this is exactly what the error message told you (albeit vaguely).

com.datastax.driver.core.exceptions.InvalidQueryException: Invalid operator IN for PRIMARY KEY part

I have cassandra 2.1.15.
I have this table
CREATE TABLE ks_mobapp.messages (
pair_id text,
belong_to text,
message_id timeuuid,
cli_time bigint,
sender text,
text text,
time bigint,
PRIMARY KEY ((pair_id, belong_to), message_id)
) WITH CLUSTERING ORDER BY (message_id DESC)
I was trying to delete multiple record as
instances.getCqlSession().execute(QueryBuilder.delete()
.from(AppConstants.KEYSPACE, "messages")
.where(QueryBuilder.eq("pair_id", pairId))
.and(QueryBuilder.eq("belong_to", currentUser.value("userId")))
.and(QueryBuilder.in("message_id", msgId)));
I am getting error:
Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Invalid operator IN for PRIMARY KEY part message_id
Then I tried:
Session session = instances.getCqlSession();
PreparedStatement statement = session.prepare("DELETE FROM ks_mobApp.messages WHERE pair_id = ? AND belong_to = ? AND message_id = ?;");
Iterator<String> iterator = msgId.iterator();
while(iterator.hasNext()) {
try {
session.executeAsync(statement.bind(pairId, currentUser.value("userId"), UUID.fromString(iterator.next())));
} catch(Exception ex) {
}
}
Its working nice. Is this the correct way? I can't use IN for same partition key ?
DELETE in Query only supported for partition key.
Delete IN relation is only supported for partition key)
There are some WHERE clause restrictions for the UPDATE and DELETE statements in cassandra 2.x
more specifically you can only use the IN operator on the last partition key column. So in your case the last partition column is belong_to. so IN can only be used on that column.
However these limitation are removed in cassandra 3.0. and it will allow
IN to be specified on any partition key column
IN to be specified on any clustering column
Here is the patch https://issues.apache.org/jira/browse/CASSANDRA-6237
Read this also http://www.datastax.com/dev/blog/a-deep-look-to-the-cql-where-clause

Cassandra : missing EOF at ')' when trying to create simple table

I am trying to create a simple table on Cassandra using cqlsh. The syntax is:
CREATE TABLE TEST(
timestamp timestamp,
system_id text,
hostname text,
cpu_pct float,
memory_used bigint,
PRIMARY_KEY(system_id, timestamp)
);
When I run this I get this error however. How to fix?
ErrorMessage code=2000 [Syntax error in CQL query] message="line 8:0 missing EOF at ')' (...,PRIMARY_KEY(system_id, timestamp)[)];)"
CREATE TABLE TEST(
timestamp timestamp,
system_id text,
hostname text,
cpu_pct float,
memory_used bigint,
PRIMARY KEY(system_id, timestamp)
);
See CQL CREATE TABLE Doc
You accidentally put an underscore between "PRIMARY KEY" instead of a space.
Also you might not want a field called "timestamp" since that is also a Cassandra type, so maybe call that "ts" or something.
PRIMARY_KEY() should be PRIMARY KEY().

DSE/Cassandra CQL now() does not work for timestamp type

I am having troubles with using now() function with timestamp type.
Please take a look at the following code:
Table creation:
CREATE TABLE "Test" (
video_id UUID,
upload_timestamp TIMESTAMP,
title VARCHAR,
views INT,
PRIMARY KEY (video_id, upload_timestamp)
) WITH CLUSTERING ORDER BY (upload_timestamp DESC);
The problematic INSERT query:
INSERT INTO "Test" (video_id, upload_timestamp, title, views)
VALUES (uuid(), now(), 'Test', 0);
The INSERT query seems looking fine to me. However, when I execute it, I see the following error:
Unable to execute CQL script on 'XXX': cannot assign result of function now (type timeuuid) to upload_timestamp (type timestamp)
What I am doing wrong here?
I use DataStax Enterprise 4.5.2
now() returns a timeuuid, not a timestamp. You clould try dateOf(now()). Have a read of this from the docs:
dateOf and unixTimestampOf
The dateOf and unixTimestampOf functions take a timeuuid argument and
extract the embedded timestamp. However, while the dateof function
return it with the timestamp type (that most client, including cqlsh,
interpret as a date), the unixTimestampOf function returns it as a
bigint raw value.

how to insert timeuuid into cassandra using datastax java driver OR Invalid version for TimeUUID

I have one column in the cassandra keyspace that is of type timeuuid. When I try to insert a reocord from the java code (using DataStax java driver1.0.3). I get the following exception
com.datastax.driver.core.exceptions.InvalidQueryException: Invalid version for TimeUUID type.
at com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:35)
at com.datastax.driver.core.ResultSetFuture.extractCauseFromExecutionException(ResultSetFuture.java:269)
at com.datastax.driver.core.ResultSetFuture.getUninterruptibly(ResultSetFuture.java:183)
at com.datastax.driver.core.Session.execute(Session.java:111)
Here is my sample code:
PreparedStatement statement = session.prepare("INSERT INTO keyspace:table " +
"(id, subscriber_id, transaction_id) VALUES (now(), ?, ?);");
BoundStatement boundStatement = new BoundStatement(statement);
Session.execute(boundStatement.bind(UUID.fromString(requestData.getsubscriberId()),
requestData.getTxnId()));
I have also tried to use UUIDs.timeBased() instead of now(). But I am gettting the same exception.
Any help on how to insert/read from timeuuid datatype would be appreciated.
By mistake I had created
id uuid
That is why when I try to insert timeuuid in the uuid type field I was getting that exception.
Now I have changed the type for id to timeuuid and everything is working fine.

Resources