问题
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