当前位置:网站首页>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"
}
]
}
]
边栏推荐
- Rails advanced -- framework theory cognition and construction scheme construction (I)
- 优巨新材冲刺深交所:拟募资6.5亿 年营收3.33亿
- Vector explanation + topic
- Force deduction solution summary 522- longest special sequence II
- 【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
- Unable to create process using 'd:\program file
- QQ被盗号后群发黄图,大批用户“社死”
- 法兰克福地区目前支持sql了吗?
- With 120billion yuan, she will ring the bell for IPO again
- Flutter series: offstage in flutter
猜你喜欢

Successful cases of rights protection of open source projects: successful rights protection of SPuG open source operation and maintenance platform

字节跳动埋点数据流建设与治理实践

3. Caller 服务调用 - dapr

The time schedule for the soft test in the second half of 2022 has been determined!

Leetcode 48. Rotate image (yes, resolved)

动力电池,是这样被“瓜分”的

Dry goods | how to calculate the KPI of scientific researchers, and what are the h index and G index

PMP真的有用吗?

Introduction to common components of IOT low code platform

Vscode writes markdown file and generates pdf
随机推荐
Mingchuangyou products passed the listing hearing: seeking dual main listing with an annual revenue of 9.1 billion
智能化转型被加速,企业需要新的工具箱
运行近20年,基于Win 98的火星探测器软件迎来首次升级
腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元
安杰思医学冲刺科创板:年营收3亿 拟募资7.7亿
快手投资电商服务商易心优选
证券公司和银行哪个更安全 怎么办理开户最安全
Which securities company is the largest and safest? How to open an account is the safest
Leetcode (167) -- sum of two numbers II - input ordered array
竞远安全冲刺创业板:拟募资4亿 启元投资与云游软件是股东
Recommendation letter brain correspondent: if love is just a chemical reaction, can you still believe in love?
seata-server 1.5.0 如何支持mysql8.0?
Seata数据库中出现以下问题要怎么解决啊?
Four visualization tools are recommended to solve 99% of large screen visualization projects!
Could you tell me whether the batch addition of Oracle such as insert all was not blocked?
How to solve the following problems in the Seata database?
动力电池,是这样被“瓜分”的
Leetcode 48. Rotate image (yes, resolved)
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
优巨新材冲刺深交所:拟募资6.5亿 年营收3.33亿