Export cassandra query result to a csv file

后端 未结 13 1858
刺人心
刺人心 2020-12-08 06:18

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

相关标签:
13条回答
  • 2020-12-08 07:07

    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

    • Creating a dump

    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)

    • Removing whitespace characters from dump(It avoids removing whitespace withing json data)

    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 = '|';"

    0 讨论(0)
  • 2020-12-08 07:08

    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.

    0 讨论(0)
  • 2020-12-08 07:11

    I just wrote a tool to export CQL query to CSV and JSON format. Give it a try :)

    https://github.com/tenmax/cqlkit

    0 讨论(0)
  • 2020-12-08 07:12

    In windows, double quotes should be used to enclose the CQL.

    cqlsh -e"SELECT video_id,title FROM stackoverflow.videos" > output.txt

    0 讨论(0)
  • 2020-12-08 07:13

    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

    0 讨论(0)
  • 2020-12-08 07:16

    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

    0 讨论(0)
提交回复
热议问题