我是cassandra的新手,我必须将特定查询的结果导出到csv文件。
我找到了COPY
命令,但是(据我了解)它只允许您将一个已经存在的表复制到csv文件中,而我想要的是直接将查询的标准输出复制到csv文件中。有什么办法可以使用COPY
命令或其他方式来做到这一点?
我的命令是style(select column1, column2 from table where condition = xy
),我正在使用cqlsh
。
最佳答案
如果您不介意使用竖线('|')作为分隔符,则可以尝试使用cqlsh上的-e
标志。 -e
标志允许您从命令提示符向Cassandra发送查询,您可以在其中重定向或什至对输出执行grep / awk /任何操作。
$ 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
标志。$ echo "SELECT video_id,title FROM stackoverflow.videos;" > select.cql
$ bin/cqlsh -f select.cql > output.txt
从这里开始,在output.txt上执行
cat
应该会产生与上述相同的行。