It is possible to insert data in JSON format using the INSERT ... JSON
CQL command. For example:
INSERT INTO table_name JSON '{
"column_name": "value"
}'
But it is a bit more nuanced than that so allow me to explain.
Data modeling in Cassandra is the complete opposite of data modeling in traditional relational databases. Instead of working out how to store the data into tables, we first start by listing all the application queries THEN designing a table for EACH of the app queries. We do this so the tables are optimised for reads.
For example, assuming the application needs to "retrieve all cards for a user", we need to design a table such that:
- the data is partitioned by users, AND
- the cards are "clustered" (grouped together).
The table schema would look something like:
CREATE TABLE cards_by_user (
user_id int,
card_number int,
card_type text,
card_expiry text,
...
PRIMARY KEY (user_id, card_number)
) WITH CLUSTERING ORDER BY (card_number ASC)
In contrast to two-dimensional tables in RDBMS, this Cassandra table is multi-dimensional such that each partition (user) can have one or more rows (cards).
To create a new card entry for a user, the CQL statement to insert JSON formatted data looks like:
INSERT INTO cards_by_user
JSON '{
"user_id": "123",
"card_number": "456",
"card_type": "visa",
"card_expiry": "07/2028"
}'
You can insert multiple rows of cards using the same INSERT
format. For example:
INSERT INTO cards_by_user
JSON '{
"user_id": "123",
"card_number": "789",
"card_type": "mastercard",
"card_expiry": "04/2025"
}'
To retrieve all the cards for a user:
SELECT * FROM cards_by_user WHERE user_id = 123;
user_id | card_number | card_expiry | card_type
---------+-------------+-------------+------------
123 | 456 | 07/2028 | visa
123 | 789 | 04/2025 | mastercard
As you can see, it is not necessary to use user-defined types (UDTs) to store the data. We recommend mapping the data to native CQL columns instead of UDTs whenever possible to reduce the level of complexity required to maintain your data and code.
As a side note, if you prefer to work with JSON documents then have a look at Stargate.io -- an open-source data API gateway which has a Document API which allows you to store and retrieve JSON documents similar to MongoDB.
Have a look at the free tutorial on datastax.com/dev where you can try it out in a hands-on lab. Cheers!