简体   繁体   English

如何使用附加字段丰富 csv 内容数据,并使用 apache camel 将管道作为分隔符转换为文本文件

[英]How to enrich the csv content data with additional fields and convert into text file with pipe as delimiter using apache camel

I have a csv file with input content separated with comma ",".我有一个 csv 文件,输入内容用逗号“,”分隔。 I want to convert that into text file with "|"我想将其转换为带有“|”的文本文件delimiter.分隔符。 I'm using apache camel CsvDataFormat to convert the given csv data format.I can be able to convert the csv content to pipe delimited string.我正在使用 apache camel CsvDataFormat 来转换给定的 csv 数据格式。我可以将 csv 内容转换为管道分隔的字符串。 I have two more constants which I have assigned to two variables.I would like to enrich the content of the output data with two additional fields as shown in expected output.我还有两个常量,它们已分配给两个变量。我想用两个附加字段来丰富输出数据的内容,如预期输出中所示。

Input输入

test.csv测试文件

Jack Dalton, 115, mad at Averell 
Joe Dalton, 105, calming Joe 
William Dalton, 105, keeping Joe from killing 
Averell Averell Dalton, 80, playing with Rantanplan 
Lucky Luke, 120, capturing the Daltons

Test.java测试.java

public class Test{
    private String name = "Hell0";
    private String address = "134 johen rd";
}

ConverterRoute.java转换器路由

public class ConverterRoute implements RoutesBuilder {

    private static final String SOURCE_INPUT_PATH = "file://inbox?fileName=test.csv";

    private static final String SOURCE_OUTPUT_PATH = "file://outbox?fileName=file.txt";

    public void addRoutesToCamelContext(CamelContext context) throws Exception {

        context.addRoutes(new RouteBuilder() {
            public void configure() {
                try {
                    CsvDataFormat csvDataFormat = new CsvDataFormat();
                    csvDataFormat.setDelimiter('|');
                    csvDataFormat.setHeaderDisabled(true);

                    from(SOURCE_INPUT_PATH).
                            unmarshal().csv().
                            marshal(csvDataFormat).
                            to(SOURCE_OUTPUT_PATH)
                            .end();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

Output输出

file.txt文件.txt

Jack Dalton| 115 | mad at Averell 
Joe Dalton| 105 | calming Joe
William Dalton | 105 | keeping Joe from killing
Averell Averell Dalton| 80| playing with Rantanplan
Lucky Luke| 120| capturing the Daltons

Expected Output预期产出

file.txt文件.txt

Jack Dalton| 115 | mad at Averell | Hell0 | 134 johen rd
Joe Dalton| 105 | calming Joe | Hell0 | 134 johen rd
William Dalton | 105 | keeping Joe from killing | Hell0 | 134 johen rd
Averell Averell Dalton| 80| playing with Rantanplan | Hell0 | 134 johen rd
Lucky Luke| 120| capturing the Daltons | Hell0 | 134 johen rd

In the above output file.txt the last two columns are the two constants which I have in my Test.java pojo class.在上面的输出 file.txt 中,最后两列是我在 Test.java pojo 类中的两个常量。 I would like to enrich my pojo fields into the final output.我想将我的 pojo 字段丰富到最终输出中。 Is there a way I can achieve the result.有没有办法可以达到这个结果。

In your route after the step在步骤后的路线中

unmarshal().csv()

you got a List<List<String>> as message body.你有一个List<List<String>>作为消息正文。 Every list entry of the outer list represents a CSV line and each inner list contains the values of a line.外部列表的每个列表条目代表一个 CSV 行,每个内部列表包含一行的值。

What you want to do is to add two values to each inner list.您想要做的是向每个内部列表添加两个值。

Since your values are the same for every "line", it is probably the most simple approach to write a plain Java Bean that takes the List<List<String>> , iterates over the outer list and adds the two values (list entries) to each inner list.由于每个“行”的值都相同,因此编写一个采用List<List<String>>的普通 Java Bean 的最简单方法可能是遍历外部列表并添加两个值(列表条目)到每个内部列表。

in your Camel route you call this bean with在您的 Camel 路线中,您将这个 bean 称为

.bean(YourBean.class, "methodname")

but methodname is only needed when the Bean has multiple methods.但是只有当 Bean 有多个方法时才需要methodname

And then your route continues with然后你的路线继续

.marshal(csvDataFormat)
.to(SOURCE_OUTPUT_PATH)

This should generate the file you want.这应该会生成您想要的文件。

By the way, the .end() at the end of the route is not needed.顺便说一下,不需要路由末尾的.end()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM