【问题标题】:What is the right way to store arrays in a RedShift table?在 RedShift 表中存储数组的正确方法是什么?
【发布时间】:2019-01-11 19:54:22
【问题描述】:

我在 Redshift 中创建表时遇到以下错误:

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

在 DataFrame 模式中,它看起来像这样:

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

我试图像往常在 PostgreSQL 中那样声明该列:sales_metrics text[] 正如我从文档中读到的,Amazon Redshift 不支持 PostgreSQL 数据类型。

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

【问题讨论】:

    标签: postgresql apache-spark dataframe apache-spark-sql amazon-redshift


    【解决方案1】:

    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】:

      除了@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
      

      【讨论】:

        猜你喜欢
        • 2011-05-29
        • 1970-01-01
        • 2016-05-31
        • 2012-05-01
        • 1970-01-01
        • 2020-01-19
        • 2012-06-16
        • 2020-12-02
        • 1970-01-01
        相关资源
        最近更新 更多