【问题标题】:Apache Camel - JsonPath & unique file namesApache Camel - JsonPath 和唯一的文件名
【发布时间】:2014-04-30 20:18:39
【问题描述】:

骆驼 2.13.0

我正在尝试使用包含多条记录的 json 字符串并为每条记录生成一个输出文件。

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");
    }
}

示例输入:

“这是字符串 #1。这是字符串 #2。”

如果我按原样运行上面的代码,我会得到一个包含“This is string #2.”的输出文件。如果我删除 .split() 调用,我会按预期得到一个包含 json 的输出文件:

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

如何实现代表两行数据的两个输出文件?

我突然想到,也许拆分工作正常,第二个输出文件覆盖了第一个。根据文档,如果未设置 CamelFileName,则默认行为是创建一个唯一的生成 ID,但我没有遇到这种情况。在我的情况下,输出文件名始终与输入文件名匹配。

如何在每个文件夹中获取唯一的文件名?

谢谢!

【问题讨论】:

    标签: apache-camel


    【解决方案1】:

    终于偶然发现了正确的搜索词,并发现了以下有用的帖子:Camel: Splitting a collection and writing to files

    诀窍是在 .to() 中使用一些逻辑来实现唯一的输出文件名:

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

    JsonPathExpression 的工作原理就像一个魅力,不需要 Processor() 或 unmarshal(),就像我之前尝试过的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 2015-05-06
      相关资源
      最近更新 更多