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

sam*_*mba 3 postgresql dataframe amazon-redshift apache-spark apache-spark-sql

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

Column "main.sales_metrics" has unsupported type "character varying[]".;
Run Code Online (Sandbox Code Playgroud)

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

|-- sales_metrics: array (nullable = true)
     |-- element: string (nullable = true)
Run Code Online (Sandbox Code Playgroud)

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

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

itt*_*tus 7

Redshift 不支持数组,但您可以使用一些JSON 函数。基本上你可以将数据存储为 varchar 并使用 json 函数来查询数据

例如:

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

然后

select json_extract_array_element_text(col1, 2) from sales_metrics;
 json_extract_array_element_text
---------------------------------
 3
(1 row)
Run Code Online (Sandbox Code Playgroud)