Cassandra vs. MongoDB - Saving JSON data with previously unknown keys?

I am trying to integrate a NoSQL database to store JSON data, not an SQL database to store JSON data (the column in which the JSON object is stored).

For MongoDB, I can insert a JSON file by simply doing:

document = <JSON OBJECT> collection.insert(document) 

However, for Kassandra, according to this web page: http://www.datastax.com/dev/blog/whats-new-in-cassandra-2-2-json-support

This cannot be less than a schema, which means that I will need to create a table in advance:

 CREATE TABLE users ( id text PRIMARY KEY, age int, state text ); 

And then paste the data:

 INSERT INTO users JSON '{"id": "user123", "age": 42, "state": "TX"}'; 

The problem is that I want to try using Cassandra, I just finished the DataStax tutorial, but it seems to me that I need to know the JSON data keys in advance, which is impossible.

Or do I need to change the table when there is a new data column, if there is an unknown key? This does not seem like a very good design decision.

Can someone point me in the right direction? Thanks

+5
source share
2 answers

This JSON support is very misleading - it is JSON in Cql, not in the repository.

Or do I need to change the table when there is a new data column, if an unknown key? This does not seem like a very good design decision.

Indeed, this is not a good solution - your fields in JSON can have different types for all objects - a single column name cannot serve all of this. In addition, adding a new field requires spreading the scheme across your cluster, so the very first insert (which will contain data with an alternative table + insert) will be very slow.

Cassandra does not give you any built-in mechanism, but what you can do is to put all JSON in one field and set the necessary properties in additional separate columns. For instance:

 CREATE TABLE users ( id text PRIMARY KEY, json text, //in json age and state age int //explicit duplicated property - if you need eg index ); 

BTW. AFAIK Cassandra has supported your case for a long time, but now it is more “strong typed”.

+8
source

CQL provides the ability to create and use custom data types. You can create a data type to handle multiple fields. so you can just use the command 'create type' cql http://www.tutorialspoint.com/cassandra/cassandra_cql_user_defined_datatypes.htm

0
source

Source: https://habr.com/ru/post/1233091/


All Articles