morphlines中使用java
2021/11/11 11:10:07
本文主要是介绍morphlines中使用java,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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:
这篇关于morphlines中使用java的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南