如何使用 python 或直接通过 cqlsh 将 Cassandra 表数据提取到 json 文件中?

发布于 2025-01-16 23:43:16 字数 40 浏览 4 评论 0原文

我想将 Cassandra 中的表中的数据导出到 json 文件。

I want to export the data from a table in Cassandra to json file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千仐 2025-01-23 23:43:16

使用 CQL,您实际上可以在 SELECT 之后使用 JSON 关键字。如果您使用 cqlsh 的 -e 标志从命令行运行查询,则可以将输出重定向到文件,如下所示:

% bin/cqlsh -u aaron -p password \
 -e 'SELECT JSON * FROM packt.astronauts_by_group WHERE group=2;' \
 > astronauts_group2.json

% head -n 4 astronauts_group2.json

 [json]
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"group": 2, "name": "Charles Conrad Jr. ", "alma_mater": "Princeton University", "birthplace": "Philadelphia, PA", "dob": "5/2/1930", "gender": "Male", "missions": "Gemini 5, Gemini 11, Apollo 12, Skylab 2", "spaceflight_hours": 1179, "spaceflights": 4, "spacewalk_hours": 12, "spacewalks": 4, "status": "Deceased", "year": 1962}

With CQL, you can actually use the JSON keyword after the SELECT. If you use cqlsh's -e flag to run the query from the command line, you can redirect the output to a file, like this:

% bin/cqlsh -u aaron -p password \
 -e 'SELECT JSON * FROM packt.astronauts_by_group WHERE group=2;' \
 > astronauts_group2.json

% head -n 4 astronauts_group2.json

 [json]
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"group": 2, "name": "Charles Conrad Jr. ", "alma_mater": "Princeton University", "birthplace": "Philadelphia, PA", "dob": "5/2/1930", "gender": "Male", "missions": "Gemini 5, Gemini 11, Apollo 12, Skylab 2", "spaceflight_hours": 1179, "spaceflights": 4, "spacewalk_hours": 12, "spacewalks": 4, "status": "Deceased", "year": 1962}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文