2

Hadoop シーケンス ファイルは非常に奇妙です。イメージをシーケンス ファイルにパックしましたが、イメージを復元できません。私はいくつかの簡単なテストを行います。そして、シーケンスファイルを使用する前後でバイトのサイズが同じではないことがわかりました。

Configuration confHadoop = new Configuration();
        FileSystem fs = FileSystem.get(confHadoop);

        String fileName = args[0];
        Path file = new Path(fs.getUri().toString() + "/" + fileName);
        Path seqFile = new Path("/temp.seq");
        SequenceFile.Writer writer = null;
        FSDataInputStream in = null;
        try{
            writer = SequenceFile.createWriter(confHadoop,Writer.file(seqFile), Writer.keyClass(Text.class),
                    Writer.valueClass(BytesWritable.class));

            in = fs.open(file);
            byte buffer[] = IOUtils.toByteArray(in);


            System.out.println("original size ---->  " + String.valueOf(buffer.length));
            writer.append(new Text(fileName), new BytesWritable(buffer));
            System.out.println(calculateMd5(buffer));
            writer.close();

        }finally{
            IOUtils.closeQuietly(in);
        }

        SequenceFile.Reader reader = new SequenceFile.Reader(confHadoop, Reader.file(seqFile));

        Text key = new Text();
        BytesWritable val = new BytesWritable();

        while (reader.next(key, val)) {
            System.out.println("size get from sequence file --->" + String.valueOf(val.getLength()));
            String md5 = calculateMd5(val.getBytes());
            Path readSeq=new Path("/write back.png");  
            FSDataOutputStream out = null;
            out = fs.create(readSeq);
            out.write(val.getBytes());  //YES! GOT THE ORIGIANL IAMGE
            out.close();
            System.out.println(md5);
            .............
}

出力は同じバイト数を取得したことを示しており、イメージをローカル ディスクに書き戻した後、元のイメージを取得したことを確信しています。しかし、なぜ MD5 値が同じではないのでしょうか?

ここで何が間違っていたのですか?

14/04/22 16:21:35 INFO compress.CodecPool: Got brand-new compressor [.deflate]
original size ---->  485709
c413e36fd864b27d4c8927956298edbb
14/04/22 16:21:35 INFO compress.CodecPool: Got brand-new decompressor [.deflate]
size get from sequence file --->485709
322cce20b732126bcb8876c4fcd925cb
4

1 に答える 1

9

私はついにこの奇妙な問題を解決し、それを共有しなければなりません. 最初に、シーケンスからバイトを取得する間違った方法を示します。

Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path input = new Path(inPath);
Reader reader = new SequenceFile.Reader(conf, Reader.file(input));
Text key = new Text();

BytesWritable val = new BytesWritable();
    while (reader.next(key, val)) {
    fileName = key.toString();
    byte[] data = val.getBytes(); //don't think you have got the data!
}

その理由は、 getBytes() が元のデータの正確なサイズを返さないためです。私はデータを使用して入れました

FSDataInputStream in = null;
in = fs.open(input);
byte[] buffer = IOUtils.toByteArray(in);

Writer writer = SequenceFile.createWriter(conf,
Writer.file(output), Writer.keyClass(Text.class),
Writer.valueClass(BytesWritable.class));

writer.append(new Text(inPath), new BytesWritable(buffer));
writer.close();

出力シーケンス ファイルのサイズを確認します。元のサイズにヘッドを加えたものです。getBytes() で元のサイズよりも多くのバイト数が得られる理由がわかりません。しかし、データを正しく取得する方法を見てみましょう。

オプション #1、必要なデータのサイズをコピーします。

byte[] rawdata = val.getBytes();
length = val.getLength(); //exactly size of original data
byte[] data = Arrays.copyOfRange(rawdata,  0, length); this is corrent

オプション #2

byte[] data = val.copyBytes();

これはもっと甘いです。:) 最後にそれを正しくしました。

于 2014-04-23T14:31:31.167 に答える