我尝试使用DataStax Java Driver,但后来我发现它不支持COPY
命令,有没有人知道使用Java导出数据的其他方法?感谢。
答案 0 :(得分:0)
例如我创建了事件表:
cqlsh:kunderatest> describe TABLE event ;
CREATE TABLE event (
id text,
log text,
timstamp bigint,
PRIMARY KEY (id)
)
并插入三条记录
cqlsh:kunderatest> INSERT INTO event (id, log , timstamp ) VALUES ( '1', 'my first log' , 12345678);
cqlsh:kunderatest> INSERT INTO event (id, log , timstamp ) VALUES ( '2', 'my second log' , 12345679);
cqlsh:kunderatest> INSERT INTO event (id, log , timstamp ) VALUES ( '3', 'my third log' , 12345680);
1)首先,您可以使用CQLSH客户端来完成。现在,您可以通过执行以下命令将事件表的数据导出到任何文件中(在本例中为log.txt)。
cqlsh:kunderatest> COPY kunderatest.sample (id, name, age, address) TO './log.txt' WITH DELIMITER = '|' AND QUOTE = '''' AND ESCAPE = '''' AND NULL = '<null>';
3 rows exported in 0.042 seconds.
您可以通过验证log.txt文件验证命令输出。希望它会对你有所帮助。
2)其次,您还可以使用Java的Runtime实用程序来执行导出命令以实现目标。
创建一个文件(比如说command.txt)并将以下导出命令粘贴到该文件中。
COPY kunderatest.sample (id, name, age, address) TO './log.txt' WITH DELIMITER = '|' AND QUOTE = '''' AND ESCAPE = '''' AND NULL = '<null>'
创建文件并将上述命令添加到该文件后,执行以下操作将数据导出到export命令中给出的文件中。
String exportCommand = cassandraHome + "bin/cqlsh " + hostname + " " + rpcPort + " -f command.txt"; // file which holds export command
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(exoprtCommand);
// for keep tracking the log, you can do following.
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
{
}
注意:cassandrahome是cassandra包目录的路径。就我而言,它是/usr/local/apache-cassandra-2.0.6