DataNode上的block以及meta文件的结构

package org.apache.hadoop.atest.datanade;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.hadoop.util.DataChecksum;
/**

*程序的主要目的是读取一个block文件和相应的meta文件,然后多相应的data数据进行校验 。

**/
public class BlockDataCheckTest {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        //meta文件
        DataInputStream checksumIn= new DataInputStream(new BufferedInputStream(new FileInputStream("D:/hdfs/blk_-8834172473825889857_1040.meta"), 1024));
        //block文件,数据格式:bytesPerChecksum的数据,bytesPerChecksum的数据,bytesPerChecksum的数据,bytesPerChecksum的数据,bytesPerChecksum的数据。。。。。。。。。
        DataInputStream dataIn=new DataInputStream(new BufferedInputStream(new FileInputStream("D:/hdfs/blk_-8834172473825889857"), 1024));
        //meta文件数据:数据version(shot),checkType(byte,0--无校验,1--CRC32校验),bytesPerChecksum(每多少数据做一次校验),bytesPerChecksum个byte数据的校验码,bytesPerChecksum个byte数据的校验码,bytesPerChecksum个byte数据的校验码。。。。。。。
        //其中数据version是一静态变量:FSDataset.METADATA_VERSION = 1;
        short version=checksumIn.readShort();
        byte checkType=checksumIn.readByte();
        int bytesPerChecksum=checksumIn.readInt();
        System.out.println(version+"   "+checkType+"   "+bytesPerChecksum);
        DataChecksum dataChecksum=DataChecksum.newDataChecksum(checkType, bytesPerChecksum);
        byte[]  dataBytes=new byte[bytesPerChecksum];
        byte[]  checkBytes=new byte[dataChecksum.getChecksumSize()];
        while(dataIn.read(dataBytes)>0){
            checksumIn.read(checkBytes);
            dataChecksum.reset();
            dataChecksum.update(dataBytes, 0, dataBytes.length);
            if(dataChecksum.compare(checkBytes, 0)){
                System.out.println("right !!");
            }else{
                System.err.println("wrong !!");
                throw new RuntimeException("-------------------");
            }
        }
        dataIn.close();
        checksumIn.close();
    }
}

转载于:https://www.cnblogs.com/serendipity/archive/2012/03/14/2396095.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值