hdfs文件校验和

fhg3lkii  于 2021-06-04  发布在  Hadoop
关注(0)|答案(2)|浏览(392)

在使用hadoopapi-dfscleint.getfilechecksum()复制到hdfs之后,我正在尝试检查文件的一致性。
我将获得上述代码的以下输出:

Null
HDFS : null
Local : null

有人能指出错误吗?代码如下:

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileChecksum;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocalFileSystem;
import org.apache.hadoop.fs.Path;

public class fileCheckSum {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        Configuration conf = new Configuration();

         FileSystem hadoopFS = FileSystem.get(conf);
    //  Path hdfsPath = new Path("/derby.log");

        LocalFileSystem localFS = LocalFileSystem.getLocal(conf);
    //      Path localPath = new Path("file:///home/ubuntu/derby.log");

    //  System.out.println("HDFS PATH : "+hdfsPath.getName());
    //      System.out.println("Local PATH : "+localPath.getName());

        FileChecksum hdfsChecksum = hadoopFS.getFileChecksum(new Path("/derby.log"));
        FileChecksum localChecksum = localFS.getFileChecksum(new Path("file:///home/ubuntu/derby.log"));

        if(null!=hdfsChecksum || null!=localChecksum){
            System.out.println("HDFS Checksum : "+hdfsChecksum.toString()+"\t"+hdfsChecksum.getLength());
            System.out.println("Local Checksum : "+localChecksum.toString()+"\t"+localChecksum.getLength());

            if(hdfsChecksum.toString().equals(localChecksum.toString())){
                System.out.println("Equal");
            }else{
                System.out.println("UnEqual");

            }
        }else{
            System.out.println("Null");
            System.out.println("HDFS : "+hdfsChecksum);
            System.out.println("Local : "+localChecksum);

        }

    }

}
au9on6nz

au9on6nz1#

试试这个。在本文中,我计算了本地和hdfs文件的md5,然后比较了这两个文件的md5。希望这有帮助。

public static void compareChecksumForLocalAndHdfsFile(String sourceHdfsFilePath, String sourceLocalFilepath, Map<String, String> hdfsConfigMap)
        throws Exception {
    System.setProperty("HADOOP_USER_NAME", hdfsConfigMap.get(Constants.USERNAME));
    System.setProperty("hadoop.home.dir", "/tmp");
    Configuration hdfsConfig = new Configuration();
    hdfsConfig.set(Constants.USERNAME, hdfsConfigMap.get(Constants.USERNAME));
    hdfsConfig.set("fsURI", hdfsConfigMap.get("fsURI"));
    FileSystem hdfs = FileSystem.get(new URI(hdfsConfigMap.get("fsURI")), hdfsConfig);
    Path inputPath = new Path(hdfsConfigMap.get("fsURI") + "/" + sourceHdfsFilePath);
    InputStream is = hdfs.open(inputPath);
    String localChecksum = getMD5Checksum(new FileInputStream(sourceLocalFilepath));
    String hdfsChecksum = getMD5Checksum(is);
    if (null != hdfsChecksum || null != localChecksum) {
        System.out.println("HDFS Checksum : " + hdfsChecksum.toString() + "\t" + hdfsChecksum.length());
        System.out.println("Local Checksum : " + localChecksum.toString() + "\t" + localChecksum.length());

        if (hdfsChecksum.toString().equals(localChecksum.toString())) {
            System.out.println("Equal");
        } else {
            System.out.println("UnEqual");
        }
    } else {
        System.out.println("Null");
        System.out.println("HDFS : " + hdfsChecksum);
        System.out.println("Local : " + localChecksum);
    }
}

public static byte[] createChecksum(String filename) throws Exception {
    InputStream fis = new FileInputStream(filename);

    byte[] buffer = new byte[1024];
    MessageDigest complete = MessageDigest.getInstance("MD5");
    int numRead;

    do {
        numRead = fis.read(buffer);
        if (numRead > 0) {
            complete.update(buffer, 0, numRead);
        }
    } while (numRead != -1);

    fis.close();
    return complete.digest();
}

// see this How-to for a faster way to convert
// a byte array to a HEX string
public static String getMD5Checksum(String filename) throws Exception {
    byte[] b = createChecksum(filename);
    String result = "";

    for (int i = 0; i < b.length; i++) {
        result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
    }
    return result;
}

输出:

HDFS Checksum : d99513cc4f1d9c51679a125702bd27b0    32
Local Checksum : d99513cc4f1d9c51679a125702bd27b0   32
Equal
d5vmydt9

d5vmydt92#

因为您没有在服务器上设置远程地址 conf 基本上使用相同的配置 hadoopFS 以及 localFS 正在指向的示例 LocalFileSystem . getFileChecksum 未为实现 LocalFileSystem 并返回null。它应该为你工作 DistributedFileSystem 不过,如果你的 conf 指向一个分布式集群, FileSystem.get(conf) 应返回的示例 DistributedFileSystem 返回大小为的块的crc32校验和的md5或md5 bytes.per.checksum . 此值取决于块大小和集群范围的配置, bytes.per.checksum . 这就是为什么这两个参数也被编码在分布式校验和的返回值中作为算法的名称:md5-of-md5-of-yyycrc32,其中是每个块的crc校验和数,yyy是 bytes.per.checksum 参数。
这个 getFileChecksum 不是设计成可以跨文件系统进行比较的。虽然可以在本地模拟分布式校验和,或者手工Mapreduce作业来计算本地哈希的等价物,但我建议依赖hadoop自己的完整性检查,当文件被写入hadoop或从hadoop读取时,会发生这种检查

相关问题