当前位置:网站首页>spark sql 生成 json
spark sql 生成 json
2022-06-28 15:00:00 【shengjk1】
背景
想通过 spark sql 查询 hive 表然后将相应的字段组装成 sql,类似于 json_object ,不过可惜的是 spark 3.1.x 并没有 json_object 函数,不过还好spark sql 有 to_json 函数
例子:
SELECT to_json(struct(bis_type,year,douban_rating))
from tv.test where date=20220620
limit 10
SELECT to_json(named_struct('bis_type',bis_type,'year',year,'douban_rating',douban_rating))
from tv.test where date=20220620
limit 10
结果,可以保留数据原类型。这是最重要的一个点,组装 json,得保留原字段的数据类型
{
"bis_type":"xxx","year":2022,"douban_rating":8.3}
如何生成嵌套式的 json
SELECT to_json(array(named_struct('ruleType', 'datetime', 'ruleDetail', array(map('columnName', 'a1','function','test'),map('columnName', 'a2','function','test')))));
结果
[
{
"ruleType": "datetime",
"ruleDetail": [
{
"columnName": "a1",
"function": "test"
},
{
"columnName": "a2",
"function": "test"
}
]
}
]
边栏推荐
- open3d里pointcloud和numpy数组之间的转化
- Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan
- Send2vec tutorial
- 字节跳动埋点数据流建设与治理实践
- Recommendation letter brain correspondent: if love is just a chemical reaction, can you still believe in love?
- Does Frankfurt currently support SQL?
- Four visualization tools are recommended to solve 99% of large screen visualization projects!
- 币圈大地震:去年赚100万,今年亏500万
- Leetcode 705. Design hash collection
- 证券公司和银行哪个更安全 怎么办理开户最安全
猜你喜欢
With 120billion yuan, she will ring the bell for IPO again
抽奖动画 - 鲤鱼跳龙门
Practice of constructing ten billion relationship knowledge map based on Nebula graph
腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元
[Zhongyi Xinsheng] 5 SPI interface test TF Card
老板嘱咐了三遍:低调、低调、低调
Opengauss kernel: analysis of SQL parsing process
张同学还没学会当主播
After nearly 20 years of operation, the Mars probe software based on win 98 has been upgraded for the first time
3. caller service call - dapr
随机推荐
With 120billion yuan, she will ring the bell for IPO again
After nearly 20 years of operation, the Mars probe software based on win 98 has been upgraded for the first time
安杰思医学冲刺科创板:年营收3亿 拟募资7.7亿
Q-tester 3.2: applicable to development, production and after-sales diagnostic test software
[C language] implementation of binary tree and three Traversals
Leetcode(665)——非递减数列
Does Frankfurt currently support SQL?
Send2vec tutorial
2022 recurrent training question bank and online simulation examination for safety inspection of metal and nonmetal mines (underground mines)
法兰克福地区目前支持sql了吗?
5000倍回报,南非报业投资腾讯赚了一个省
The boss told me three times: low key, low key, low key
Spacetutorial (continuous updating...)
Express模板引擎
老板囑咐了三遍:低調、低調、低調
Conversion between pointcloud and numpy arrays in open3d
[collection of excellent articles on Digital IC] nearly 500 articles | learning route | basic knowledge | interface | bus | scripting language | chip job search | security | EDA | tools | low power de
【mysql学习笔记23】索引优化
字节跳动埋点数据流建设与治理实践
How can I get the stock account opening discount link? Is it safe to open a mobile account?