You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Camel反序列化CSV时测试与生产环境结果不一致问题

问题原因及修复方案

核心原因

  1. CSV文件结构不统一:生产环境的CSV是单行多列格式(一行包含两个ID,逗号分隔),测试环境的CSV是多行单列格式(每行一个ID)。Camel的CSV组件默认逻辑是把每一行解析为一个List<String>,因此:

    • 生产环境解析后得到List<String>,转成String后是"id1, id2"格式,Camel可自动转换为List<String>;
    • 测试环境解析后得到List<List<String>>,转成String后是"[[id1], [id2]]"的嵌套格式,Camel无法直接将其转为List<String>,导致getBody(List.class)返回null。
  2. 冗余转换放大问题:路由中的.convertBodyTo(String.class)完全多余——unmarshal().csv()已经将文件解析为List结构,转成String反而破坏了原生类型结构,导致后续类型转换失败。

修复方案

  1. 对齐测试与生产的CSV格式:确保测试用CSV和生产环境结构一致(要么单行多列,要么多行单列)。
  2. 移除多余的类型转换:删掉.convertBodyTo(String.class)步骤,直接使用解析后的List结构:
    from("direct:start")
        .routeId("report")
        .pollEnrich("file:///Users/xxx/csv")
        .log(LoggingLevel.INFO, " File detected: ${header.CamelAwsS3Key}")
        .unmarshal().csv()
        .process(new Processor() {
          @Override
          public void process(Exchange exchange) throws Exception {
            // 若CSV是单行多列,直接取List<String>
            List<String> ids = exchange.getIn().getBody(List.class);
            // 若CSV是多行单列,先取嵌套列表再扁平化
            // List<List<String>> rawIds = exchange.getIn().getBody(List.class);
            // List<String> ids = rawIds.stream().flatMap(List::stream).collect(Collectors.toList());
          }
        });
    
  3. 固定CSV解析规则:给CSV组件添加明确配置,避免环境差异影响解析结果:
    .unmarshal().csv(new CsvDataFormat().setDelimiter(',').setSkipHeaderRecord(false))
    

内容的提问来源于stack exchange,提问作者Paulo Henrique de Siqueira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 14:45:33