storing comple json object in cassandra table

2,205 views
Skip to first unread message

Bhavesh Joshi

unread,
Nov 16, 2017, 8:50:26 AM11/16/17
to DataStax Node.js Driver for Apache Cassandra Mailing List
Hi,

I need to store json object in cassandra table, a id will map to a particular json object.

I have been following this link.

I need to add some complex json to the table.

one of the example is:-

{
  "string" : {
    "type": "string",
    "supported_filters":[
       "equals"
      ,"is_blank"
      ,"is_null"
      ,"not_equals_to"
      ,"doesn't_contains"
    ]
  },
  "number" : {
    "type": "number",
    "supported_filters":[
       "equals"
      ,"gte"
      ,"gt"
      ,"lte"
      ,"lt"
      ,"not_between"
      ,"between"
    ]
  },
  "date": {
    "type": "date",
    "supported_filters":[
       "is_on_the_day"
      ,"is_in_range"
      ,"is_before"
    ]
  }
}

and it can be much larger and complex.

this is what I am trying 

CREATE TABLE json (id text PRIMARY KEY,int_map map<test,frozen<set< text>>> );


when I try to Insert


INSERT INTO json JSON '{"id": "11", "int_map":{ "string" : { "type": "string", "supported_filters":["equals","is_blank"]}}}';


I get 

InvalidRequest: Error from server: code=2200 [Invalid query] message="Error decoding JSON value for int_map: Expected a list (representing a set), but got a LinkedHashMap: {type=string, supported_filters=[equals, is_blank]}"


I tried 


CREATE TABLE jsontest1 (id text PRIMARY KEY,int_map map<text,frozen<LinkedHashMap<text>>> );


I get


SyntaxException: line 1:81 mismatched input '<' expecting '>' (...,int_map map<text,frozen<LinkedHashMap[<]text...)


CREATE TABLE jsontest (id text PRIMARY KEY,int_map LinkedHashMap<text,frozen<set< text>>> );

SyntaxException: line 1:64 mismatched input '<' expecting ')' (... text PRIMARY KEY,int_map LinkedHashMap[<]text...)


I have just started with cassandra, so don't have much idea about it.


how can I do it?


and is there any documentation which give the complete view about registering complex json apart from the mentioned one.

 




Jorge Bay Gondra

unread,
Nov 22, 2017, 6:15:38 AM11/22/17
to nodejs-dr...@lists.datastax.com
Hi,
Regarding the schema, I think it would be a good idea to use user-defined types:

CREATE TYPE type1 (type text, supported_filters frozen<set<text>>);

CREATE TABLE tbl1 (id text PRIMARY KEY, my_col type1);


In CQL, to insert with hardcoded parameters, it would be something like:


INSERT INTO tbl1 (id, my_col) VALUES ('sample', { type: 'number', supported_filters: { 'equals', 'gte' }});


Using the Node.js driver, you can use javascript objects to represent UDTs (recommended):


const query = 'INSERT INTO tbl1 (id, my_col) VALUES (?, ?)';
const udtValue = { type: 'number', supported_filters: ['equals', 'gte']};
client.execute(query, [ "sample-recommended", udtValue ], { prepare: true });


Documentation: 

If you still want to use JSON (which is a nice feature, but maybe more suitable for REPL or other, less rich, scripting environments:


INSERT INTO tbl1 JSON '{ "id": "sample_json", "my_col": { "type": "number", "supported_filters": ["equals", "gte"] } }';


Hope it helps,
Jorge

--
You received this message because you are subscribed to the Google Groups "DataStax Node.js Driver for Apache Cassandra Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs-driver-user+unsub...@lists.datastax.com.

Reply all
Reply to author
Forward
0 new messages