Cassandra Data Modeling : Dynamic column - cassandra

I have a table user_activity.
user_activity
{
user_id text
user_name text
email_id text
password text
activity1 text}
Is it possible to add column dynamically?
for ex -
user1(user_id : Rowkey)
user_name, emai_id, password, activity1, activity2
user2(user_id : Rowkey)
user_name, emai_id, password, activity1, activity2, activity3
user3(user_id : Rowkey)
user_name, emai_id, password, activity1
since activities could be of any number.
thanks in advance :)

You can use Map
For example :
CREATE TABLE user_activity (
user_id text primary key,
emai_id text,
password text,
activity map<text,text>
);
Insert :
INSERT INTO user_activity (user_id, emai_id, password, activity) VALUES ('1', 'a1#b.com','password1', {'activity1' : 'test activity 1', 'activity2' : 'test acitivty 2' });
Update :
UPDATE user_activity SET activity['activity1'] = 'updated test activity 1' where user_id = '1';
Delete :
DELETE activity['activity2'] FROM user_activity WHERE user_id = '1';
Note : Keep the collection (map) size small, In Cassandra for map Maximum number of keys: 65535 and values size: 65535

Related

Cassandra: If a field inside an UDT is set to null, does this create a tombstone in Cassandra?

Please look at the following example:
Insert
INSERT INTO my_keyspace.my_table (id, name, my_info) VALUES (
3464546,
'Sumit',
{ birthday : '1990-01-01', height : '6.2 feet', weight : '74 kg' }
);
Second Insert
INSERT INTO my_keyspace.my_table (id, name, my_info) VALUES (
3464546,
'Sumit',
{ birthday : '1990-01-01', height : '6.2 feet', weight : null }
);
Consider "id" as the Primary Key.
In the second insert "weight" attribute inside "my_info" UDT is set as null. Does this create a tombstone? How null inside an UDT is stored in the Cassandra database?
Yes Setting a column to NULL is the same as writing a tombstone in some cases.

Reference logical aggregates - CQL

I have a univerity assignment that consists in working with CQL and Cassandra.
In the first part of the assignment I have to "identify the
reference logical aggregates, thus providing a row-oriented view of schema information, and point out attributes which model relationships", but I don't understand what is a "reference logical aggregate".
The schema is the following (it's not created by me, I downloaded it with the assignment):
DROP KEYSPACE gameindustry;
CREATE KEYSPACE gameindustry WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
USE gameindustry;
CREATE TYPE fullname (
firstname text,
lastname text
);
CREATE TABLE games (
name text PRIMARY KEY,
description text,
tags set<text>
);
CREATE TABLE tags (
tag text PRIMARY KEY,
games set<text>
);
CREATE TABLE users (
email text PRIMARY KEY,
password text,
name fullname,
notes text,
games set<text>
);
CREATE TABLE user_games (
user text,
game text,
PRIMARY KEY (user, game)
);
CREATE TABLE friends (
user text,
friend text,
PRIMARY KEY (user, friend)
);
CREATE TABLE matches (
user text,
game text,
when timestamp,
score int,
PRIMARY KEY (game, when, user),
);

Cassandra insert data into table column with data type set<text>

We're trying to setup an INSERT statement on a table with a set<text> column data type in Cassandra, but have come up with no luck. Here's an example:
INSERT INTO test_table
(my_items)
VALUES
([{"test1":"test val 1","test2":"test val 2"}])
The result is always something like:
no viable alternative at input for [test val ]2
Enclosing values in curly brackets and separate by comma
Example :
Let's we have the schema :
CREATE TABLE users (
user_id text PRIMARY KEY,
first_name text,
last_name text,
emails set<text>
);
Insert :
INSERT INTO users (user_id, first_name, last_name, emails)
VALUES('frodo', 'Frodo', 'Baggins', {'f#baggins.com', 'baggins#gmail.com'});
Update :
UPDATE users SET emails = emails + {'fb#friendsofmordor.org'}
WHERE user_id = 'frodo';
More Using the set type
Edited
If you want to insert value "test1":"test val 1" and "test2":"test val 2" into set then enclose each value with single quote
Example :
INSERT INTO users (user_id, first_name, last_name, emails)
VALUES('2011331035', 'Md Ashraful', 'Islam', {'"test1":"test val 1"', '"test2":"test val 2"'});

I have my type author and i put it in map in my table. How can I get access to map author, delete eg. firstname or delete lastname etc.?

My type:
CREATE TYPE author(
firstname text,
lastname text
);
My table:
CREATE TABLE ksiazki(
publishing text,
origin text,
id int,
title text,
author map<text, frozen<author>>,
categories set<text>,
pages int,
PRIMARY KEY ((publishing,origin), id, title)
);
You can't update or delete firstname or lastname from frozen author. you have to update or delete that whole author value
You can insert data using.
INSERT INTO ksiazki (publishing , origin , id , title , author ) VALUES ( 'pub', 'orig', 1, 'Title', {'author 1' : {firstname:'MD', lastname : 'Test'}}) ;
If you want to change 'author 1' name just update with this query :
UPDATE ksiazki SET author = author + {'author 1' : {firstname:'Ashraful', lastname : 'Islam'}} where publishing = 'pub' and origin = 'orig' and id = 1 and title = 'Title';
If you want to add another author 'author 2' use this query :
UPDATE ksiazki SET author = author + {'author 2' : {firstname:'Ashraful', lastname : 'Alom'}} where publishing = 'pub' and origin = 'orig' and id = 1 and title = 'Title';
If you want to delete 'author 2' use this query :
DELETE author['author 2'] FROM ksiazki where publishing = 'pub' and origin = 'orig' and id = 1 and title = 'Title';

Cassandra + Fetch the last records using in query

I am new in this cassandra database using with nodejs.
I have user_activity table. In this table data will insert based on user activity.
Also I have some user list. I need to fetch the data in that particular users and last record.
I don't interest to put the query in for loop. Have any other idea to achieve this?
Example Code:
var userlist = ["12", "34", "56"];
var query = 'SELECT * FROM user_activity WHERE userid IN ?';
server.user.execute(query, [userlist], {
prepare : true
}, function(err, result) {
console.log(results);
});
How to get the user lists for last one ?
Example:
user id = 12 - need to get last record;
user id = 34 - need to get last record;
user id = 56 - need to get last record;
I need to get these 3 records.
Table Schema:
CREATE TABLE test.user_activity (
userid text,
ts timestamp,
clientid text,
clientip text,
status text,
PRIMARY KEY (userid, ts)
)
It is not possible if you use the IN filter.
If it is a single user_id filter you can apply order by. Of course you need a column for inserted/updated time. So query will be like this:
SELECT * FROM user_activity WHERE user_id = 12 ORDER BY updated_at LIMIT 1;
You can put N value to get number of records
SELECT * FROM user_activity WHERE userid IN ? ORDER BY id DESC LIMIT N

Resources