TechnologyJune 15, 2015

What’s New in Cassandra 2.2: JSON Support

Tyler Hobbs
Tyler Hobbs
What’s New in Cassandra 2.2: JSON Support
CREATE TABLE users (
    id text PRIMARY KEY,
    age int,
    state text
);
INSERT INTO users (id, age, state) VALUES ('user123', 42, 'TX');
INSERT INTO users JSON '{"id": "user123", "age": 42, "state": "TX"}';
insert_statment = session.prepare('INSERT INTO users JSON ?')
json_values = '{"id": "user123", "age": 42, "state": "TX"}'
session.execute(insert_statement, [json_values])
import json
prepared = session.prepare('INSERT INTO users JSON ?')
while True:
    user = {'id': get_username(user_input),
            'age': get_age(user_input),
            'state': get_state(user_input) or 'TX'}
    session.execute(prepared, [json.dumps(user)])
INSERT INTO uuid_map (id, theuuid) VALUES (10, 994FF312-111E-11E5-9FDE-E0B9A54A6D93);
INSERT INTO uuid_map JSON '{"id": 10, "theuuid": "994FF312-111E-11E5-9FDE-E0B9A54A6D93"}';
CREATE TABLE example (
    id int PRIMARY KEY,
    tupleval tuple<int, text>,
    numbers set<int>,
    words list<text>
)
INSERT INTO example JSON '{"id": 0, "tupleval": [1, "abc"], "numbers": [1, 2, 3], "letters": ["a", "b", "c"]}';
CREATE TYPE address (number int, street text);
CREATE TABLE users (
    id int PRIMARY KEY,
    street_address frozen<address>
)
INSERT INTO users JSON '{"id": 0, "street_address": {"number": 123, "street": "Main St."}}';
CREATE TABLE comments (
    id int PRIMARY KEY,
    int_map map<int, text>
)
INSERT INTO comments JSON '{"id": 10, "int_map": {"1": "foo", "2": "bar"}}';
CREATE TABLE tags (
    id int PRIMARY KEY,
    tags map<frozen<set<text>>, text>
)
INSERT INTO tags JSON '{"id": 10, "tags": {"[\"tag1\", \"tag2\"]": "details"}}';
CREATE TABLE users (
    id text PRIMARY KEY,
    age int,
    state text
);
INSERT INTO users JSON '{"ID": "user123", "Age": 42, "StAtE": "TX"}';
CREATE TABLE users (
    id text PRIMARY KEY,
    "Age" int,
    "State" text
);
INSERT INTO users JSON '{"id": "user123", "\"Age\"": 42, "\"State\"": "TX"}';
CREATE TABLE users (
    id text PRIMARY KEY,
    age int,
    state text
);
SELECT JSON * FROM users;
[json]
-------------------------------------------
{"id": "user123", "age": 42, "state": "TX"}
SELECT JSON id, writetime(age), ttl(state) as ttl FROM users;
[json]
------------------------------------------------------------------
{"id": "user123", "writetime(age)": 1434135381782986, "ttl": null}
UPDATE users SET age = fromJson('42') WHERE id = fromJson('"user123"');
SELECT id, toJson(tags) as tags FROM tags;
id | tags
---+------------------------------------
10 | {"[\"tag1\", \"tag2\"]": "details"}
Discover more
Releases
Share

One-stop Data API for Production GenAI

Astra DB gives JavaScript developers a complete data API and out-of-the-box integrations that make it easier to build production RAG apps with high relevancy and low latency.