【发布时间】:2019-04-21 16:18:23
【问题描述】:
我有一个包含 99 个文件的目录,我想读取这些文件,然后将它们散列到 sha256 校验和中。我最终想将它们输出到一个带有键值对的 JSON 文件,例如(文件 1,092180x0123)。目前我无法将我的 ParDo 函数传递给一个可读的文件,我必须遗漏一些非常容易的东西。这是我第一次使用 Apache Beam,所以任何帮助都会很棒。这是我目前所拥有的
public class BeamPipeline {
public static void main(String[] args) {
PipelineOptions options = PipelineOptionsFactory.create();
Pipeline p = Pipeline.create(options);
p
.apply("Match Files", FileIO.match().filepattern("../testdata/input-*"))
.apply("Read Files", FileIO.readMatches())
.apply("Hash File",ParDo.of(new DoFn<FileIO.ReadableFile, KV<FileIO.ReadableFile, String>>() {
@ProcessElement
public void processElement(@Element FileIO.ReadableFile file, OutputReceiver<KV<FileIO.ReadableFile, String>> out) throws
NoSuchAlgorithmException, IOException {
// File -> Bytes
String strfile = file.toString();
byte[] byteFile = strfile.getBytes();
// SHA-256
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] messageDigest = md.digest(byteFile);
BigInteger no = new BigInteger(1, messageDigest);
String hashtext = no.toString(16);
while(hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
out.output(KV.of(file, hashtext));
}
}))
.apply(FileIO.write());
p.run();
}
}
【问题讨论】:
标签: google-cloud-dataflow apache-beam dataflow