gpt4 book ai didi

java - Apache Beam TextIO glob 获取原始文件名

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:37 28 4
gpt4 key购买 nike

我已经设置了一个管道。我必须解析数百个 *.gz 文件。因此 glob 工作得很好。

但我需要当前处理文件的原始名称,因为我想将结果文件命名为原始文件。

有人可以帮我吗?

这是我的代码。

@Default.String(LOGS_PATH + "*.gz")
String getInputFile();
void setInputFile(String value);


TextIO.Read read = TextIO.read().withCompressionType(TextIO.CompressionType.GZIP).from(options.getInputFile());
read.getName();

p.apply("ReadLines", read).apply(new CountWords())
.apply(MapElements.via(new FormatAsTextFn()))
.apply("WriteCounts", TextIO.write().to(WordCountOptions.LOGS_PATH + "_" + options.getOutput()));

p.run().waitUntilFinish();

最佳答案

从 Beam 2.2 开始,可以使用 FileIO.match()FileIO.read() 和自定义代码的组合来读取文本行。您已经可以在 HEAD 中使用它,也可以等到 2.2 版最终确定(目前正在进行中)。

PCollection<KV<String, String>> filesAndLines = 
p.apply(FileIO.match().filepattern(...))
.apply(FileIO.read())
.apply(ParDo.of(new DoFn<ReadableFile, KV<String, String>>() {
@ProcessElement
public void process(ProcessContext c) {
ReadableFile f = c.element();
String filename = f.getMetadata().resourceId().toString();
String line;
try (BufferedReader r = new BufferedReader(Channels.newInputStream(f.open()))) {
while ((line = r.readLine()) != null) {
c.output(KV.of(filename, line));
}
}
}
}));

关于java - Apache Beam TextIO glob 获取原始文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47022853/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com