Search in sources :

Example 11 with MD5MD5CRC32FileChecksum

use of org.apache.hadoop.fs.MD5MD5CRC32FileChecksum in project SSM by Intel-bigdata.

the class CheckSumAction method execute.

@Override
protected void execute() throws Exception {
    // HadoopUtil.getNameNodeUri(conf), conf));
    if (fileName == null) {
        throw new IllegalArgumentException("Please specify file path!");
    }
    if (fileName.charAt(fileName.length() - 1) == '*') {
        DirectoryListing listing = dfsClient.listPaths(fileName.substring(0, fileName.length() - 1), HdfsFileStatus.EMPTY_NAME);
        HdfsFileStatus[] fileList = listing.getPartialListing();
        for (HdfsFileStatus fileStatus : fileList) {
            String file1 = fileStatus.getFullPath(new Path(fileName.substring(0, fileName.length() - 1))).toString();
            HdfsFileStatus fileStatus1 = dfsClient.getFileInfo(file1);
            long length = fileStatus1.getLen();
            MD5MD5CRC32FileChecksum md5 = dfsClient.getFileChecksum(file1, length);
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            DataOutputStream dataStream = new DataOutputStream(byteStream);
            md5.write(dataStream);
            byte[] bytes = byteStream.toByteArray();
            appendLog(String.format("%s\t%s\t%s", file1, md5.getAlgorithmName(), byteArray2HexString(bytes)));
        }
        return;
    }
    HdfsFileStatus fileStatus = dfsClient.getFileInfo(fileName);
    if (fileStatus != null) {
        if (fileStatus.isDir()) {
            appendResult("This is a directory which has no checksum result!");
            appendLog("This is a directory which has no checksum result!");
            return;
        }
    }
    long length = fileStatus.getLen();
    MD5MD5CRC32FileChecksum md5 = dfsClient.getFileChecksum(fileName, length);
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    DataOutputStream dataStream = new DataOutputStream(byteStream);
    md5.write(dataStream);
    byte[] bytes = byteStream.toByteArray();
    appendLog(String.format("%s\t%s\t%s", fileName, md5.getAlgorithmName(), byteArray2HexString(bytes)));
}
Also used : DirectoryListing(org.apache.hadoop.hdfs.protocol.DirectoryListing) Path(org.apache.hadoop.fs.Path) MD5MD5CRC32FileChecksum(org.apache.hadoop.fs.MD5MD5CRC32FileChecksum) DataOutputStream(java.io.DataOutputStream) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

MD5MD5CRC32FileChecksum (org.apache.hadoop.fs.MD5MD5CRC32FileChecksum)11 Path (org.apache.hadoop.fs.Path)7 Test (org.junit.Test)6 MD5Hash (org.apache.hadoop.io.MD5Hash)5 IOException (java.io.IOException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 InOrder (org.mockito.InOrder)2 Gson (com.google.gson.Gson)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 Map (java.util.Map)1 Configuration (org.apache.hadoop.conf.Configuration)1 CreateFlag (org.apache.hadoop.fs.CreateFlag)1 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)1 FileSystem (org.apache.hadoop.fs.FileSystem)1 MD5MD5CRC32CastagnoliFileChecksum (org.apache.hadoop.fs.MD5MD5CRC32CastagnoliFileChecksum)1 MD5MD5CRC32GzipFileChecksum (org.apache.hadoop.fs.MD5MD5CRC32GzipFileChecksum)1 ChecksumOpt (org.apache.hadoop.fs.Options.ChecksumOpt)1