0

にはランダムな読み取りと書き込みが存在しないことを読みましたHadoop HDFS。しかし、書き込みの引数DFSOutputStream

void write(byte buf[], int off, int len)
void write(int b)

同様に、読み込みの引数DFSInputStream

int read(byte buf[], int off, int len)

int read()

OffSet パラメータは、 への読み取り/書き込み呼び出しの両方で確認できますHDFSMapReduceフレームワークが最後の位置にデータを追加するためにのみ使用される場合、なぜ必要なのですか? で「オフセット」パラメータはどのように使用されHDFSますか? HDFS 書き込みは常に追加のみですか?

4

2 に答える 2

1

パラメータint offは、入力ファイル内のランダム ポイントを表していません。これは実際には、データが byte[ ] 内に書き込まれる場所からバイト数lenまでの byte[ ] 内のオフセットです。たとえば、次のように書いたとします。

byte buf[15];
read(buf, 5, 10);

これにより、ファイルの5 番目のバイトからではなく入力ファイルの先頭からデータが読み取られます。しかし、配列buf[ ]は5 番目のバイトから最後のバイト (5+10) まで埋められます。

クロスチェックするには、パラメーター offに別の値を使用できます。offに指定した値に関係なく、データは常にファイルの先頭から読み取られます (明示的に seek を使用していない場合)

ここで注意すべき点の 1 つは、配列のサイズがoff+len 未満であってはならないということです。

明確に理解するために、次の例を実行してください。

public class ReadHdfsFile {

    public static void main(String[] args) throws IOException {

        Configuration conf = new Configuration();
        conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/core-site.xml"));
        conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/hdfs-site.xml"));
        FileSystem fs = FileSystem.get(conf);
        FSDataInputStream in = fs.open(new Path("/demo.txt"));

        //Filling the array b1 from the 5th byte
        int charPos = 0;
        byte[] b1 = new byte[10];
        int bytesRead = in.read(b1, 5, 5);
        System.out.println("Bytes Read : " + bytesRead);
        String s = new String(b1, "UTF-8");
        System.out.println("Printing char by char(you'll see first 5 bytes as blank)...");
        for(char c : s.toCharArray()){
            System.out.println("Character " + ++charPos + " : " + c);

        }
        System.out.println();
        System.out.println("Changing offset value....");

        //Filling the array b2 from the 10th byte
        in.seek(0);
        charPos = 0;
        byte[] b2 = new byte[15];
        bytesRead = in.read(b2, 10, 5);
        System.out.println("Bytes Read : " + bytesRead);
        s = new String(b2, "UTF-8");
        System.out.println("Printing char by char(you'll see first 10 bytes as blank)...");
        for(char c : s.toCharArray()){
            System.out.println("Character " + ++charPos + " : " + c);
        }

        System.out.println("DONE!!!");
        in.close();
        fs.close();
    }
}

HTH

于 2013-10-05T02:05:39.927 に答える