Export cassandra query result to a csv file

后端 未结 13 1856
刺人心
刺人心 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 06:52

    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.

    0 讨论(0)
  • 2020-12-08 06:53
    1. Use CAPTURE command to export the query result to a file.
    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

    1. Use DevCenter and execute a query. Right click on the output and select "Copy All as CSV" to paste the output in CSV.

    0 讨论(0)
  • 2020-12-08 06:53

    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

    0 讨论(0)
  • 2020-12-08 06:53

    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.

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

    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:

    • https://www.datastax.com/blog/2019/03/datastax-bulk-loader-introduction-and-loading
    • https://www.datastax.com/blog/2019/04/datastax-bulk-loader-more-loading
    • https://www.datastax.com/blog/2019/04/datastax-bulk-loader-common-settings
    • https://www.datastax.com/blog/2019/06/datastax-bulk-loader-unloading
    • https://www.datastax.com/blog/2019/07/datastax-bulk-loader-counting
    • https://www.datastax.com/blog/2019/12/datastax-bulk-loader-examples-loading-other-locations
    0 讨论(0)
  • 2020-12-08 07:07

    I believe DevCenter also allows you to copy to CSV. http://www.datastax.com/what-we-offer/products-services/devcenter

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