查询雅典娜时将结构转换为json [英] converting a struct to a json when querying athena

查看:97
本文介绍了查询雅典娜时将结构转换为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个雅典娜表,我没有创建或管理它,但可以查询。字段之一是结构类型。为了便于说明,我们假设它看起来像这样:

  my_field struct< a:string,
b:string ,
c:struct< d:string,e:string>
>

现在,我知道如何查询此结构中的特定字段。但是在我的查询之一中,我需要提取完整的结构。所以我只用:

 从my_table 


,结果看起来像一个字符串:

  {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 |


解决方案

您可以直接引用带有<$ c的嵌套字段$ c> parent_field.child_field 表示法。尝试:

  SELECT 
my_field,
my_field.a,
my_field.b,
my_field.cd,
my_field.ce
FROM
my_table


I have an athena table which I did not create or manage, but can query. one of the fields is a struct type. for the sake of the example let's suppose it looks like this:

my_field struct<a:string,
                b:string,
                c:struct<d:string,e:string>
                >

Now, I know how to query specific fields within this struct. But in one of my queries I need to extract the complete struct. so I just use:

select my_field from my_table

and the result looks like a string:

{a=aaa, b=bbb, c={d=ddd, e=eee}}

I want to get the result as a json string:

{"a":"aaa", "b":"bbb","c":{"d":"ddd", "e":"eee"}}

this string will then be processed by another application, this is why i need it in json format.

How can I achieve this?

EDIT: Better still, is there a way to query the struct in a way that flattens it? so the result would look like:

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

解决方案

You can directly reference nested fields with a parent_field.child_field notation. Try:

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

这篇关于查询雅典娜时将结构转换为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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