Example usage for org.apache.hadoop.fs BlockLocation getNames

List of usage examples for org.apache.hadoop.fs BlockLocation getNames

Introduction

In this page you can find the example usage for org.apache.hadoop.fs BlockLocation getNames.

Prototype

public String[] getNames() throws IOException 

Source Link

Document

Get the list of names (IP:xferPort) hosting this block

Usage

From source file:ch.cern.db.hdfs.Main.java

License:GNU General Public License

private void printBlockMetadata(BlockLocation blockLocation, String[] dataDirs) throws IOException {

    System.out.println("   Offset: " + blockLocation.getOffset());
    System.out.println("   Length: " + blockLocation.getLength());

    String[] cachedHosts = blockLocation.getCachedHosts();
    if (cachedHosts.length == 0) {
        System.out.println("   No cached hosts");
    }//from  w ww . j a v  a 2s  .co m

    System.out.println("   Replicas:");
    VolumeId[] volumeIds = blockLocation instanceof BlockStorageLocation
            ? (((BlockStorageLocation) blockLocation).getVolumeIds())
            : null;
    String[] hosts = blockLocation.getHosts();
    String[] names = blockLocation.getNames();
    String[] topologyPaths = blockLocation.getTopologyPaths();
    for (int i = 0; i < topologyPaths.length; i++) {
        int diskId = volumeIds != null ? DistributedFileSystemMetadata.getDiskId(volumeIds[i]) : -1;

        System.out.println("      Replica (" + i + "):");
        System.out.println("         Host: " + hosts[i]);

        if (diskId == -1)
            System.out.println("         DiskId: unknown");
        else if (dataDirs != null && diskId < dataDirs.length)
            System.out.println("         Location: " + dataDirs[diskId] + " (DiskId: " + diskId + ")");
        else
            System.out.println("         DiskId: " + diskId);

        System.out.println("         Name: " + names[i]);
        System.out.println("         TopologyPaths: " + topologyPaths[i]);
    }

    if (cachedHosts.length > 0) {
        System.out.println("   Cached hosts:");
        for (String cachedHost : cachedHosts) {
            System.out.println("      Host: " + cachedHost);
        }
    }
}

From source file:com.cloudera.GetBlockLocations.java

License:Apache License

public static void main(String[] args) throws Exception {
    final Configuration conf = new Configuration();
    String url = getStringOrDie("get.block.locations.path");
    final FileSystem fs = FileSystem.get(new URI(url), conf);

    if (!fs.exists(new Path(url))) {
        System.out.println("no file at " + url);
        System.exit(1);//w w  w  . j a v a 2  s . co m
    }
    BlockLocation locs[] = null;
    try {
        locs = fs.getFileBlockLocations(new Path(url), 0, Long.MAX_VALUE);
    } catch (IOException e) {
        System.out.println("Error calling getFileBlockLocations(" + url + ")\n");
        e.printStackTrace(System.err);
        System.exit(1);
    }

    String prefix = "";
    for (BlockLocation loc : locs) {
        System.out.println(prefix);
        System.out.println("{");
        System.out.println("  hosts =         " + Arrays.toString(loc.getHosts()));
        System.out.println("  cachedHosts =   " + Arrays.toString(loc.getCachedHosts()));
        System.out.println("  names    =      " + Arrays.toString(loc.getNames()));
        System.out.println("  topologyPaths = " + Arrays.toString(loc.getTopologyPaths()));
        System.out.println("  offset =        " + loc.getOffset());
        System.out.println("  length =        " + loc.getLength());
        System.out.println("  corrupt =       " + loc.isCorrupt());
        System.out.println("}");
        prefix = ",";
    }
}

From source file:com.cloudera.impala.catalog.HdfsTable.java

License:Apache License

/**
 * Loads the file block metadata for the given collection of FileDescriptors.  The
 * FileDescriptors are passed as a tree, where the first level is indexed by
 * filesystem, the second level is indexed by partition location, and the leaves are
 * the list of files that exist under each directory.
 *//*from  w w w .  j a  va 2  s  .  c  o  m*/
private void loadBlockMd(Map<FsKey, Map<String, List<FileDescriptor>>> perFsFileDescs) throws RuntimeException {
    Preconditions.checkNotNull(perFsFileDescs);
    LOG.debug("load block md for " + name_);

    for (FsKey fsEntry : perFsFileDescs.keySet()) {
        FileSystem fs = fsEntry.filesystem;
        // Store all BlockLocations so they can be reused when loading the disk IDs.
        List<BlockLocation> blockLocations = Lists.newArrayList();
        int numCachedBlocks = 0;
        Map<String, List<FileDescriptor>> partitionToFds = perFsFileDescs.get(fsEntry);
        Preconditions.checkNotNull(partitionToFds);
        // loop over all files and record their block metadata, minus volume ids
        for (String partitionDir : partitionToFds.keySet()) {
            Path partDirPath = new Path(partitionDir);
            for (FileDescriptor fileDescriptor : partitionToFds.get(partitionDir)) {
                Path p = new Path(partDirPath, fileDescriptor.getFileName());
                try {
                    FileStatus fileStatus = fs.getFileStatus(p);
                    // fileDescriptors should not contain directories.
                    Preconditions.checkArgument(!fileStatus.isDirectory());
                    BlockLocation[] locations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
                    Preconditions.checkNotNull(locations);
                    blockLocations.addAll(Arrays.asList(locations));

                    // Loop over all blocks in the file.
                    for (BlockLocation block : locations) {
                        String[] blockHostPorts = block.getNames();
                        try {
                            blockHostPorts = block.getNames();
                        } catch (IOException e) {
                            // this shouldn't happen, getNames() doesn't throw anything
                            String errorMsg = "BlockLocation.getNames() failed:\n" + e.getMessage();
                            LOG.error(errorMsg);
                            throw new IllegalStateException(errorMsg);
                        }
                        // Now enumerate all replicas of the block, adding any unknown hosts to
                        // hostIndex_ and the index for that host to replicaHostIdxs.
                        List<Integer> replicaHostIdxs = new ArrayList<Integer>(blockHostPorts.length);
                        for (int i = 0; i < blockHostPorts.length; ++i) {
                            String[] ip_port = blockHostPorts[i].split(":");
                            Preconditions.checkState(ip_port.length == 2);
                            TNetworkAddress network_address = new TNetworkAddress(ip_port[0],
                                    Integer.parseInt(ip_port[1]));
                            replicaHostIdxs.add(hostIndex_.getIndex(network_address));
                        }
                        fileDescriptor.addFileBlock(
                                new FileBlock(block.getOffset(), block.getLength(), replicaHostIdxs));
                    }
                } catch (IOException e) {
                    throw new RuntimeException(
                            "couldn't determine block locations for path '" + p + "':\n" + e.getMessage(), e);
                }
            }
        }

        if (SUPPORTS_VOLUME_ID && fs instanceof DistributedFileSystem) {
            LOG.trace("loading disk ids for: " + getFullName() + ". nodes: " + getNumNodes() + ". file system: "
                    + fsEntry);
            loadDiskIds((DistributedFileSystem) fs, blockLocations, partitionToFds);
            LOG.trace("completed load of disk ids for: " + getFullName());
        }
    }
}

From source file:com.cloudera.impala.catalog.TestLoadHdfsMetadataPerf.java

License:Apache License

/**
 * List file status by calling fileSystem.listStatus.
 *//*  w  ww  .j  ava 2  s  . c o m*/
private static void listStatus(String dirPath) {
    Path path = new Path(dirPath);
    boolean exceptionThrown = false;
    try {
        FileSystem fs = path.getFileSystem(LoadMetadataUtil.getConf());
        FileStatus[] fileStatus = fs.listStatus(path);
        if (fs.exists(path)) {
            for (FileStatus status : fileStatus) {
                BlockLocation[] locations = fs.getFileBlockLocations(status, 0, status.getLen());
                for (BlockLocation loc : locations) {
                    loc.getNames();
                    loc.getHosts();
                }
            }
        }
    } catch (IOException e) {
        exceptionThrown = true;
        LOG.error("Failed to list Status", e);
    }
    assertFalse(exceptionThrown);
}

From source file:com.cloudera.impala.catalog.TestLoadHdfsMetadataPerf.java

License:Apache License

/**
 * List file status by calling abstractFileSystem.listStatusIterator.
 *//*from ww  w  .  jav  a  2 s .c o m*/
private static void listStatusIterator(String dirPath) {
    Path path = new Path(dirPath);
    boolean exceptionThrown = false;
    try {
        AbstractFileSystem fs = AbstractFileSystem.createFileSystem(path.toUri(), LoadMetadataUtil.getConf());
        RemoteIterator<FileStatus> iter = fs.listStatusIterator(path);
        while (iter.hasNext()) {
            FileStatus fileStatus = iter.next();
            BlockLocation[] locations = fs.getFileBlockLocations(fileStatus.getPath(), 0, fileStatus.getLen());
            for (BlockLocation loc : locations) {
                loc.getNames();
                loc.getHosts();
            }
        }
    } catch (IOException e) {
        exceptionThrown = true;
        LOG.error("Failed to list Status Iterator", e);
    }
    assertFalse(exceptionThrown);
}

From source file:com.cloudera.impala.catalog.TestLoadHdfsMetadataPerf.java

License:Apache License

/**
 * List file status by calling fileSystem.listLocatedStatus.
 *//*from w  w w . j  ava2  s. c o  m*/
private static void listLocatedStatus(String dirPath) {
    Path path = new Path(dirPath);
    boolean exceptionThrown = false;
    try {
        FileSystem fs = path.getFileSystem(LoadMetadataUtil.getConf());
        RemoteIterator<LocatedFileStatus> iterator = fs.listLocatedStatus(path);
        if (fs.exists(path)) {
            while (iterator.hasNext()) {
                LocatedFileStatus fileStatus = iterator.next();
                BlockLocation[] locations = fileStatus.getBlockLocations();
                for (BlockLocation loc : locations) {
                    loc.getHosts();
                    loc.getNames();
                }
            }
        }
    } catch (IOException e) {
        exceptionThrown = true;
        LOG.error("Failed to list Located Status", e);
    }
    assertFalse(exceptionThrown);
}

From source file:com.cloudera.impala.util.LoadMetadataUtil.java

License:Apache License

/**
 * Create FileBlock according to BlockLocation and hostIndex. Get host names and ports
 * from BlockLocation, and get all replicas' host id from hostIndex.
 *
 * Must be threadsafe. Access to 'hostIndex' must be protected.
 *//*from  w  w w .j  a va2 s.  c  om*/
private static FileBlock createFileBlock(BlockLocation loc, ListMap<TNetworkAddress> hostIndex)
        throws IOException {
    // Get the location of all block replicas in ip:port format.
    String[] blockHostPorts = loc.getNames();
    // Get the hostnames for all block replicas. Used to resolve which hosts
    // contain cached data. The results are returned in the same order as
    // block.getNames() so it allows us to match a host specified as ip:port to
    // corresponding hostname using the same array index.
    String[] blockHostNames = loc.getHosts();
    Preconditions.checkState(blockHostNames.length == blockHostPorts.length);
    // Get the hostnames that contain cached replicas of this block.
    Set<String> cachedHosts = Sets.newHashSet(Arrays.asList(loc.getCachedHosts()));
    Preconditions.checkState(cachedHosts.size() <= blockHostNames.length);

    // Now enumerate all replicas of the block, adding any unknown hosts
    // to hostMap_/hostList_. The host ID (index in to the hostList_) for each
    // replica is stored in replicaHostIdxs.
    List<BlockReplica> replicas = Lists.newArrayListWithExpectedSize(blockHostPorts.length);
    for (int i = 0; i < blockHostPorts.length; ++i) {
        TNetworkAddress networkAddress = BlockReplica.parseLocation(blockHostPorts[i]);
        Preconditions.checkState(networkAddress != null);
        networkAddress.setHdfs_host_name(blockHostNames[i]);
        int idx = -1;
        synchronized (hostIndex) {
            idx = hostIndex.getIndex(networkAddress);
        }
        replicas.add(new BlockReplica(idx, cachedHosts.contains(blockHostNames[i])));
    }
    return new FileBlock(loc.getOffset(), loc.getLength(), replicas);
}

From source file:com.cloudera.kitten.appmaster.util.HDFSFileFinder.java

License:Open Source License

public static Map<String, Long> getNumBytesOfGlobHeldByDatanodes(Path p, Configuration conf)
        throws IOException {
    FileSystem fs = p.getFileSystem(conf);

    HashMap<String, Long> bytesHeld = Maps.newHashMap();
    for (FileStatus f : fs.globStatus(p)) {
        BlockLocation[] bls = fs.getFileBlockLocations(p, 0, f.getLen());
        if (bls.length > 0) {
            for (BlockLocation bl : bls) {
                long l = bl.getLength();
                for (String name : bl.getNames()) {
                    if (bytesHeld.containsKey(name))
                        bytesHeld.put(name, bytesHeld.get(name) + l);
                    else
                        bytesHeld.put(name, l);
                }//from  w ww  . ja  v a  2  s.  c o m
            }
        }
    }

    return bytesHeld;
}

From source file:hsyndicate.tools.BlockLocations.java

License:Apache License

public static void main(String[] args) throws Exception {
    Path p = new Path(args[0]);
    Configuration conf = new Configuration();
    FileSystem fs = p.getFileSystem(conf);
    FileStatus f = fs.getFileStatus(p);//from  w  w w. j a v  a2s . c  o m
    BlockLocation[] bla = fs.getFileBlockLocations(f, 0, f.getLen());

    System.out.println("File : " + f.getPath().toString());
    for (BlockLocation bl : bla) {
        System.out.println("Offset : " + bl.getOffset());
        System.out.println("Len : " + bl.getLength());
        System.out.println("Hosts : " + makeCommaSeparated(bl.getHosts()));
        System.out.println("Names : " + makeCommaSeparated(bl.getNames()));
        System.out.println("TopologyPaths : " + makeCommaSeparated(bl.getTopologyPaths()));
    }
}

From source file:io.hops.erasure_coding.TestErasureCodingManager.java

License:Apache License

@Test
public void testLateEncoding() throws IOException {
    Util.createRandomFile(dfs, testFile, seed, TEST_BLOCK_COUNT, DFS_TEST_BLOCK_SIZE);
    EncodingPolicy policy = new EncodingPolicy("src", (short) 1);
    dfs.encodeFile(testFile.toUri().getPath(), policy);

    EncodingStatus encodingStatus;/*  www  .  j  a  v  a2 s  .  c o m*/
    while (!(encodingStatus = dfs.getEncodingStatus(testFile.toUri().getPath())).isEncoded()) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            LOG.error("Wait for encoding thread was interrupted.");
        }
    }

    FileStatus fileStatus = dfs.getFileStatus(testFile);
    Path parityFile = new Path(conf.get(DFSConfigKeys.PARITY_FOLDER, DFSConfigKeys.DEFAULT_PARITY_FOLDER),
            encodingStatus.getParityFileName());
    FileStatus parityStatus = dfs.getFileStatus(parityFile);
    BlockLocation[] blockLocations = dfs.getFileBlockLocations(fileStatus, 0,
            TEST_STRIPE_LENGTH * DFS_TEST_BLOCK_SIZE);
    BlockLocation[] parityBlockLocations = dfs.getFileBlockLocations(parityStatus, 0,
            TEST_PARITY_LENGTH * DFS_TEST_BLOCK_SIZE);

    Set<String> set = new HashSet<String>();
    for (BlockLocation blockLocation : blockLocations) {
        String host = blockLocation.getNames()[0];
        if (set.contains(host)) {
            fail("Duplicated location " + Arrays.toString(blockLocation.getNames()));
        }
        set.add(host);
    }
    for (BlockLocation blockLocation : parityBlockLocations) {
        String host = blockLocation.getNames()[0];
        if (set.contains(host)) {
            fail("Duplicated location " + Arrays.toString(blockLocation.getNames()));
        }
        set.add(host);
    }
}