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

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().

Related

Cassandra: missing ')' at '<missing '

tried to create the following table:
CREATE TABLE customTableSchema(
id UUID PRIMARY KEY,
table_id UUID,
schema text,
created_at timestamp,
last_modified_at timestamp,
);
came up with this error:
SyntaxException: line 4:8 missing ')' at '<missing '
The schema is reserved keyword in the Cassandra Query Language, so you can't use it (see this table in the docs).
Since "schema" is a reserverd keyword, you cannot use your query in current form. If you intend to use it then you can do it as below
CREATE TABLE customTableSchema(
id UUID PRIMARY KEY,
table_id UUID,
"schema" text,
created_at timestamp,
last_modified_at timestamp,
);
You can refer this page for reserved cql keywords.

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

Cassandra invalid query "Map-entry equality predicates on frozen map column device_details are not supported"

I have the following table:
CREATE TABLE dove.backend_events (
log_time_local timeuuid,
username text,
log_type text,
log_time timestamp,
device_category text,
log text,
device_details frozen<map<text, text>>,
PRIMARY KEY (log_time_local, username, device_details)
);
I am running this query: SELECT * FROM dove.backend_events WHERE device_details['category'] = 'mobile' ALLOW FILTERING;
I am getting this error: InvalidRequest: code=2200 [Invalid query] message="Map-entry equality predicates on frozen map column device_details are not supported"
What is causing it and how do I fix it? This error is not occurring when device_details is not part of the primary key and is not frozen.
You could add an index for device_details instead of setting it as primary key (and without freezing it):
CREATE TABLE dove.backend_events (
log_time_local timeuuid,
username text,
log_type text,
log_time timestamp,
device_category text,
log text,
device_details map<text, text>,
PRIMARY KEY (log_time_local, username)
);
CREATE INDEX dove.device_details_index ON dove.backend_events (ENTRIES(device_details));
This way you could run your query efficiently and without having to use ALLOW FILTERING:
SELECT * FROM dove.backend_events WHERE device_details['category'] = 'mobile';

no viable alternative at input '<' cassandra map

I created a type as such
create type (
street text,
city text,
state text,
zip int
);
then tried to create a table like this:
create table (
fName text PRIMARY KEY,
addressess map<text, frozen<address>>
);
but I keep getting this error though:
SyntaxException: <ErrorMessage code=2000 [Syntax error in CQL query] message="line 1:78 no viable alternative at input '<' (..., addresses <string, frozen [<]...)">
I followed this doc on datastax and I'm on version 2.1.8
http://docs.datastax.com/en/cql/3.1/cql/cql_using/cqlUseUDT.html

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.

Resources