将 CSV 文件转换为 JSON 并将其发送到 JMS 队列

问题描述 投票:0回答:1

我的目标是读取 CSV 文件,将其转换为 JSON,并将生成的 JSON 一一发送到 JMS 队列。我的代码如下:

final BindyCsvDataFormat bindy=new BindyCsvDataFormat(camelproject.EquityFeeds.class);
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
CamelContext _ctx = new DefaultCamelContext(); 
_ctx.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
_ctx.addRoutes(new RouteBuilder() {
    
    public void configure() throws Exception {
        from("file:src/main/resources?fileName=data-sample.csv")
        .unmarshal(bindy)
        .marshal()
        .json(JsonLibrary.Jackson).log("${body}")
        .to("file:src/main/resources/?fileName=emp.json");
    }
    
});

EquityFeeds是上面代码中我的POJO类。

问题:

  1. 没有产生任何输出。 “emp.json”文件未在给定位置生成。
  2. 另外,如何将生成的 JSON 拆分为单独的 JSON 并将其发送到 JMS 队列,就像我对 XML 所做的那样:
.split(body().tokenizeXML("equityFeeds", null)).streaming().to("jms:queue:xml.upstream.queue");

Equity Feeds (POJO):

@CsvRecord(separator = ",",skipFirstLine = true)
public class EquityFeeds {
        
    @DataField(pos = 1) 
    private String externalTransactionId;

    @DataField(pos = 2)
    private String clientId;

    @DataField(pos = 3)
    private String securityId;

    @DataField(pos = 4)
    private String transactionType;

    @DataField(pos = 5)
    private Date transactionDate;

    @DataField(pos = 6)
    private float marketValue; 

    @DataField(pos = 7)
    private String priorityFlag;
    
    // getters and setters... 
}

请注意: 我评论了

.marshal()
.json()
以检查
.unmarshal()
是否正常工作,但解组也无法正常工作,因为未创建“emp.json”。

json csv apache-camel
1个回答
0
投票

如果启动路由时没有任何反应,那么很可能是由于您传递给文件组件的相对路径造成的。可能您的 Java 进程的执行目录不在您想象的位置,并且找不到该文件。为了简化事情,我建议您从绝对路径开始。一旦其他一切正常工作,找出正确的相对路径(您的基础应该是

user.dir
系统属性的值)。

关于拆分内容的问题:这在文档中得到了解答。

这对我有用(Camel 3.1):

public class CsvRouteBuilder extends EndpointRouteBuilder {
    @Override
    public void configure() {
        DataFormat bindy = new BindyCsvDataFormat(BindyModel.class);

        from(file("/tmp?fileName=simpsons.csv"))
            .unmarshal(bindy)
            .split(body())
            .log("Unmarshalled model: ${body}")
            .marshal().json()
            .log("Marshalled to JSON: ${body}")
            // Unique file name for the JSON output
            .setHeader(Exchange.FILE_NAME, () -> UUID.randomUUID().toString() + ".json")
            .to(file("/tmp"));
    }
}

// Use lombok to generate all the boilerplate stuff
@ToString
@Getter
@Setter
@NoArgsConstructor
// Bindy record definition
@CsvRecord(separator = ";", skipFirstLine = true, crlf = "UNIX")
public static class BindyModel {

    @DataField(pos = 1)
    private String firstName;
    @DataField(pos = 2)
    private String middleName;
    @DataField(pos = 3)
    private String lastName;

}

鉴于

/tmp/simpsons.csv

中的输入
firstname;middlename;lastname
Homer;Jay;Simpson
Marge;Jacqueline;Simpson

日志输出看起来像这样

Unmarshalled model: RestRouteBuilder.BindyModel(firstName=Homer, middleName=Jay, lastName=Simpson)
Marshalled to JSON: {"firstName":"Homer","middleName":"Jay","lastName":"Simpson"}
Unmarshalled model: RestRouteBuilder.BindyModel(firstName=Marge, middleName=Jacqueline, lastName=Simpson)
Marshalled to JSON: {"firstName":"Marge","middleName":"Jacqueline","lastName":"Simpson"}

并且两个json文件写在

/tmp
.

© www.soinside.com 2019 - 2024. All rights reserved.