Cassandra 3.0 导入导出文本(cqlsh+copy)

官网资料:

https://docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlshCopy.html

数据文件

可以是任意分隔符文件,带表头不带表头均可,这里带表头了

  • test.file:
c1|c2|c3|value
1|1|1|a
2|2|2|a

建表

  • 创建表空间
    CREATE KEYSPACE myks WITH REPLICATION = { ‘class’ : ‘SimpleStrategy’, ‘replication_factor’ : 1 };

  • 建表
    create table mytable(c1 int,c2 int,c3 int,value text, primary key(c1,c2,c3));

数据导入

语法:

COPY table_name [( column_list )]
FROM 'file_name'[, 'file2_name', ...] | STDIN
[WITH option = 'value' [AND ...]]

示例:

copy myks.mytable from '/path/to/test.file' with delimiter='|' and header=true;

输出:
Using 7 child processes

Starting copy of myks.mytable with columns [c1, c2, c3, value].
Processed: 2 rows; Rate:       3 rows/s; Avg. rate:       5 rows/s
2 rows imported from 1 files in 0.425 seconds (0 skipped).

如果有数据格式不对无法导入,会将未成功导入的数据输出至执行 cqlsh 脚本的路径下

Failed to import 40 rows: ParseError - Invalid row length 6 should be 5,  given up without retries
Failed to process 40 rows; failed rows written to import_myks_mytable.err

数据导出

语法:

COPY table_name [( column_list )]
TO 'file_name'[, 'file2_name', ...] | STDOUT
[WITH option = 'value' [AND ...]]

示例:

copy myks.mytable to '/Users/qiaojialin/Desktop/test.csv' with delimiter='|' and header=true;

输出:
Using 7 child processes

Starting copy of myks.mytable with columns [c1, c2, c3, value].
Processed: 2 rows; Rate:       8 rows/s; Avg. rate:       8 rows/s
2 rows exported to 1 files in 0.253 seconds.

你可能感兴趣的:(Cassandra)