【问题标题】:Cannot access Array elements in AWS Athena无法访问 AWS Athena 中的数组元素
【发布时间】:2020-04-18 23:06:26
【问题描述】:

我有一个 .txt 文件,其中的列带有括号中的逗号分隔字符串数组,我想在 AWS Athena/QS 中对其进行一些分析。 原始数据如下所示:

col_id    col2
1         ["string1", "string2", "string3", "string4"] 
2         ["string1", "string2"]
3         ["string1", "string2", "string3"]
...

我在 Athena 中创建了一个表,其中包含以下内容:

create external table db.xx (
    col1 string,
    col2 array<string>

ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
WITH SERDEPROPERTIES (
  'serialization.format' = '  ',
  'field.delim' = ' ',
  'collection.delim' = ','
) LOCATION 's3://xxx'
TBLPROPERTIES ("skip.header.line.count"="1");

表创建成功,列被识别为数组数据类型。

但是我无法访问数组中的元素。

从表中选择 element_at(col2,1) 返回:

string1, string2, string3, string4
string1, string2
string1, string2, string3

我也尝试从原始数据中删除 [] 和 "",但仍然得到相同的结果。

【问题讨论】:

    标签: arrays amazon-athena amazon-quicksight


    【解决方案1】:

    CSV 没有数组类型,并且可以通过多种方式对数组进行编码。不幸的是,Athena 不会自动确定您的数据以哪种方式执行它,即使您说某列的类型为 array&lt;string&gt;

    但是,有一种解决方法:使用 string 作为列类型,然后在查询时将值转换为 JSON(因为看起来您的数组是按照 JSON 编码字符串数组的方式编码的),或者使用其中一种the many JSON functions 从数组中提取值:

    这样创建表:

    create external table db.xx (
        col1 string,
        col2 string
    )
    ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
    WITH SERDEPROPERTIES (
      'serialization.format' = '  ',
      'field.delim' = ' ',
      'collection.delim' = ','
    ) LOCATION 's3://xxx'
    TBLPROPERTIES ("skip.header.line.count"="1");
    

    然后像这样查询它:

    SELECT
      col1,
      json_array_get(col2, 0)
    FROM db.xx
    

    【讨论】:

      猜你喜欢
      • 2020-11-22
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 2020-08-12
      • 1970-01-01
      • 2016-01-04
      相关资源
      最近更新 更多