aws athena - 转换为 json 不返回 json 对象

Posted

技术标签:

【中文标题】aws athena - 转换为 json 不返回 json 对象【英文标题】:aws athena - cast as json don't return json object 【发布时间】:2018-12-09 21:11:37 【问题描述】:

我有一个 json 对象列表(结果属性),如示例中所示:

    select result   from mytable limit 1

我明白了:

[hop=1, error=null, result=[x=null, from=192.168.0.1, rtt=0.378, ttl=64, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=192.168.0.1, rtt=0.314, ttl=64, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=192.168.0.1, rtt=0.303, ttl=64, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=2, error=null, result=[x=null, from=71.120.7.1, rtt=8.135, ttl=254, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=71.120.7.1, rtt=0.769, ttl=254, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=71.120.7.1, rtt=0.761, ttl=254, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=3, error=null, result=[x=null, from=100.41.136.160, rtt=5.583, ttl=253, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=100.41.136.160, rtt=1.749, ttl=253, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=100.41.136.160, rtt=1.805, ttl=253, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=4, error=null, result=[x=*, from=null, rtt=null, ttl=null, err=null, ittl=null, edst=null, late=null, mtu=null, size=null, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=*, from=null, rtt=null, ttl=null, err=null, ittl=null, edst=null, late=null, mtu=null, size=null, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=*, from=null, rtt=null, ttl=null, err=null, ittl=null, edst=null, late=null, mtu=null, size=null, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=5, error=null, result=[x=null, from=140.222.239.91, rtt=3.121, ttl=251, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=140.222.239.91, rtt=2.984, ttl=251, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=140.222.239.91, rtt=3.0, ttl=251, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=6, error=null, result=[x=*, from=null, rtt=null, ttl=null, err=null, ittl=null, edst=null, late=null, mtu=null, size=null, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=*, from=null, rtt=null, ttl=null, err=null, ittl=null, edst=null, late=null, mtu=null, size=null, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=*, from=null, rtt=null, ttl=null, err=null, ittl=null, edst=null, late=null, mtu=null, size=null, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=7, error=null, result=[x=null, from=4.69.153.150, rtt=100.718, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=4.69.153.150, rtt=100.748, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=4.69.153.150, rtt=100.708, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=8, error=null, result=[x=null, from=212.73.203.18, rtt=101.694, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=212.73.203.18, rtt=101.954, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=212.73.203.18, rtt=101.721, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=9, error=null, result=[x=null, from=193.171.23.41, rtt=101.096, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=193.171.23.41, rtt=101.132, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=193.171.23.41, rtt=101.115, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=10, error=null, result=[x=null, from=193.170.114.242, rtt=101.884, ttl=56, err=null, ittl=null, edst=null, late=null, mtu=null, size=40, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=193.170.114.242, rtt=101.695, ttl=56, err=null, ittl=null, edst=null, late=null, mtu=null, size=40, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=193.170.114.242, rtt=100.881, ttl=56, err=null, ittl=null, edst=null, late=null, mtu=null, size=40, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null]]

我尝试将此属性作为 json 对象(不带“=”),因此我可以在我的应用程序中使用仅采用 json 数据的结果。

我尝试了函数 cast (myattribute as json)

select cast (result as json)  from mytable limit 1

但是,我明白了:

[[1,null,[[null,"192.168.0.1",0.378,64,null,null,null,null,null,null,68,null,null,null,null],[null,"192.168.0.1",0.314,64,null,null,null,null,null,null,68,null,null,null,null],[null,"192.168.0.1",0.303,64,null,null,null,null,null,null,68,null,null,null,null]]],[2,null,[[null,"71.120.7.1",8.135,254,null,null,null,null,null,null,28,null,null,null,null],[null,"71.120.7.1",0.769,254,null,null,null,null,null,null,28,null,null,null,null],[null,"71.120.7.1",0.761,254,null,null,null,null,null,null,28,null,null,null,null]]],[3,null,[[null,"100.41.136.160",5.583,253,null,null,null,null,null,null,68,null,null,null,null],[null,"100.41.136.160",1.749,253,null,null,null,null,null,null,68,null,null,null,null],[null,"100.41.136.160",1.805,253,null,null,null,null,null,null,68,null,null,null,null]]],[4,null,[["*",null,null,null,"*",null,null,null,null,null,null,null,null,null,null],["*",null,null,null,"*",null,null,null,null,null,null,null,null,null,null],["*",null,null,null,"*",null,null,null,null,null,null,null,null,null,null]]],[5,null,[[null,"140.222.239.91",3.121,251,null,null,null,null,null,null,28,null,null,null,null],[null,"140.222.239.91",2.984,251,null,null,null,null,null,null,28,null,null,null,null],[null,"140.222.239.91",3.0,251,null,null,null,null,null,null,28,null,null,null,null]]],[6,null,[["*",null,null,null,"*",null,null,null,null,null,null,null,null,null,null],["*",null,null,null,"*",null,null,null,null,null,null,null,null,null,null],["*",null,null,null,"*",null,null,null,null,null,null,null,null,null,null]]],[7,null,[[null,"4.69.153.150",100.718,244,null,null,null,null,null,null,28,null,null,null,null],[null,"4.69.153.150",100.748,244,null,null,null,null,null,null,28,null,null,null,null],[null,"4.69.153.150",100.708,244,null,null,null,null,null,null,28,null,null,null,null]]],[8,null,[[null,"212.73.203.18",101.694,244,null,null,null,null,null,null,68,null,null,null,null],[null,"212.73.203.18",101.954,244,null,null,null,null,null,null,68,null,null,null,null],[null,"212.73.203.18",101.721,244,null,null,null,null,null,null,68,null,null,null,null]]],[9,null,[[null,"193.171.23.41",101.096,244,null,null,null,null,null,null,28,null,null,null,null],[null,"193.171.23.41",101.132,244,null,null,null,null,null,null,28,null,null,null,null],[null,"193.171.23.41",101.115,244,null,null,null,null,null,null,28,null,null,null,null]]],[10,null,[[null,"193.170.114.242",101.884,56,null,null,null,null,null,null,40,null,null,null,null],[null,"193.170.114.242",101.695,56,null,null,null,null,null,null,40,null,null,null,null],[null,"193.170.114.242",100.881,56,null,null,null,null,null,null,40,null,null,null,null]]]]

如您所见,键被省略了! 有什么帮助吗?

【问题讨论】:

您能分享一下您的表格定义吗? @Hayat 你解决了吗?我也有同样的问题。 @Hayat,select typeof(result) from mytable limit 1 返回什么?它不是 JSON,所以让我们弄清楚它是什么。 @PiotrFindeisen,如果下面的行不是 JSON,你们是否也想出了一种获取密钥的方法。我有类似的问题 [这里] (***.com/questions/64111630/…) @Ram 不确定我是否理解这个问题,但如果您想影响项目路线图,请提交并发布github.com/prestosql/presto/issues/new 或加入对话prestosql.io/slack.html 【参考方案1】:

这是因为它存储为 ROW 类型字段而不是 JSON。您可以通过以下方式访问不同的密钥

select field.key from table

您可以在here查看更多信息

【讨论】:

【参考方案2】:

你不能转换 varchar 值

[hop=1, error=null, result=[x=null, from=192.168.0.1, rtt=0.378, ttl=64, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=192.168.0.1, rtt=0.314, ttl=64, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=192.168.0.1, rtt=0.303, ttl=64, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=2, error=null, result=[x=null, from=71.120.7.1, rtt=8.135, ttl=254, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=71.120.7.1, rtt=0.769, ttl=254, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=71.120.7.1, rtt=0.761, ttl=254, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=3, error=null, result=[x=null, from=100.41.136.160, rtt=5.583, ttl=253, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=100.41.136.160, rtt=1.749, ttl=253, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=100.41.136.160, rtt=1.805, ttl=253, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=4, error=null, result=[x=*, from=null, rtt=null, ttl=null, err=null, ittl=null, edst=null, late=null, mtu=null, size=null, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=*, from=null, rtt=null, ttl=null, err=null, ittl=null, edst=null, late=null, mtu=null, size=null, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=*, from=null, rtt=null, ttl=null, err=null, ittl=null, edst=null, late=null, mtu=null, size=null, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=5, error=null, result=[x=null, from=140.222.239.91, rtt=3.121, ttl=251, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=140.222.239.91, rtt=2.984, ttl=251, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=140.222.239.91, rtt=3.0, ttl=251, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=6, error=null, result=[x=*, from=null, rtt=null, ttl=null, err=null, ittl=null, edst=null, late=null, mtu=null, size=null, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=*, from=null, rtt=null, ttl=null, err=null, ittl=null, edst=null, late=null, mtu=null, size=null, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=*, from=null, rtt=null, ttl=null, err=null, ittl=null, edst=null, late=null, mtu=null, size=null, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=7, error=null, result=[x=null, from=4.69.153.150, rtt=100.718, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=4.69.153.150, rtt=100.748, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=4.69.153.150, rtt=100.708, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=8, error=null, result=[x=null, from=212.73.203.18, rtt=101.694, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=212.73.203.18, rtt=101.954, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=212.73.203.18, rtt=101.721, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=68, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=9, error=null, result=[x=null, from=193.171.23.41, rtt=101.096, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=193.171.23.41, rtt=101.132, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=193.171.23.41, rtt=101.115, ttl=244, err=null, ittl=null, edst=null, late=null, mtu=null, size=28, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null], hop=10, error=null, result=[x=null, from=193.170.114.242, rtt=101.884, ttl=56, err=null, ittl=null, edst=null, late=null, mtu=null, size=40, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=193.170.114.242, rtt=101.695, ttl=56, err=null, ittl=null, edst=null, late=null, mtu=null, size=40, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null, x=null, from=193.170.114.242, rtt=100.881, ttl=56, err=null, ittl=null, edst=null, late=null, mtu=null, size=40, flags=null, dstoptsize=null, hbhoptsize=null, icmpext=null]]

到 JSON,因为它不是格式良好的 JSON 表示。例如。键必须用引号括起来,值用冒号分隔,如下所示:"hop":1, "error": null, "result": ...

【讨论】:

我认为在问题中,result 是有效的 json,但发布的是 Athena 格式化输出的方式。应该可以解析为json。 可能是。 @LuigiPlinge 让我们澄清一下。

以上是关于aws athena - 转换为 json 不返回 json 对象的主要内容,如果未能解决你的问题,请参考以下文章

AWS Athena 无法将 .csv 整数转换为表值

如何将我的 Bigquery 查询转换为 AWS Athena 查询?

如何在 AWS Athena 中成功将字符串转换为日期类型?

在 AWS Athena 中查询第一个非空值的动态 JSON 字段

AWS Athena 将结构数组导出到 JSON

AWS Athena 使用填充错误数据的创建表从 Epoch 转换为时间戳