将 cassandra 查询结果导出到 csv 文件 [英] Export cassandra query result to a csv file
问题描述
我是 cassandra 的新手,我必须将特定查询的结果导出到 csv 文件.
I'm new in cassandra, and I have to export the result of a specific query to a csv file.
我找到了 COPY
命令,但是(据我所知)它只允许您将已经存在的表复制到 csv 文件,而我想要的是直接复制我的标准输出查询 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 ?
我的命令是样式(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/whatever.
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
从这里开始,对 output.txt 执行 cat
应该产生与上面相同的行.
From here, doing a cat
on output.txt should yield the same rows as above.
这篇关于将 cassandra 查询结果导出到 csv 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!