org.apache.hadoop.fs.BlockAndLocation

Here are the examples of the java api class org.apache.hadoop.fs.BlockAndLocation taken from open source projects.

1. DFSUtil#locatedBlocks2BlockLocations()

Project: hadoop-20
Source File: DFSUtil.java
View license
/**
  * Convert a LocatedBlocks to BlockAndLocations[]
  * @param blocks a LocatedBlocks
  * @return an array of BlockLocations
  */
public static BlockAndLocation[] locatedBlocks2BlockLocations(LocatedBlocks blocks) {
    if (blocks == null) {
        return new BlockAndLocation[0];
    }
    int nrBlocks = blocks.locatedBlockCount();
    BlockAndLocation[] blkLocations = new BlockAndLocation[nrBlocks];
    if (nrBlocks == 0) {
        return blkLocations;
    }
    int idx = 0;
    for (LocatedBlock blk : blocks.getLocatedBlocks()) {
        assert idx < nrBlocks : "Incorrect index";
        DatanodeInfo[] locations = blk.getLocations();
        String[] hosts = new String[locations.length];
        String[] names = new String[locations.length];
        String[] racks = new String[locations.length];
        for (int hCnt = 0; hCnt < locations.length; hCnt++) {
            hosts[hCnt] = locations[hCnt].getHostName();
            names[hCnt] = locations[hCnt].getName();
            NodeBase node = new NodeBase(names[hCnt], locations[hCnt].getNetworkLocation());
            racks[hCnt] = node.toString();
        }
        Block block = blk.getBlock();
        blkLocations[idx] = new BlockAndLocation(block.getBlockId(), block.getGenerationStamp(), names, hosts, racks, blk.getStartOffset(), block.getNumBytes(), blk.isCorrupt());
        idx++;
    }
    return blkLocations;
}

2. TestLocatedBlockStatus#testFile()

View license
/** Test when input path is a file */
@Test
public void testFile() throws IOException {
    fs.mkdirs(TEST_DIR);
    writeFile(fs, FILE1, FILE_LEN);
    RemoteIterator<LocatedBlockFileStatus> itor = fs.listLocatedBlockStatus(FILE1);
    LocatedBlockFileStatus stat = itor.next();
    assertFalse(itor.hasNext());
    // check file status
    assertFalse(stat.isDir());
    assertEquals(FILE_LEN, stat.getLen());
    assertEquals(fs.makeQualified(FILE1), stat.getPath());
    assertEquals(-1, stat.getChildrenCount());
    // check block and locations
    BlockAndLocation[] locations = stat.getBlockLocations();
    assertEquals(2, locations.length);
    assertEquals(BLOCK_LEN, locations[0].getLength());
    assertEquals(0L, locations[0].getOffset());
    String[] hostNames = locations[0].getNames();
    assertEquals(3, hostNames.length);
    for (String hostName : hostNames) {
        assertTrue("Hostname is " + hostName, hostName.startsWith("127.0.0.1:"));
    }
    assertEquals(FILE_LEN - BLOCK_LEN, locations[1].getLength());
    assertEquals(BLOCK_LEN, locations[1].getOffset());
    fs.delete(FILE1, true);
}