我如何获得可读文件?

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

我有一个目录,里面有99个文件,我想读取这些文件,然后将它们哈希到sha256校验和中。我最终想要将它们输出到具有键值对的JSON文件,例如(文件1,092180x0123)。目前我无法将我的ParDo功能传递给可读文件我一定很容易错过。这是我第一次使用Apache光束,所以任何帮助都会很棒。这是我到目前为止所拥有的

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
1个回答
1
投票

一个例子是让KV对包含匹配的文件名(来自MetadataResult)和整个文件的相应SHA-256(而不是逐行读取):

p
  .apply("Match Filenames", FileIO.match().filepattern(options.getInput()))
  .apply("Read Matches", FileIO.readMatches())
  .apply(MapElements.via(new SimpleFunction <ReadableFile, KV<String,String>>() {
      public KV<String,String> apply(ReadableFile f) {
            String temp = null;
            try{
                temp = f.readFullyAsUTF8String();
            }catch(IOException e){

            }

            String sha256hex = org.apache.commons.codec.digest.DigestUtils.sha256Hex(temp);   

            return KV.of(f.getMetadata().resourceId().toString(), sha256hex);
        }
      }
  ))
  .apply("Print results", ParDo.of(new DoFn<KV<String, String>, Void>() {
      @ProcessElement
      public void processElement(ProcessContext c) {
        Log.info(String.format("File: %s, SHA-256: %s ", c.element().getKey(), c.element().getValue()));
      }
    }
 ));

完整代码here。我的输出是:

Apr 21, 2019 10:02:21 PM com.dataflow.samples.DataflowSHA256$2 processElement
INFO: File: /home/.../data/file1, SHA-256: e27cf439835d04081d6cd21f90ce7b784c9ed0336d1aa90c70c8bb476cd41157 
Apr 21, 2019 10:02:21 PM com.dataflow.samples.DataflowSHA256$2 processElement
INFO: File: /home/.../data/file2, SHA-256: 72113bf9fc03be3d0117e6acee24e3d840fa96295474594ec8ecb7bbcb5ed024

我用在线哈希tool验证的:

enter image description here

顺便说一句,我不认为你需要OutputReceiver单输出(没有侧输出)。感谢这些有用的问题/答案:123

© www.soinside.com 2019 - 2024. All rights reserved.