How do I extract the Cassandra table data into json file using python or directly through cqlsh?

393 Views Asked by At

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

1

There are 1 best solutions below

0
Aaron On

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}