简体   繁体   English

使用骆驼从FTP动态重命名文件

[英]Dynamically rename files from FTP with camel

I am attempting to use camel to route some files from an FTP into HDFS. 我正在尝试使用骆驼将某些文件从FTP路由到HDFS。 My routes in general seem to be working fine, however anytime there is a file that has a space in its name, the route fails. 我的路由总体而言似乎运行良好,但是只要文件名中包含空格,该路由就会失败。 It fails trying to copy the file over to HDFS. 尝试将文件复制到HDFS失败。 The files are dynamic and nature and change daily, so I will not be able to do a specific include and change the file name through .setHeader, nor will I be able to rename the files on the FTP. 这些文件是动态的,自然的并且每天都在变化,因此我将无法通过.setHeader进行特定的包含和更改文件名,也无法在FTP上重命名文件。

Is it possible to dynamically rename files that have a space in there names with camel, before routing them into HDFS? 在将它们路由到HDFS之前,是否可以使用骆驼动态重命名名称中具有空格的文件?

The file name being stored in the message header called "CamelFileName", you could go with something like this: 文件名存储在名为“ CamelFileName”的消息头中,您可以使用类似以下内容的文件:

from("sftp:...")
        .process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                Message in = exchange.getIn();
                String originalFilename = (String) in.getHeader(Exchange.FILE_NAME);
                String modifiedFilename = originalFilename.replaceAll("\\s+", "");
                in.setHeader(Exchange.FILE_NAME,  modifiedFilename);
        }})
.to("hdfs:...");

or more succinctly: 或更简洁地:

from("ftp:in")
     .setHeader(Exchange.FILE_NAME, header(Exchange.FILE_NAME).regexReplaceAll("\\s+", "_").getExpression())
     .to("hdfs:out");

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

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