Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting multiple types in map in cassandra

Tags:

cassandra

cql3

Is it possible in cassandra map to input different data types like if I have a table like

(id int, value map<text,text>)

Now I want to insert values in this table like

(1,{'test':'test1'})
(2,{'a':1})
(3,{'c':2})
like image 945
Nipun Avatar asked Aug 04 '15 14:08

Nipun


People also ask

How do I add a map value in Cassandra?

Use the UPDATE command to insert values into the map. Append an element to the map by enclosing the key-value pair in curly brackets and using the addition (+) operator. cqlsh> UPDATE cycling.

How do I use the map in Cassandra?

Use a map when pairs of related elements must be stored as a key-value pair. A map relates one item to another with a key-value pair. For each key, only one value may exist, and duplicates cannot be stored. Both the key and the value are designated with a data type.

Is insert and update same in Cassandra?

Is insert and update same in Cassandra? Insert, Update, and Upsert Because Cassandra uses an append model, there is no fundamental difference between the insert and update operations. If you insert a row that has the same primary key as an existing row, the row is replaced.

What is frozen in Cassandra?

A frozen value serializes multiple components into a single value. Non-frozen types allow updates to individual fields. Cassandra treats the value of a frozen type as a blob. The entire value must be overwritten.


2 Answers

The Cassandra Map type does not support values (or keys) of differing types. However, you could create a User Defined Type to handle that.

aploetz@cqlsh:stackoverflow2> CREATE TYPE testac (test text, a int, c int);

aploetz@cqlsh:stackoverflow2> CREATE TABLE testactable (
                                    key int, 
                                    values frozen<testac>,
                                    PRIMARY KEY (key));

aploetz@cqlsh:stackoverflow2> INSERT INTO testactable (key,values) 
                              VALUES (1,{test: 'test1', a: 1, c: 2});

aploetz@cqlsh:stackoverflow2> SELECT * FROm testactable ;

 key | values
-----+-----------------------------
   1 | {test: 'test1', a: 1, c: 2}

(1 rows)
like image 112
Aaron Avatar answered Sep 22 '22 13:09

Aaron


Instead of having map in you case have it as text (String) column which will save you lots of space. Keep data in JSON format by stringifying it.

like image 35
Aftab Avatar answered Sep 23 '22 13:09

Aftab