将cassandra查询结果导出到csv文件 [英] Export cassandra query result to a csv file

查看:2173
本文介绍了将cassandra查询结果导出到csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是cassandra的新用户,我必须将特定查询的结果导出到csv文件。

I'm new in cassandra, and I have to export the result of a specific query to a csv file.

我发现 COPY 命令,但是(从我的理解)它允许你只将现有的表复制到csv文件,我想要的是直接复制我的查询的stdout到csv文件。有没有办法用 COPY 命令或用另一种方式?

I found the COPY command, but (from what I understand) it allows you only to copy an already existing table to a csv file, and what I want is to copy directly the stdout of my query to the csv file. is there any way to do it with COPY command or with another way ?

我的命令是style c $ c> select column1,column2 from table where condition = xy )和我使用 cqlsh

My command is style (select column1, column2 from table where condition = xy) and I'm using cqlsh.

推荐答案

如果你不介意你的数据使用管道('|')作为分隔符,你可以尝试使用 -e 在cqlsh上的标志。 -e 标志允许您从命令提示符处向Cassandra发送查询,您可以在此输出重定向甚至执行grep / awk /任何操作。

If you don't mind your data using a pipe ('|') as a delimiter, you can try using the -e flag on cqlsh. The -e flag allows you to send a query to Cassandra from the command prompt, where you could redirect or even perform a grep/awk/whatever on your output.

$ bin/cqlsh -e'SELECT video_id,title FROM stackoverflow.videos' > output.txt
$ cat output.txt

 video_id                             | title
--------------------------------------+---------------------------
 2977b806-df76-4dd7-a57e-11d361e72ce1 |                 Star Wars
 ab696e1f-78c0-45e6-893f-430e88db7f46 | The Witches of Whitewater
 15e6bc0d-6195-4d8b-ad25-771966c780c8 |              Pulp Fiction

(3 rows)

旧版本的cqlsh具有 -e 标志。对于旧版本的cqlsh,您可以将命令放入一个文件,并使用 -f 标志。

Older versions of cqlsh don't have the -e flag. For older versions of cqlsh, you can put your command into a file, and use the -f flag.

$ echo "SELECT video_id,title FROM stackoverflow.videos;" > select.cql
$ bin/cqlsh -f select.cql > output.txt

从这里开始执行 cat on output.txt应该产生与上面相同的行。

From here, doing a cat on output.txt should yield the same rows as above.

这篇关于将cassandra查询结果导出到csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆