1

假设我想将一个 csv 文件导入下表:

CREATE TABLE example_table (
  id int PRIMARY KEY,
  comma_delimited_str_list list<ascii>,
  space_delimited_str_list list<ascii>
);

wherecomma_delimited_str_listspace_delimited_str_list是两个列表属性,分别使用逗号和空格作为分隔符。

一个示例 csv 记录将是:

12345,"hello,world","stack overflow"

我想将"hello,world""stack overflow"视为两个多值属性。

我可以知道如何将这样的 CSV 文件导入到 Cassandra 中的相应表中吗?最好使用 CQL COPY?

4

1 回答 1

4

CQL 1.2 能够将具有多值字段的 CSV 文件直接移植到表中。但是,这些多值字段的格式必须与 CQL 格式匹配。

例如,列表必须采用 形式['abc','def','ghi'],集合必须采用 形式{'123','456','789'}

example_table以下是将 CSV 格式的数据从 STDIN 导入到 OP 中提到的示例:

cqlsh:demo> copy example_table from STDIN;
[Use \. on a line by itself to end input]
[copy] 12345,"['hello','world']","['stack','overflow']"
[copy] 56780,"['this','is','a','test','list']","['here','is','another','one']"
[copy] \.

2 rows imported in 11.304 seconds.
cqlsh:demo> select * from example_table;

 id    | comma_delimited_str_list  | space_delimited_str_list
-------+---------------------------+--------------------------
 12345 |            [hello, world] |        [stack, overflow]
 56780 | [this, is, a, test, list] | [here, is, another, one]

从 CSV 文件导入格式不正确的列表或设置值会引发错误:

cqlsh:demo> copy example_table from STDIN;
[Use \. on a line by itself to end input]
[copy] 9999,"hello","world"
Bad Request: line 1:108 no viable alternative at input ','
Aborting import at record #0 (line 1). Previously-inserted values still present.

上述输入应替换为9999,"['hello']","['world']"

cqlsh:demo> copy example_table from STDIN;
[Use \. on a line by itself to end input]
[copy] 9999,"['hello']","['world']"
[copy] \.

1 rows imported in 16.859 seconds.
cqlsh:demo> select * from example_table;

 id    | comma_delimited_str_list  | space_delimited_str_list
-------+---------------------------+--------------------------
  9999 |                   [hello] |                  [world]
 12345 |            [hello, world] |        [stack, overflow]
 56780 | [this, is, a, test, list] | [here, is, another, one]
于 2013-08-09T21:41:01.917 回答