If you
have data in ‘JSON’ format, then you can use below command to insert data into
a table.
INSERT
INTO cassandratutorial.employee JSON '{"id" : 1,
"firstName" : "Krishna", "lastName" :
"Gurram", "age" : 30}';
Below
step-by-step procedure explains how to insert json string to a table.
Step 1: Create a table cassandratutorial.
CREATE
keyspace cassandratutorial WITH REPLICATION =
{
'class' : 'SimpleStrategy',
'replication_factor' : 1
};
Step 2: Create a table employee in the
keyspace 'cassandratutorial'.
CREATE
TABLE IF NOT EXISTS cassandratutorial.employee (
id INT PRIMARY KEY,
firstName VARCHAR,
lastName VARCHAR,
age int,
) WITH
comment = 'Table to store Employee information';
Step 3: Execute below statements to insert 3
records to the table 'employee'.
INSERT
INTO cassandratutorial.employee JSON '{"id" : 1,
"firstName" : "Krishna", "lastName" :
"Gurram", "age" : 30}';
INSERT
INTO cassandratutorial.employee JSON '{"id" : 2,
"firstName" : "Ram", "lastName" :
"Gurram", "age" : 31}';
INSERT
INTO cassandratutorial.employee JSON '{"id" : 3,
"firstName" : "Vijay", "lastName" :
"Ponnam", "age" : 45}';
Step 4: Run 'SELECT *' query against employee
table to get all the information.
cqlsh> SELECT * FROM cassandratutorial.employee;
id | age | firstname | lastname
----+-----+-----------+----------
1 | 30 | Krishna | Gurram
2 | 31 | Ram | Gurram
3 | 45 | Vijay | Ponnam
(3 rows)
Sample
cqlsh console output is given below.
cqlsh> DROP KEYSPACE cassandratutorial;
cqlsh>
cqlsh> CREATE keyspace cassandratutorial WITH REPLICATION =
... {
... 'class' : 'SimpleStrategy',
... 'replication_factor' : 1
... };
cqlsh>
cqlsh> CREATE TABLE IF NOT EXISTS cassandratutorial.employee (
... id INT PRIMARY KEY,
... firstName VARCHAR,
... lastName VARCHAR,
... age int,
... ) WITH comment = 'Table to store Employee information';
cqlsh>
cqlsh> INSERT INTO cassandratutorial.employee JSON '{"id" : 1, "firstName" : "Krishna", "lastName" : "Gurram", "age" : 30}';
cqlsh> INSERT INTO cassandratutorial.employee JSON '{"id" : 2, "firstName" : "Ram", "lastName" : "Gurram", "age" : 31}';
cqlsh> INSERT INTO cassandratutorial.employee JSON '{"id" : 3, "firstName" : "Vijay", "lastName" : "Ponnam", "age" : 45}';
cqlsh>
cqlsh> SELECT * FROM cassandratutorial.employee;
id | age | firstname | lastname
----+-----+-----------+----------
1 | 30 | Krishna | Gurram
2 | 31 | Ram | Gurram
3 | 45 | Vijay | Ponnam
(3 rows)
No comments:
Post a Comment