无法将varchar转换为Presto Athena中的数组 [英] Unable to convert varchar to array in Presto Athena

查看:903
本文介绍了无法将varchar转换为Presto Athena中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据是varchar格式.我想拆分此数组的两个元素,以便随后可以从json中提取键值.

My data is in varchar format. I want to split both the elements of this array so that I can then extract a key value from the json.

Data format:

[
  {
    "skuId": "5bc87ae20d298a283c297ca1",
    "unitPrice": 0,
    "id": "5bc87ae20d298a283c297ca1",
    "quantity": "1"
  },

{
    "skuId": "182784738484wefhdchs4848",
    "unitPrice": 50,
    "id": "5bc87ae20d298a283c297ca1",
    "quantity": "4"
  },
]

例如我想从上面的列中提取臭味. 因此,提取后的数据应如下所示:

For e.g. I want to extract skuid from the above column. So my data after extraction should look like:

1 5bc87ae20d298a283c297ca1
2 182784738484wefhdchs4848

投射到数组不起作用 例如select cast(col as array)给出以下错误: 未知类型:数组

Cast to array doesn't work e.g select cast(col as array) gives the following error: Unknown type: array

所以我无法取消嵌套该数组的操作.

So I am not able to unnest the array.

我该如何解决雅典娜的这个问题?

How do I do solve this problem in Athena?

推荐答案

您可以结合使用将值解析为JSON ,将其转换为结构化的SQL类型(数组/地图/行),然后

You can use a combination of parsing the value as JSON, casting it to a structured SQL type (array/map/row), and UNNEST WITH ORDINALITY to extract the elements from the array as separate rows. Note that this only works if the array elements in the JSON payload don't have a trailing commas. Your example has one but it is removed from the example below.

WITH data(value) AS (VALUES
 '[
    {
      "skuId": "5bc87ae20d298a283c297ca1",
      "unitPrice": 0,
      "id": "5bc87ae20d298a283c297ca1",
      "quantity": "1"
    },
    {
      "skuId": "182784738484wefhdchs4848",
      "unitPrice": 50,
      "id": "5bc87ae20d298a283c297ca1",
      "quantity": "4"
    }
  ]'
),
parsed(entries) AS (
  SELECT cast(json_parse(value) AS array(row(skuId varchar)))
  FROM data
)
SELECT ordinal, skuId
FROM parsed, UNNEST(entries) WITH ORDINALITY t(skuId, ordinal)

产生:

 ordinal |          skuId
---------+--------------------------
       1 | 5bc87ae20d298a283c297ca1
       2 | 182784738484wefhdchs4848
(2 rows)

这篇关于无法将varchar转换为Presto Athena中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆