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