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
With bash:
If you need to query the data (not possible with COPY TO) and if you need the final product to be importable (ie with COPY FROM):
cqlsh -e "SELECT * FROM bar WHERE column = 'baz' > raw_output.txt
Then you can reformat the output with sed
sed 's/\ //g; /^----.*/d; /^(/d; /^\s*$/d;' raw_output.txt | tee clean_output.csv
Which pretty much says
sed 'remove spaces; remove the column boarder; remove lines beginning with (COUNT X); and remove blank lines' | write output into clean_output.csv
The sed regexp's could be cleaned up to better suite your specific case, but thats the general idea.
cqlsh> CAPTURE cqlsh> CAPTURE '/home/Desktop/user.csv'; cqlsh> select *from user; Now capturing query output to '/home/Desktop/user.csv'.
Now, view the output of the query in /home/Desktop/user.csv
You can use the COPY command to create the CSV file. e.g. copy table with selected columns. Columns are optional, if you select them, every column will be picked.
COPY TABLE (COL1, COL2) TO 'filename.csv' HEADER=TRUE/FALSE
For more reference https://docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlshCopy.html
If I am understanding correctly you want to redirect your output to stdout?
Put your cql command in a file. My files is called select.cql and contents are:
select id from wiki.solr limit 100;
Then issue the following and you get it to stdout:
cqlsh < select.cql
I hope this helps. From there on you can pipe it and add commas, remove headers etc.
In 2020th you can use DSBulk to export or import data to/from CSV (by default), or JSON. It could be as simple as:
dsbulk unload -k keyspace -t table -u user -p password -url filename
DSBulk is heavily optimized for fast data export, without putting too much load onto the coordinator node that happens when you just run select * from table
.
You can control what columns to export, and even provide your own query, etc. See following blog posts for examples:
I believe DevCenter also allows you to copy to CSV. http://www.datastax.com/what-we-offer/products-services/devcenter