Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

gabor-recrd avatar image
gabor-recrd asked Erick Ramirez answered

Can the REST API provide the ability to run CQL functions?

Hi All,

I am wondering if REST API could provide us all the capabilities CQL does support.

Please read the below examples to get more details.

This is how I create a table:

##################### POST create table
curl -s -L -X POST https://$ASTRA_DB_ID-$AWS_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/$ASTRA_KEYSPACE_NAME/tables \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "content-type: application/json" \
-H "Accept: application/json" \
-d '{"name":"test_table","ifNotExists":true,"columnDefinitions": [ {"name":"id","typeDefinition":"text","static":false}, {"name":"productname","typeDefinition":"text","static":false}, {"name":"description","typeDefinition":"text","static":false}, {"name":"price","typeDefinition":"decimal","static":false}, {"name":"created","typeDefinition":"timeuuid","static":false}],"primaryKey": {"partitionKey":["id", "created"],"clusteringKey":["price"]},"tableOptions":{"defaultTimeToLive":0, "clusteringExpression":[{"column": "price", "order": "DESC"}]}}'

And this is how I query it:

## 1. NO where clause

curl -s -L -G https://$ASTRA_DB_ID-$AWS_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/$ASTRA_KEYSPACE_NAME/test_table \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "content-type: application/json" \
-H "Accept: application/json" \
--data-urlencode 'fields=dateof(created)'

I am getting the error below:

{
  
"description": "where parameter is required",
"code": 400
}

CQL works:

select dateof(created) from test_table;

## 2. NOW() in where clause

curl -s -L -G https://$ASTRA_DB_ID-$AWS_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/$ASTRA_KEYSPACE_NAME/test_table \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "content-type: application/json" \
-H "Accept: application/json" \
--data-urlencode 'where={"id":{"$eq":"some_id"},"created":{"$eq":now()}}'
--data-urlencode 'fields=dateof(created)'

I am getting the error below:

{
  
"description": "Bad request: Input provided is not valid json. Unrecognized token 'now': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: (String)\"{\"id\":{\"$eq\":\"some_id\"},\"created\":{\"$eq\":now()}}\"; line: 1, column: 45]",
"code": 400
}

CQL works:

select dateof(created) from test_table where id = 'some_id' and created = now();

## 3. HARD-CODED timeUUID in where clause (AS STRING)

curl -s -L -G https://$ASTRA_DB_ID-$AWS_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/$ASTRA_KEYSPACE_NAME/test_table \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "content-type: application/json" \
-H "Accept: application/json" \
--data-urlencode 'where={"id":{"$eq":"some_id"},"created":{"$eq":"2debafa0-8519-11ec-a724-8798f253fa33"}}'
--data-urlencode 'fields=dateof(created)'

I am getting the error below:

{
  
"description": "Server error: null",
"code": 500
}

CQL FAILs if I pass hard-coded value as a string:

select dateof(created) from test_table where id = 'some_id' and created = '2debafa0-8519-11ec-a724-8798f253fa33';
InvalidRequest: Error from server: code=2200 [Invalid query] message="Invalid STRING constant (2debafa0-8519-11ec-a724-8798f253fa33) for "created" of type timeuuid"

CQL works if I pass as timeUUID:

select dateof(created) from test_table where id = 'some_id' and created = 2debafa0-8519-11ec-a724-8798f253fa33;

## 4. No function in FIELDS list

curl -s -L -G https://$ASTRA_DB_ID-$AWS_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/$ASTRA_KEYSPACE_NAME/test_table \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "content-type: application/json" \
-H "Accept: application/json" \
--data-urlencode 'where={"id":{"$eq":"some_id"},"created":{"$eq":"2debafa0-8519-11ec-a724-8798f253fa33"}}'
--data-urlencode 'fields=created'

It WORKS:

{
  
"count": 0,
"data": []
}

Thank you for all your inputs.

Regards,

Gabor

stargate
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered

The quick answer is no, the REST API is designed to be an abstraction on CQL. It wasn't meant to be a mechanism for providing CQL over HTTP. Cheers!

Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.