代码之家  ›  专栏  ›  技术社区  ›  samba

在RedShift表中存储数组的正确方法是什么?

  •  0
  • samba  · 技术社区  · 6 年前

    在Redshift中创建表时,我面临以下错误:

    Column "main.sales_metrics" has unsupported type "character varying[]".;
    

    在数据帧架构中,如下所示:

    |-- sales_metrics: array (nullable = true)
         |-- element: string (nullable = true)
    

    我试着像在PostgreSQL中一样声明这个列: sales_metrics text[] 正如我从文档中读到的,Amazon Redshift不支持PostgreSQL数据类型。

    那么我应该如何正确地声明 sales_metrics 存储的列 Array[String] 在RedShift中创建表时?

    2 回复  |  直到 6 年前
        1
  •  4
  •   ittus    6 年前

    Redshift does not support arrays ,但是有一些 JSON functions 你可以用。 基本上,您可以将数据存储为varchar并使用json函数来查询数据

    例如:

    create temporary table sales_metrics (col1 varchar(20));
    insert into sales_metrics values ('[1,2,3]');
    

    然后

    select json_extract_array_element_text(col1, 2) from sales_metrics;
     json_extract_array_element_text
    ---------------------------------
     3
    (1 row)
    
        2
  •  1
  •   Dennis    6 年前

    除了@ittus的答案,注意Redshift对数组的存储方式很挑剔。

             json_arrays          | is_valid_json_array
    ------------------------------+---------------------
     []                           | t
     ["a","b"]                    | t
     ["a",["b",1,["c",2,3,null]]] | t
     {"a":1}                      | f
     a                            | f
     {foo, bar}                   | f
     {"one", "two"}               | f
     [x,y,z]                      | f
     [1,2,]                       | f