How do I store nested data in Cassandra
jeffj from #cassandra on irc advises that i don't even need the first table.
i'm starting to understand it now.
CREATE TABLE posts ( name text, field text, title text, value text, PRIMARY KEY (name, field));INSERT INTO posts (name, field, title, value) VALUES ( 'kingsbounty', 'title', 'Game Title', 'Kings Bounty');INSERT INTO posts (name, field, title, value) VALUES ( 'kingsbounty', 'body', 'Game Description', 'Kings Bounty is a turn-based fantasy...');INSERT INTO posts (name, field, title, value) VALUES ( 'outrun', 'vehicle', 'Vehicle', 'Ferrari Testarossa');INSERT INTO posts (name, field, title, value) VALUES ( 'outrun', 'color', 'Vehicle Color', 'Red');INSERT INTO posts (name, field, title, value) VALUES ( 'outrun', 'driver', 'Driver', 'David Hasselhoff');SELECT field FROM posts WHERE name = 'kingsbounty'; field------- body titleSELECT * FROM posts WHERE name = 'kingsbounty'; name | field | title | value-------------+-------+------------------+----------------------------------------- kingsbounty | body | Game Description | Kings Bounty is a turn-based fantasy... kingsbounty | title | Game Title | Kings BountySELECT fields FROM posts WHERE name = 'outrun'; field--------- color driver vehicleSELECT * FROM posts WHERE name = 'outrun'; name | field | title | value--------+---------+---------------+-------------------- outrun | color | Vehicle Color | Red outrun | driver | Driver | David Hasselhoff outrun | vehicle | Vehicle | Ferrari Testarossa
Create table with whatever information you want to return. Assuming you need all the information to be returned, store it into a single table as shown below and do the necessary manipulations at client side.
CREATE TABLE posts ( id uuid, name text, fields map<text,text>, PRIMARY KEY (id));insert into posts (id,name,fields) values (uuid(),'kingsbounty',{'title':'"title": "Game Title","value": "Kings Bounty"','body':'"title": "Game Description","value": "Kings Bounty is a turn-based fantasy..."'});insert into posts (id,name,fields) values (uuid(),'outrun',{'vehicle':'"title": "Vehicle","value": "Ferrari Testarossa"','color':'"title": "Vehicle Color","value": "Red"','driver':'"title": "Driver","value": "David Hasselhoff"'}); cqlsh> select id,name,fields from posts; id | name | fields--------------------------------------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- dd31393d-2654-42ec-a5fb-73ab13c12932 | kingsbounty | {'body': '"title": "Game Description","value": "Kings Bounty is a turn-based fantasy..."', 'title': '"title": "Game Title","value": "Kings Bounty"'} a1e2b512-7177-4a2d-8da3-528b9d5097c0 | outrun | {'color': '"title": "Vehicle Color","value": "Red"', 'driver': '"title": "Driver","value": "David Hasselhoff"', 'vehicle': '"title": "Vehicle","value": "Ferrari Testarossa"'}