项目场景:
maxcompute
问题描述
mongdb数据同步到maxcompute时 出现一个字段 存储的是 对象数组型字符串,此时想 将对象中的某个字段做统计
解决方案:
类似这种字符串
-- '[{"addOnDealId":0,"amount":"2"}, {"addOnDealId":0,"amount":"2"}, {"addOnDealId":0,"amount":"2"}]'
此时需要提取amount字段做求和
select array_reduce(
from_json('[{"addOnDealId":0,"amount":"3"}, {"addOnDealId":0,"amount":"3"}, {"addOnDealId":0,"amount":"2"}]', 'array<Struct<amount:string>>'), 0,
(buf, e)->buf + CAST(e.amount as int), buf->buf);
array_reduct 将数组聚合
from_json 将json类型字符串 进行格式转换
cast 类型转换
(小白,有更好的方法麻烦教教,哈,目前想到的最好方法了)
参考:https://help.aliyun.com/document_detail/48969.html
在将MongoDB中的数据同步到MaxCompute时,如果遇到字段存储的是对象数组字符串,可以使用`array_reduce`、`from_json`和`CAST`函数进行处理。例如,要提取并统计amount字段,可以使用以下SQL:`select array_reduce(from_json(json_string, 'array<Struct<amount:string>>'), 0, (buf, e) -> buf + CAST(e.amount as int), buf -> buf)`。这种方法将字符串转换为结构化数组,然后对amount字段求和。如果你有更高效的方法,欢迎分享。

被折叠的 条评论
为什么被折叠?



