How to export data from Cassandra to CSV file using Java -


i tried use datastax java driver, found out not support copy command, know other methods of exporting data using java? thanks.

for example have created event table:

cqlsh:kunderatest> describe table event ;  create table event (   id text,   log text,   timstamp bigint,   primary key (id) ) 

and inserted 3 record

cqlsh:kunderatest> insert event (id, log , timstamp ) values ( '1', 'my first log' , 12345678); cqlsh:kunderatest> insert event (id, log , timstamp ) values ( '2', 'my second log' , 12345679); cqlsh:kunderatest> insert event (id, log , timstamp ) values ( '3', 'my third log' , 12345680); 

1) first can using cqlsh client. can export data of event table file (in case log.txt) executing following command.

cqlsh:kunderatest>  copy kunderatest.sample (id, name, age, address) './log.txt' delimiter = '|' , quote = '''' , escape = '''' , null = '<null>'; 3 rows exported in 0.042 seconds. 

you can validate command output verify log.txt file. hope you.

2) second can use runtime utility of java execute export command in order achieve goal.

  • create file (let command.txt) , paste following export command file.

    copy kunderatest.sample (id, name, age, address) './log.txt' delimiter = '|' , quote = '''' , escape = '''' , null = '<null>' 

after creating file , adding above command file following export data file given in export command.

    string exportcommand = cassandrahome + "bin/cqlsh " + hostname + " " + rpcport + " -f command.txt"; // file holds export command      runtime runtime = runtime.getruntime();     process process = runtime.exec(exoprtcommand);      // keep tracking log, can following.      inputstream = process.getinputstream();     inputstreamreader isr = new inputstreamreader(is);     bufferedreader br = new bufferedreader(isr);     string line = null;      while ((line = br.readline()) != null)     {      } 

note: cassandrahome path of cassandra package directory. in case /usr/local/apache-cassandra-2.0.6


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -