1.pom.xml加入依赖:
<!-- https://mvnrepository.com/artifact/org.kitesdk/kite-morphlines-json -->
<dependency>
<groupId>org.kitesdk</groupId>
<artifactId>kite-morphlines-json</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.kitesdk</groupId>
<artifactId>kite-morphlines-core</artifactId>
<version>1.1.0</version>
</dependency>
2.test方法
public static void main(String[] args) {
try {
MorphlineContext morphlineContext =new MorphlineContext.Builder().build();
Command morphing = new Compiler().compile(new File("D:\\workspace\\morphlinesTest\\src\\main\\resources\\jsonParser.conf"), null, morphlineContext, null);;
String[] inputs = new String[]{"D:\\workspace\\morphlinesTest\\src\\main\\resources\\data\\tweets.json"};
boolean[] outcome = new boolean[inputs.length];
// Process each input data file
Notifications.notifyBeginTransaction(morphing);
Record record = new Record();
for (int i = 0; i < inputs.length; i++) {
InputStream in = new BufferedInputStream(new FileInputStream(new File(inputs[i])));
record.put(Fields.ATTACHMENT_BODY, in);
Notifications.notifyStartSession(morphing);
outcome[i] = morphing.process(record);
if (outcome[i] == false) {
System.out.println("Morphline failed to process record: " + record + "for file " + inputs[i]);
}
in.close();
}
Notifications.notifyShutdown(morphing);
System.out.println("===========>"+outcome[0]);
} catch (IOException e) {
e.printStackTrace();
}
}
3.conf
morphlines : [{
id : json2string
importCommands : ["org.kitesdk.**"]
commands : [
# read the JSON blob
{ readJson: {} }
# extract JSON objects into head fields
{ extractJsonPaths {
flatten: true
paths: {
confidences : "/objects[]/confidence"
ids: "/objects[]/id"
types : "/objects[]/observables[]/type"
values : "/objects[]/observables[]/value"
}
} }
# java
{
java {
code: """
ListMultimap<String, Object> fields = record.getFields();
Map<String, Collection<Object>> stringCollectionMap = fields.asMap();
System.out.println("==record==>"+stringCollectionMap);
Collection<Object> types = stringCollectionMap.get("values");
Iterator<Object> iterator = types.iterator();
while (iterator.hasNext()){
System.out.println("==单个value==>"+iterator.next());
}
return true;
"""
}
}
]
}]
4.json
{
"count": 2,
"objects": [{
"confidence": 55,
"observables": [{
"type": "domian",
"value": "www.baidu.com"
}],
"id": "dddddd"
}, {
"confidence": 77,
"observables": [{
"type": "domian",
"value": "www.taobao.com"
}],
"id": "aaaaaaaaaaaa"
}]
}
5.result: