$ cat test
row-1 colfam1 quan1 val1
row-2 colfam1 quan2 val2
Now , we have a table(columnfamily) in Cassandra "hbtable" in "mykeyspace' ,in which we have to load the data from this file . We have four columns in this table (rowkey , cf , quantifier,value) with data type varchar .
Following is example of loading data in to this table using Cassandra Hector API:
public class SimpleWriteHector {
public static void main(String[] args) throws IOException {
Cluster cluster = HFactory.getOrCreateCluster("Test Cluster","192.168.213.1:9160,192.168.213.2;9160");
Keyspace keyspace = HFactory.createKeyspace("mykeyspace", cluster);
Serializer se= StringSerializer.get() ;
Mutator mutator = HFactory.createMutator(keyspace, se);
BufferedReader br = new BufferedReader(new FileReader("/home/user/test"));
String line;
while((line=br.readLine())!=null)
{
String columns[]=line.split("\\s");
mutator.addInsertion(columns[0], "hbtable", HFactory.createStringColumn("cf",columns[1]))
.addInsertion(columns[0], "hbtable", HFactory.createStringColumn("quantifier",columns[2]))
.addInsertion(columns[0], "hbtable", HFactory.createStringColumn("value",columns[3]));
}
mutator.execute();
System.out.println("Records inserted");
}
}
Lets check it now in Cassandra :
cqlsh:mykeyspace>select * from hbtable;
rowkey | cf | quantifier | value
--------+---------+------------+-------
row-2 | colfam1 | quan2 | val2
row-1 | colfam1 | quan1 | val1
No comments:
Post a Comment