I\'m new in cassandra, and I have to export the result of a specific query to a csv file.
I found the COPY
command, but (from what I understand) it allo
Follow the below steps to selectively export & import the Cassandra data.
Exporting:
Write all the select queries in a file named dump.cql like below
paging off;
select * from student where id=10;
select * from student where id=15;
Note: Paging off is mandatory above the queries to avoid limiting the query results to default 100 records
cqlsh -u user_name -p 'password' ip_address -k keyspace_name -f dump.cql > dump.csv;
(for remote machine)
or
cqlsh -k keyspace_name -f dump.cql > dump.csv;
(for local machine)
sed -r 's/(\".*\")|\s*/\1/g' dump.csv > data_without_spaces.csv
Importing:
cqlsh -e "copy keyspace_name.table_name from 'data_without_spaces.csv' with delimiter = '|';"
Cannot comment... To deal with "MORE" issue when there are more than 100 rows, simply add "paging off" before the SQL.
Something like
$ bin/cqlsh -e'PAGING OFF;SELECT video_id,title FROM stackoverflow.videos' > output.txt
This will cause a little messy at the beginning of the output file but can easily be removed afterwards.
I just wrote a tool to export CQL query to CSV and JSON format. Give it a try :)
https://github.com/tenmax/cqlkit
In windows, double quotes should be used to enclose the CQL.
cqlsh -e"SELECT video_id,title FROM stackoverflow.videos" > output.txt
The person asking asked for CSV not text.
I did this hack get my results. It worked for me and I moved on with my day.
me:~/MOOSE2# echo "USE ████it; select * from samples_daily_buffer where dog_id=██48;" | cqlsh --cqlversion="3.4.4" cassandra0.stage.███████ | sed -e "s/
| */,/g" | sed -e "s/^ *//g" | tail -n +4 > ./myfile.csv
CQL COPY is good option for importing or exporting data. But if you want to analyze some small query output you can run below command and save the output in a file.
cqlsh -e "SELECT * FROM table WHERE column = 'xyz' > queryoutput.txt
However, you can use CAPTURE also for saving output of the query to analyze something