1. 程式人生 > >Cassandra 3.0 匯入匯出文字(cqlsh+copy)

Cassandra 3.0 匯入匯出文字(cqlsh+copy)

官網資料:

資料檔案

可以是任意分隔符檔案,帶表頭不帶表頭均可,這裡帶表頭了

  • 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.

大家有興趣的可以關注我的公眾號(資料庫漫遊指南),涉及分散式資料庫、大資料和個人成長分享,歡迎大家一起交流進步

這裡寫圖片描述