camel how to process json with streaming mode?

℡╲_俬逩灬. 提交于 2019-12-11 01:14:09

问题


In order to read json and unmarshal in streaming mode what options are available in Camel OOB ? If not out of box how can this be implemented ?

I found camel-xstream which might help. Is streaming mode a default of this or do we need to do something else to make it read in streaming mode ?

There is also jackson streaming api. Is that accessible with camel ?

Some examples would help. Thanks


回答1:


You can use JSON data format, to to marshal and unmarshal Java objects to and from JSON.

Some thing like .marshal().json(JsonLibrary.Jackson) which uses Jackson Library, You can use others as well.

Following is a sample configuration to stream csv file content and convert to JSON and then call publish method of MyBean class.

  CsvDataFormat csv = new CsvDataFormat();
  csv.setDelimiter(","); 
  csv.setQuoteDisabled(true);
  csv.setUseMaps(true);
  csv.setLazyLoad(true);
  csv.setHeader(Arrays.asList("head1","head2","head3","message"));

from("file:///test/?fileName=test.csv&noop=true").split(body().tokenize("\n")).streaming().unmarshal(csv).marshal().json(JsonLibrary.Jackson).bean(MyBean.class,"publish").log("done.").end();



回答2:


You can use the unmarshal with json format. like this:

.unmarshal().json(JsonLibrary.Jackson, YourClass.class)

or

.unmarshal().json(JsonLibrary.Jackson)

In the first example you can take it in your Pojo, for manipulate the datas in a process like this:

.process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                           YourClass yourClass = exchange.getIn().getBody(YourClass.class);
})


来源:https://stackoverflow.com/questions/44618227/camel-how-to-process-json-with-streaming-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!