0

Camel 2.13.0

I am attempting to consume a json string containing multiple records and produce an output file for each record.

public void configure() {
    from("file:data/input")
        // my bean splits the input string and returns a json string via Gson
        .bean(new com.camel.Tokenizer(), "getSentences")
        .split(new JsonPathExpression("$[*]"))
        .convertBodyTo(String.class)
        .to("file:data/output");
    }
}

Sample input:

"This is string #1. This is string #2."

If I run the code above as-is I get a single output file containing "This is string #2.". If I remove the .split() call I get a single output file containing json as expected:

[
    {
        "token": "This is string #1."
    }, 
    {
        "token": "This is string #2."
    }
]

How can I achieve two output files representing both lines of data?

It occurred to me that perhaps the split was working correctly and the second output file was overwriting the first. According to the documentation, the default behavior if CamelFileName is not set is to create a unique generated ID but I do not experience this. In my case the output file name always matches the input file name.

How can I get unique file name within each folder?

Thanks!

1 Answer 1

2

Finally stumbled upon the proper search terms and came across the following helpful post: Camel: Splitting a collection and writing to files

The trick is to use a bit of logic in the .to() to achieve unique output file names:

.to("file:data/sentence_q?fileName=${header.CamelSplitIndex}.txt");

The JsonPathExpression works like a charm and no need for a Processor() or unmarshal(), as I'd tried previously.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.