项目场景:
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