如何创建structs spark sql数组

utugiqy6  于 2021-05-24  发布在  Spark
关注(0)|答案(1)|浏览(491)

我想创造一个 array<struct> 类型。我的代码似乎返回了我想要的,但是当我打开json文件时,数组只包含1个struct。

select idCol,
collect_list(
named_struct("name", "apple", "amount", named_struct("value", apple_amount, "currency", currency),
             "name", "orange", "amount", named_struct("value", orange_amount, "currency", currency),
             "name", "banana", "amount", named_struct("value", banana_amount, "currency", currency))) as categories
  from (
     select
       1 as idCol,
       'usd' as currency,
       20 as apple_amount,
       30 as orange_amount,
       40 as banana_amount)
  group by 1

退货

{
  "idCol": 1,
  "categories": [
    {
      "name": "apple",
      "amount": {
        "value": 20,
        "currency": "usd"
      },
      "name": "orange",
      "amount": {
        "value": 30,
        "currency": "usd"
      },
      "name": "banana",
      "amount": {
        "value": 40,
        "currency": "usd"
      }
    }
  ]
}

我怎样才能让它变成这样?

{
      "idCol": 1,
      "categories": [
        {
          "name": "apple",
          "amount": {
            "value": 20,
            "currency": "usd"
          }
        },
        {
          "name": "orange",
          "amount": {
            "value": 30,
            "currency": "usd"
          }
        },
        {
          "name": "banana",
          "amount": {
            "value": 40,
            "currency": "usd"
          }
        }
      ]
    }
lf5gs5x2

lf5gs5x21#

我应该有一个狭隘的问题。

select idCol,
collect_set(named_struct("name", type, "amount", named_struct("value", amount, "currency", currency))) as categories
from (
select
 1 as idCol,
 'usd' as currency,
 'apple' as type,
 20 as amount
 union all
 select 1 as idCol,
 'usd' as currency,
 'orange' as type,
 30 as amount
 union all
 select 1 as idCol,
 'usd' as currency,
 'banana' as type,
 40 as amount)
group by 1

相关问题