【问题标题】:Import CSV with multi-valued (collection) attributes to Cassandra将具有多值(集合)属性的 CSV 导入 Cassandra
【发布时间】:2015-05-28 15:04:26
【问题描述】:

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

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

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

一个示例 csv 记录是:

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

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

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

【问题讨论】:

    标签: csv collections cassandra


    【解决方案1】:

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

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

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

    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]
    

    【讨论】:

    • 在 CQL 3.1.1 中对我不起作用:copy mylist from STDIN;'test', "['foo','bar','baz']"Record #0 (line 1) has the wrong number of fields (4 instead of 2)
    • 这是我必须做的:copy mylist from STDIN;'test', [\'foo\'\,\'bar\'\,\'baz\']\.
    猜你喜欢
    • 1970-01-01
    • 2015-09-13
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2019-06-16
    • 2013-08-12
    • 1970-01-01
    相关资源
    最近更新 更多