DFSAdmin extends FsShell { public int printTopology() throws IOException { DistributedFileSystem dfs = getDFS(); final DatanodeInfo[] report = dfs.getDataNodeStats(); HashMap<String, TreeSet<String> > tree = new HashMap<String, TreeSet<String>>(); for(DatanodeInfo dni : report) { String location = dni.getNetworkLocation(); String name = dni.getName(); if(!tree.containsKey(location)) { tree.put(location, new TreeSet<String>()); } tree.get(location).add(name); } ArrayList<String> racks = new ArrayList<String>(tree.keySet()); Collections.sort(racks); for(String r : racks) { System.out.println("Rack: " + r); TreeSet<String> nodes = tree.get(r); for(String n : nodes) { System.out.print(" " + n); String hostname = NetUtils.getHostNameOfIP(n); if(hostname != null) System.out.print(" (" + hostname + ")"); System.out.println(); } System.out.println(); } return 0; } DFSAdmin();  DFSAdmin(Configuration conf); void report(String[] argv, int i); void setSafeMode(String[] argv, int idx); int triggerBlockReport(String[] argv); void allowSnapshot(String[] argv); void disallowSnapshot(String[] argv); void provisionSnapshotTrash(String[] argv); int saveNamespace(String[] argv); int rollEdits(); int restoreFailedStorage(String arg); int refreshNodes(); int listOpenFiles(String[] argv); int setBalancerBandwidth(String[] argv, int idx); int getBalancerBandwidth(String[] argv, int idx); int fetchImage(final String[] argv, final int idx); int finalizeUpgrade(); int getUpgradeStatus(); int upgrade(String arg); int metaSave(String[] argv, int idx); int printTopology(); int refreshServiceAcl(); int refreshUserToGroupsMappings(); int refreshSuperUserGroupsConfiguration(); int refreshCallQueue(); int reconfig(String[] argv, int i); int genericRefresh(String[] argv, int i); @Override int run(String[] argv); static void main(String[] argv);  }
@Test(timeout = 30000) public void testPrintTopology() throws Exception { redirectStream(); final Configuration dfsConf = new HdfsConfiguration(); final File baseDir = new File( PathUtils.getTestDir(getClass()), GenericTestUtils.getMethodName()); dfsConf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, baseDir.getAbsolutePath()); final int numDn = 4; final String[] racks = { "/d1/r1", "/d1/r2", "/d2/r1", "/d2/r2"}; try (MiniDFSCluster miniCluster = new MiniDFSCluster.Builder(dfsConf) .numDataNodes(numDn).racks(racks).build()) { miniCluster.waitActive(); assertEquals(numDn, miniCluster.getDataNodes().size()); final DFSAdmin dfsAdmin = new DFSAdmin(dfsConf); resetStream(); final int ret = ToolRunner.run(dfsAdmin, new String[] {"-printTopology"}); final List<String> outs = Lists.newArrayList(); scanIntoList(out, outs); assertEquals(0, ret); assertEquals( "There should be three lines per Datanode: the 1st line is" + " rack info, 2nd node info, 3rd empty line. The total" + " should be as a result of 3 * numDn.", 12, outs.size()); assertThat(outs.get(0), is(allOf(containsString("Rack:"), containsString("/d1/r1")))); assertThat(outs.get(3), is(allOf(containsString("Rack:"), containsString("/d1/r2")))); assertThat(outs.get(6), is(allOf(containsString("Rack:"), containsString("/d2/r1")))); assertThat(outs.get(9), is(allOf(containsString("Rack:"), containsString("/d2/r2")))); } }