将Cassandra查询导出成CSV和JSON格式的工具:CQLKIT

jopen 9年前

cqlkit 是一个CLI工具用于将Cassandra查询导出成CSV和JSON格式。 

Here is a simple some examples.

Export JSON for the system columns in cassandra cluster.

cql2json -q "select * from system.schema_columns"

Export CSV for the system columns in cassandra cluster.

cql2csv -q "select * from system.schema_columns"

Requirement

  • Java8

Installation

  1. Download from release page.
  2. Unzip the package.
  3. Add$CQLKIT_PATH/binto the PATH environment variable

Usage

CQL2CSV

usage: cql2csv [-c contactpoint] [-q query] [FILE]  File       The file to use as CQL query. If both FILE and QUERY are             omitted, query will be read from STDIN.     -c <arg>              The contact point.   -cqlshrc <arg>        Use an alternative cqlshrc file location, path.   -h,--help             Show the help and exit   -H,--no-header-row    Do not output column names.   -k <arg>              The keyspace to use.   -l,--linenumbers      Insert a column of line numbers at the front of the                         output. Useful when piping to grep or as a simple                         primary key.   -p,--parallel <arg>   The level of parallelism to run the task. Default                         is sequential.   -q,--query <arg>      The CQL query to execute. If specified, it                         overrides FILE and STDIN.   -u <arg>              The user to authenticate.   -v,--version          Print the version

CQL2JSON

usage: cql2json [-c contactpoint] [-q query] [FILE]  File       The file to use as CQL query. If both FILE and QUERY are             omitted, query will be read from STDIN.     -c <arg>                  The contact point.   -cqlshrc <arg>            Use an alternative cqlshrc file location, path.   -h,--help                 Show the help and exit   -j,--json-columns <arg>   The columns that contains json string. The                             content would be used as json object instead of                             plain text. Columns are separated by comma.   -k <arg>                  The keyspace to use.   -l,--linenumbers          Insert a column of line numbers at the front of                             the output. Useful when piping to grep or as a                             simple primary key.   -p,--parallel <arg>       The level of parallelism to run the task.                             Default is sequential.   -q,--query <arg>          The CQL query to execute. If specified, it                             overrides FILE and STDIN.   -u <arg>                  The user to authenticate.   -v,--version              Print the version

Setup the cqlshrc

To connect to cassandra cluster, although we can use-cand-kto specify the contact server and keyspace respectively, to preapre a cqlshrc is recommended to simply your query. cqlshrc is used by cqlsh. cqlkit leverages this file to connect to your cluster. Here is the setup steps.

  1. Create the cqlshrc file at~/.cassandra/cqlshrc
  2. Here is the example format.

[authentication]  keyspace = system    [connection]  hostname = 192.168.59.103  port = 9042    ; vim: set ft=dosini :

推荐的第三方工具

  • csvkit - A toolkit to handle CSV files. There are many useful CLI tools included.

  • q - Another CSV tool which focuses on query on CSV files.

  • json2csv - Convert JSON format to CSV format

  • jq - a lightweight and flexible command-line JSON processor.

项目主页:http://www.open-open.com/lib/view/home/1438239676269