Wednesday, December 2, 2015

How to use Cassandra Bulk Loader

In this blog post, I will explain how to Cassandra Bulk Loader to load the data into Cassandra node(s).

Step 1: Create a table as mentioned below.
CREATE TABLE mytestkeyspace.userinfo (id int PRIMARY KEY,name text,age int);

Step 2: Create a dataload directory,keyspace and table directories in your Cassandra data directory as mentioned below. This userinfo directory is used to place all the generated SSTables files.
C:\Murali\cassandra\data\dataload\mytestkeyspace\userinfo

Step 3: Create a simple, sample CQL3SSTDataLoader.java file as mentioned below here.

public static void main(String[] args) throws InvalidRequestException, IOException {

String schema = "CREATE TABLE mytestkeyspace.userinfo ("
+ " id int PRIMARY KEY,"
+ " name text,"
+ " age int"
+ ")";
String insert = "INSERT INTO mytestkeyspace.userinfo (id, name, age) VALUES (?, ?, ?)";

CQLSSTableWriter writer = CQLSSTableWriter.builder()
.inDirectory("C:\\Murali\\cassandra\\data\\dataload\\mytestkeyspace\\userinfo")
.forTable(schema)
.using(insert).build();
// Adds a number of rows to the resulting sstable
writer.addRow(1, "John", 24);
writer.addRow(2, "Rob", 20);
writer.addRow(3, "Peter", null);
// Close the writer, finalizing the sstable
writer.close();
}

Step 4:Compile the above program in Eclipse IDE and run it from Eclipse IDE.

Step 5:Now go the C:\Murali\cassandra\data\dataload\mytestkeyspace\userinfo to see the generated SSTable related files.
Step 6: Execute the below command in the command windows to load the data into userinfo table.
sstableloader -v -d 127.0.0.1 mytestkeyspace/userinfo

The above command loads the sample 3 three records into userinfo table to the 127.0.0.1 node in the cluster.





No comments:

Post a Comment