Wednesday 12 February 2020

Cassandra: Load the cql commands from a file

SOURCE command is used to execute cql command from a file.

Example
SOURCE '/Users/krishna/Documents/commands.cql

commands.cql
DROP KEYSPACE cassandratutorial;

CREATE KEYSPACE cassandratutorial WITH REPLICATION = 
{ 
  'class' : 'SimpleStrategy', 
  'replication_factor' : 1 
};

CREATE TABLE IF NOT EXISTS cassandratutorial.employee (
  id INT PRIMARY KEY, 
  firstName VARCHAR,
  lastName VARCHAR,
  age int,
);


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}';
INSERT INTO cassandratutorial.employee JSON '{"id" : 4, "firstName" : "Chitra", "lastName" : "Rajan", "age" : 45}';

$ cqlsh
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> 
cqlsh> SOURCE '/Users/krishna/Documents/commands.cql'
cqlsh> 
cqlsh> SELECT * FROM cassandratutorial.employee;

 id | age | firstname | lastname
----+-----+-----------+----------
  1 |  30 |   Krishna |   Gurram
  2 |  31 |       Ram |   Gurram
  4 |  45 |    Chitra |    Rajan
  3 |  45 |     Vijay |   Ponnam

(4 rows)
cqlsh>


Previous                                                    Next                                                    Home

No comments:

Post a Comment