3 schema_of_json 提供生成json格式的方法
4 explode 把JSONArray转为多行
get_json_object
select get_json_object('{"drink":"Tea","fruit":"Apple"}', '$.drink') as text
text |
---|
Tea |
from_json
select from_json('[{"text":"Tea"},{"text":"Apple"}]', 'ARRAY<STRUCT<text: STRING>>');
jsontostructs([{"text":"Tea"},{"text":"Apple"}]) |
---|
[{"text":"Tea"},{"text":"Apple"}] |
select from_json('[{"text":"Tea"},{"text":"Apple"}]', 'ARRAY<STRUCT<text: STRING>>')[0]['text'] as q;
q |
---|
tea |
schema_of_json
select schema_of_json('[{"text":"a"},{"b":"a"}, {"c":1}]')
结果:
ARRAY<STRUCT<`b`: STRING, `c`: BIGINT, `text`: STRING>>
select schema_of_json('{"vid":391420754,"aid":9701587,"isreply":"0","ispic":"0","fromPage":"2","site":"2","isreplysp":"2","isat":"0","isemoji":"0","loc":"1","page":"1","track_info":""}')
结果:
struct<aid:bigint,fromPage:string,isat:string,isemoji:string,ispic:string,isreply:string,isreplysp:string,loc:string,page:string,site:string,track_info:null,vid:bigint>
select schema_of_json('{"text":["a", 2, "b", "a"],"b":1.2, "c":1}')
结果:
STRUCT<`b`: DOUBLE, `c`: BIGINT, `text`: ARRAY<STRING>>
select from_json('{"vid":391420754,"aid":9701587,"page":"1"}' , 'struct<aid:bigint>')
结果:
{"aid":9701587}
select from_json('{"vid":391420754,"aid":9701587,"page":"1"}' , 'struct<aid:bigint>')['aid']
结果:
aid
9701587
explode
例子:
select explode(from_json('[{"text":"Tea"},{"text":"Apple"}]', 'ARRAY<STRUCT<text: STRING>>')) as q;
q
----
{"text":"Tea"}
{"text":"Apple"}
SELECT r1.col.Attr_INT, r1.col.ATTR_DATE, r1.col.ATTR_DOUBLE FROM (SELECT explode(r.json) AS col FROM (SELECT from_json('[{"Attr_INT":1, "ATTR_DOUBLE":10.201, "ATTR_DATE": "2021-01-01"},{"Attr_INT":1, "ATTR_DOUBLE":10.201, "ATTR_DATE": "2021-02-01"}]','array<struct<ATTR_DATE:string,ATTR_DOUBLE:double,Attr_INT:bigint>>') AS json) r) AS r1;
Attr_INT ATTR_DATE ATTR_DOUBLE
1 2021-01-01 10.201
1 2021-02-01 10.201
select q.text from (select explode(from_json('[{"text":"Tea"},{"text":"Apple"}]', 'ARRAY<STRUCT<text: STRING>>')) as q);
text
----
Tea
Apple
参考文章来源:https://www.toymoban.com/news/detail-405037.html
spark-sql处理json字符串的函数 - 知乎文章来源地址https://www.toymoban.com/news/detail-405037.html
到了这里,关于spark-sql处理json字符串的常用函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!