Search in sources :

Example 1 with HdfsLocatedFileStatus

use of org.apache.hadoop.hdfs.protocol.HdfsLocatedFileStatus in project hadoop by apache.

the class PBHelperClient method convert.

public static HdfsFileStatusProto convert(HdfsFileStatus fs) {
    if (fs == null)
        return null;
    FileType fType = FileType.IS_FILE;
    if (fs.isDir()) {
        fType = FileType.IS_DIR;
    } else if (fs.isSymlink()) {
        fType = FileType.IS_SYMLINK;
    }
    HdfsFileStatusProto.Builder builder = HdfsFileStatusProto.newBuilder().setLength(fs.getLen()).setFileType(fType).setBlockReplication(fs.getReplication()).setBlocksize(fs.getBlockSize()).setModificationTime(fs.getModificationTime()).setAccessTime(fs.getAccessTime()).setPermission(convert(fs.getPermission())).setOwner(fs.getOwner()).setGroup(fs.getGroup()).setFileId(fs.getFileId()).setChildrenNum(fs.getChildrenNum()).setPath(getByteString(fs.getLocalNameInBytes())).setStoragePolicy(fs.getStoragePolicy());
    if (fs.isSymlink()) {
        builder.setSymlink(getByteString(fs.getSymlinkInBytes()));
    }
    if (fs.getFileEncryptionInfo() != null) {
        builder.setFileEncryptionInfo(convert(fs.getFileEncryptionInfo()));
    }
    if (fs instanceof HdfsLocatedFileStatus) {
        final HdfsLocatedFileStatus lfs = (HdfsLocatedFileStatus) fs;
        LocatedBlocks locations = lfs.getBlockLocations();
        if (locations != null) {
            builder.setLocations(convert(locations));
        }
    }
    if (fs.getErasureCodingPolicy() != null) {
        builder.setEcPolicy(convertErasureCodingPolicy(fs.getErasureCodingPolicy()));
    }
    return builder.build();
}
Also used : FileType(org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.HdfsFileStatusProto.FileType) HdfsFileStatusProto(org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.HdfsFileStatusProto) HdfsLocatedFileStatus(org.apache.hadoop.hdfs.protocol.HdfsLocatedFileStatus) LocatedBlocks(org.apache.hadoop.hdfs.protocol.LocatedBlocks)

Example 2 with HdfsLocatedFileStatus

use of org.apache.hadoop.hdfs.protocol.HdfsLocatedFileStatus in project hadoop by apache.

the class FSDirStatAndListingOp method getListing.

/**
   * Get a partial listing of the indicated directory
   *
   * We will stop when any of the following conditions is met:
   * 1) this.lsLimit files have been added
   * 2) needLocation is true AND enough files have been added such
   * that at least this.lsLimit block locations are in the response
   *
   * @param fsd FSDirectory
   * @param iip the INodesInPath instance containing all the INodes along the
   *            path
   * @param startAfter the name to start listing after
   * @param needLocation if block locations are returned
   * @param includeStoragePolicy if storage policy is returned
   * @return a partial listing starting after startAfter
   */
private static DirectoryListing getListing(FSDirectory fsd, INodesInPath iip, byte[] startAfter, boolean needLocation, boolean includeStoragePolicy) throws IOException {
    if (FSDirectory.isExactReservedName(iip.getPathComponents())) {
        return getReservedListing(fsd);
    }
    fsd.readLock();
    try {
        if (iip.isDotSnapshotDir()) {
            return getSnapshotsListing(fsd, iip, startAfter);
        }
        final int snapshot = iip.getPathSnapshotId();
        final INode targetNode = iip.getLastINode();
        if (targetNode == null) {
            return null;
        }
        byte parentStoragePolicy = includeStoragePolicy ? targetNode.getStoragePolicyID() : HdfsConstants.BLOCK_STORAGE_POLICY_ID_UNSPECIFIED;
        if (!targetNode.isDirectory()) {
            // target INode
            return new DirectoryListing(new HdfsFileStatus[] { createFileStatus(fsd, iip, null, parentStoragePolicy, needLocation) }, 0);
        }
        final INodeDirectory dirInode = targetNode.asDirectory();
        final ReadOnlyList<INode> contents = dirInode.getChildrenList(snapshot);
        int startChild = INodeDirectory.nextChild(contents, startAfter);
        int totalNumChildren = contents.size();
        int numOfListing = Math.min(totalNumChildren - startChild, fsd.getLsLimit());
        int locationBudget = fsd.getLsLimit();
        int listingCnt = 0;
        HdfsFileStatus[] listing = new HdfsFileStatus[numOfListing];
        for (int i = 0; i < numOfListing && locationBudget > 0; i++) {
            INode child = contents.get(startChild + i);
            byte childStoragePolicy = (includeStoragePolicy && !child.isSymlink()) ? getStoragePolicyID(child.getLocalStoragePolicyID(), parentStoragePolicy) : parentStoragePolicy;
            listing[i] = createFileStatus(fsd, iip, child, childStoragePolicy, needLocation);
            listingCnt++;
            if (listing[i] instanceof HdfsLocatedFileStatus) {
                // Once we  hit lsLimit locations, stop.
                // This helps to prevent excessively large response payloads.
                // Approximate #locations with locatedBlockCount() * repl_factor
                LocatedBlocks blks = ((HdfsLocatedFileStatus) listing[i]).getBlockLocations();
                locationBudget -= (blks == null) ? 0 : blks.locatedBlockCount() * listing[i].getReplication();
            }
        }
        // truncate return array if necessary
        if (listingCnt < numOfListing) {
            listing = Arrays.copyOf(listing, listingCnt);
        }
        return new DirectoryListing(listing, totalNumChildren - startChild - listingCnt);
    } finally {
        fsd.readUnlock();
    }
}
Also used : DirectoryListing(org.apache.hadoop.hdfs.protocol.DirectoryListing) HdfsLocatedFileStatus(org.apache.hadoop.hdfs.protocol.HdfsLocatedFileStatus) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) LocatedBlocks(org.apache.hadoop.hdfs.protocol.LocatedBlocks)

Example 3 with HdfsLocatedFileStatus

use of org.apache.hadoop.hdfs.protocol.HdfsLocatedFileStatus in project hive by apache.

the class Hadoop23Shims method listLocatedHdfsStatus.

@Override
public List<HdfsFileStatusWithId> listLocatedHdfsStatus(FileSystem fs, Path p, PathFilter filter) throws IOException {
    DistributedFileSystem dfs = ensureDfs(fs);
    DFSClient dfsc = dfs.getClient();
    final String src = p.toUri().getPath();
    DirectoryListing current = dfsc.listPaths(src, org.apache.hadoop.hdfs.protocol.HdfsFileStatus.EMPTY_NAME, true);
    if (current == null) {
        // the directory does not exist
        throw new FileNotFoundException("File " + p + " does not exist.");
    }
    final URI fsUri = fs.getUri();
    List<HdfsFileStatusWithId> result = new ArrayList<HdfsFileStatusWithId>(current.getPartialListing().length);
    while (current != null) {
        org.apache.hadoop.hdfs.protocol.HdfsFileStatus[] hfss = current.getPartialListing();
        for (int i = 0; i < hfss.length; ++i) {
            HdfsLocatedFileStatus next = (HdfsLocatedFileStatus) (hfss[i]);
            if (filter != null) {
                Path filterPath = next.getFullPath(p).makeQualified(fsUri, null);
                if (!filter.accept(filterPath))
                    continue;
            }
            LocatedFileStatus lfs = next.makeQualifiedLocated(fsUri, p);
            result.add(new HdfsFileStatusWithIdImpl(lfs, next.getFileId()));
        }
        current = current.hasMore() ? dfsc.listPaths(src, current.getLastName(), true) : null;
    }
    return result;
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) DirectoryListing(org.apache.hadoop.hdfs.protocol.DirectoryListing) Path(org.apache.hadoop.fs.Path) HdfsLocatedFileStatus(org.apache.hadoop.hdfs.protocol.HdfsLocatedFileStatus) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) LocatedFileStatus(org.apache.hadoop.fs.LocatedFileStatus) HdfsLocatedFileStatus(org.apache.hadoop.hdfs.protocol.HdfsLocatedFileStatus) DistributedFileSystem(org.apache.hadoop.hdfs.DistributedFileSystem) URI(java.net.URI)

Aggregations

HdfsLocatedFileStatus (org.apache.hadoop.hdfs.protocol.HdfsLocatedFileStatus)3 DirectoryListing (org.apache.hadoop.hdfs.protocol.DirectoryListing)2 LocatedBlocks (org.apache.hadoop.hdfs.protocol.LocatedBlocks)2 FileNotFoundException (java.io.FileNotFoundException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 LocatedFileStatus (org.apache.hadoop.fs.LocatedFileStatus)1 Path (org.apache.hadoop.fs.Path)1 DFSClient (org.apache.hadoop.hdfs.DFSClient)1 DistributedFileSystem (org.apache.hadoop.hdfs.DistributedFileSystem)1 HdfsFileStatus (org.apache.hadoop.hdfs.protocol.HdfsFileStatus)1 HdfsFileStatusProto (org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.HdfsFileStatusProto)1 FileType (org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.HdfsFileStatusProto.FileType)1