Import CSV with multi-valued (collection) attributes to Cassandra

前端 未结 1 687
面向向阳花
面向向阳花 2021-02-11 08:42

Suppose I would like to import a csv file into the following table:

CREATE TABLE example_table (
  id int PRIMARY KEY,
  comma_delimited_str_list list

        
1条回答
  •  庸人自扰
    2021-02-11 09:02

    CQL 1.2 is able to port CSV file with multi-valued fields directly to a table. However, the format of those multi-valued fields must match the CQL format.

    For example, lists must be in the form ['abc','def','ghi'], and sets must be in the form {'123','456','789'}.

    Below is an example of importing CSV formatted data to the example_table mentioned in the OP from STDIN:

    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]
    

    Importing incorrect formatted list or set values from a CSV file will raise an error:

    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.
    

    The above input should be replaced by 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]
    

    0 讨论(0)
提交回复
热议问题