遇到了需要解析json数据的情况, 刚好有kettle,所以使用kettle进行json数据解析尝试
1. json数据采集
json的获取问题, kettle可以通过RESR Client按照给的url地址来获取json数据, 但是我不太清楚是我的kettle版本问题还是url不支持kettle来获取数据, 使用REST Client无法获取到数据
我采用的是直接在linux执行下方脚本每次10条数据循环获取
#!/bin/bash
dataDir=/data/ykbData
DATE=`date +%Y%m%d`
fileDir=$dataDir/test.json
beginNum=0
totalNum=100
#循环获取
while [[ $beginNum -lt $totalNum ]]
do
startnum=$beginNum
#这里填url地址
addr="****************"
curl --location --request GET $addr >> $fileDir
beginNum=$(expr $startnum + 10)
done
这里的curl命令可以在postman中获取
2.在windows上演示使用kettle 的json input模块完成json数据解析
我的json数据结构如下:
红色框代表我想要提取出来的数据, 其他的都可以类推的
现在开始
首先打开kettle ->新建 ->装换
然后在核心对象找到一下组件, 下方便是这次解析所需要用到的组件, 最后我是以文本输出的, 当然按照需求也可以换成表输出等等此处只做示范:
- 获取变量
里面的变量值是json文件放的绝对路径
- JSON input
按上图进行勾选, 并且在从字段获取源里面的填上上一步获取变量中设置的变量名称,我这里是filename, 接下来点击字段这个模块,
我这点击select fields没法显示出对象, 路径只好手写
根据我给出的json数据结构, 路径如下
$.items..form.title
$.items..form.details
$ 代表的是当前传入整个数据的根对象
.小数点一个,代表是你想要的是基于当前对象{}下的字段名
..小数点两个,代表你想要的是基于当前对象[{}]下的字段名
简而言之, 遇到{}使用一个. 遇到[]使用两个. 如果还想仔细研究可以看jsonPath文档
这样就可以获取title的值, 以及details这个[{...}]格式的值,为了获取最后我们需要的值 还需要对details进一步解析
可能有人会说, 为什么不在一个json input组件就直接把我们想要的值都拿出来?
这个是不行的, kettle不支持同一个json input拉取不同级别路径的字段出来, 会报错,报错信息如下, 所以我们需要多次解析
0:47 - JSON input.0 - ERROR (version 8.3.0.0-371, build 8.3.0.0-371 from 2019-06-11 11.09.08 by buildguy) :
2023/05/12 14:30:47 - JSON input.0 - The data structure is not the same inside the resource! We found 1 values for json path [$.count], which is different that the number returned for path [$.items..form.details] (10 values). We MUST have the same number of values for all paths.
2023/05/12 14:30:47 - JSON input.0 - ERROR (version 8.3.0.0-371, build 8.3.0.0-371 from 2019-06-11 11.09.08 by buildguy) : org.pentaho.di.trans.steps.jsoninput.exception.JsonInputException:
2023/05/12 14:30:47 - JSON input.0 - The data structure is not the same inside the resource! We found 1 values for json path [$.count], which is different that the number returned for path [$.items..form.details] (10 values). We MUST have the same number of values for all paths.
2023/05/12 14:30:47 - JSON input.0 -
2023/05/12 14:30:47 - JSON input.0 - at org.pentaho.di.trans.steps.jsoninput.reader.FastJsonReader.evalCombinedResult(FastJsonReader.java:248)
2023/05/12 14:30:47 - JSON input.0 - at org.pentaho.di.trans.steps.jsoninput.reader.FastJsonReader.parse(FastJsonReader.java:166)
2023/05/12 14:30:47 - JSON input.0 - at org.pentaho.di.trans.steps.jsoninput.JsonInput.parseNextInputToRowSet(JsonInput.java:269)
2023/05/12 14:30:47 - JSON input.0 - at org.pentaho.di.trans.steps.jsoninput.JsonInput.getOneOutputRow(JsonInput.java:340)
2023/05/12 14:30:47 - JSON input.0 - at org.pentaho.di.trans.steps.jsoninput.JsonInput.processRow(JsonInput.java:110)
2023/05/12 14:30:47 - JSON input.0 - at org.pentaho.di.trans.step.RunThread.run(RunThread.java:62)
2023/05/12 14:30:47 - JSON input.0 - at java.lang.Thread.run(Thread.java:750)
3.Json input2
与上方同理哈, 注意这次源是一个文件名这个框就不需要勾选了, 应该这个时候传进来的是details这个值, 是[{}]格式的String
4.Json input3
5.文本文件输出
现在为止, 我们已经将想要的所有字段值都拿到了, 准备以文本格式输出到指定路径,
先按下图添加你所想要保存的路径, 以及文件名
接下来点击字段模块, 点击获取字段, 删除不需要的字段, 只留下我们所需要的字段名即可
6.运行
点击运行, 再去保存路径打开文本文件查看数据
至此, 使用kettle解析json文件数据已完成