概述
您可能有大量应用程序产生的JSON数据,您可能需要对这些JSON数据进行整理,去除不想要的字段,或者只保留想要的字段,或者仅仅是进行数据查询。那么,利用阿里云Data Lake Analytics或许是目前能找到的云上最为便捷的达到这一目标的服务了。仅仅需要3步,就可以完成对海量JSON数据的处理,或者更为复杂的ETL流程。
第一步:JSON数据到阿里云OSS
利用各种手段,将JSON数据投递到OSS(https://www.aliyun.com/product/oss)中。
通常,对于云上日志链路,还有一种JSON到OSS的投递链路,可以参考“云原生日志数据分析上手指南”(https://zhuanlan.zhihu.com/p/69399941)其中的JSON部分。
第二步:DLA中建表
参考上述“云原生日志数据分析上手指南”(https://zhuanlan.zhihu.com/p/69399941),其中已经有海量JSON数据的分区模式建表方法了。本例中,以非分区表为例,假设,数据文件中每一行一个JSON数据,JSON数据放置的OSS路径为:
oss://your_bucket/json_data/...
则,在DLA中执行建表:
CREATE EXTERNAL TABLE simple_json (
data STRING
)
STORED AS TEXTFILE
LOCATION 'oss://your_bucket/json_data/';
第三步:利用DLA JSON函数SQL处理
json_remove
从JSON中去除指定JSON Path的数据。可以一次处理一个JSON path,也可以一次处理多个JSON path。注意:目前还不支持“..”等JSON path的模糊匹配,不久后会支持。
json_remove(json_string, json_path_string) -> json_string
json_remove(json_string, array[json_path_string]) -> json_string
示例:
select json_remove(
'{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}'
, '$.glossary.GlossDiv') a;
-> {"glossary":{"title":"example glossary"}}
select json_remove(
'{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}'
, array['$.glossary.title', '$.glossary.GlossDiv.title']) a;
{"glossary":{"GlossDiv":{"GlossList":{"GlossEntry":{"GlossTerm":"Standard Generalized Markup Language","GlossSee":"markup","SortAs":"SGML","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"ID":"SGML","Acronym":"SGML","Abbrev":"ISO 8879:1986"}}}}}
json_reserve
从JSON中保留指定JSON Path的数据,去除其他的数据。可以一次处理一个JSON path,也可以一次处理多个JSON path。注意:目前还不支持“..”等JSON path的模糊匹配,不久后会支持。
json_reserve(json_string, json_path_string) -> json_string
json_reserve(json_string, array[json_path_string]) -> json_string
示例:
select json_reserve(
'{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}'
, array['$.glossary.title']) a;
-> {"glossary":{"title":"example glossary"}}
select json_reserve(
'{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}'
, array['$.glossary.title', '$.glossary.GlossDiv.title', '$.glossary.GlossDiv.GlossList.GlossEntry.ID']) a;
-> "glossary":{"title":"example glossary","GlossDiv":{"GlossList":{"GlossEntry":{"ID":"SGML"}},"title":"S"}}}
Data Lake Analytics首购和流量包优惠
首购用户1元10TB,流量包阶梯折扣优惠:https://et.aliyun.com/bdad/datalake
产品详情:https://www.aliyun.com/product/datalakeanalytics
云栖社区:https://yq.aliyun.com/teams/396
知乎社区:https://zhuanlan.zhihu.com/data-lake-analytics