Sql 查询athena时将结构转换为json

Sql 查询athena时将结构转换为json,sql,json,struct,amazon-athena,Sql,Json,Struct,Amazon Athena,我有一个雅典娜表,我没有创建或管理,但可以查询。其中一个字段是结构类型。为了便于示例,我们假设它如下所示: my_field struct<a:string, b:string, c:struct<d:string,e:string> > 结果看起来像一个字符串: {a=aaa, b=bbb, c={d=ddd, e=eee}} 我希望以json字符串的形式获取结果: {"

我有一个雅典娜表,我没有创建或管理,但可以查询。其中一个字段是结构类型。为了便于示例,我们假设它如下所示:

my_field struct<a:string,
                b:string,
                c:struct<d:string,e:string>
                >
结果看起来像一个字符串:

{a=aaa, b=bbb, c={d=ddd, e=eee}}
我希望以json字符串的形式获取结果:

{"a":"aaa", "b":"bbb","c":{"d":"ddd", "e":"eee"}}
这个字符串将被另一个应用程序处理,这就是为什么我需要json格式的字符串

我怎样才能做到这一点

编辑: 更好的是,是否有一种方法可以查询结构,使其扁平化?因此,结果如下所示:

a   |   b   |   c.d  |  c.e   |
-------------------------------
aaa |   bbb |   ddd  |  eee   |

您可以使用
父字段.子字段
符号直接引用嵌套字段。尝试:

SELECT
  my_field,
  my_field.a,
  my_field.b,
  my_field.c.d,
  my_field.c.e
FROM 
  my_table

我们可以通过后处理将athena输出的结构转换为对象。下面的脚本可能会有所帮助

假设为嵌套对象接收到样本字符串

   {description=Check the Primary key count of TXN_EVENT table in Oracle, datastore_order=1, zone=yellow, aggregation_type=count, updatedcount=0, updatedat=[2021-06-09T02:03:20.243Z]}
{
  description: 'Check the Primary key count of TXN_EVENT table in Oracle',
  datastore_order: '1',
  zone: 'yellow',
  aggregation_type: 'count',
  updatedcount: '0',
  updatedat: [ '2021-06-09T02:03:20.004Z' ]
}
可以使用此npm包athena struct parser包的帮助对其进行解析

  • Nodejs--
  • 蟒蛇--
  • 示例代码

    var parseStruct =require('athena-struct-parser') ;
    var str = '{description=Check the Primary key count of TXN_EVENT table in Oracle, datastore_order=1, zone=yellow, aggregation_type=count, updatedcount=0, updatedat=[2021-06-09T02:03:20.243Z]}'
    var parseObj = parseStruct(str)
    console.log(parseObj);
    
    结果解析输出

       {description=Check the Primary key count of TXN_EVENT table in Oracle, datastore_order=1, zone=yellow, aggregation_type=count, updatedcount=0, updatedat=[2021-06-09T02:03:20.243Z]}
    
    {
      description: 'Check the Primary key count of TXN_EVENT table in Oracle',
      datastore_order: '1',
      zone: 'yellow',
      aggregation_type: 'count',
      updatedcount: '0',
      updatedat: [ '2021-06-09T02:03:20.004Z' ]
    }
    

    我说我知道:-)问题是对一个非常大的结构进行这样的查询,特别是在你事先不知道它的结构的情况下,你只知道你想把它放平。你好@amit,你有幸在不知道它的结构的情况下把它放平了吗?你找到解决方案了吗?