使用Apache文件组件分块读取大文件进行处理,而无需将整个文件内容加载到内存中

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

当路由语句“from(file://dir/largefile.txt)”拾取文件时,apache 文件组件将整个文件加载到内存中。问题是文件大小为 300MB,而 kubernetes pod 的内存有限,因此在加载大文件时会导致内存不足错误。

加载文件后,将使用分割来处理文件中的行。 .split(body().tokenize(" ")).streaming()

问题是如何让文件组件不首先将整个文件加载到内存中。

如果有任何其他方法可以与 Apache Camel 配合使用来实现这一点也可以。

在上一节中已解释过

apache-camel
1个回答
0
投票

您是否尝试使用流缓存,它使用临时文件,并且不会加载整个文件来处理它?

类似的东西:

 @Override
public void configure() throws Exception {
    // Enable StreamCache
    getContext().setStreamCaching(true);
    getContext().getStreamCachingStrategy().setSpoolThreshold(1024L * 1024L); // 1MB threshold

    from("file://dir?fileName=largefile.txt&noop=true")
        .split(body().tokenize("\n")).streaming()
        .process(exchange -> {
            String line = exchange.getIn().getBody(String.class);
            // Process each line
            System.out.println(line);
        })
        .end();
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.