private InputStream OpenMultiplePartsWithOffset(FileSystem fs, Path pt, long offset) throws IOException { RemoteIterator<LocatedFileStatus> rit = fs.listFiles(pt, false); Vector<FSDataInputStream> fileHandleList = new Vector<FSDataInputStream>(); while (rit.hasNext()) { Path path = rit.next().getPath(); String filename = path.toString().substring(path.getParent().toString().length(), path.toString().length()); if (filename.startsWith("/part-")) { long filesize = fs.getFileStatus(path).getLen(); if (offset < filesize) { FSDataInputStream handle = fs.open(path); if (offset > 0) { handle.seek(offset); } fileHandleList.add(handle); } offset -= filesize; } } if (fileHandleList.size() == 1) return fileHandleList.get(0); else if (fileHandleList.size() > 1) { Enumeration<FSDataInputStream> enu = fileHandleList.elements(); return new SequenceInputStream(enu); } else { System.err.println("Error, no source file loaded. run genSeedDataset.sh fisrt!"); return null; } }
@Test public void testListStatus() throws Exception { final String hPrefix = "test/hadoop"; final String[] dirs = { hPrefix + "/a", hPrefix + "/b", hPrefix + "/c", hPrefix + "/1", hPrefix + "/#@#@", hPrefix + "/&*#$#$@234" }; ArrayList<Path> testDirs = new ArrayList<Path>(); for (String d : dirs) { testDirs.add(qualifiedPath(d, fc2)); } Assert.assertFalse(exists(fc1, testDirs.get(0))); for (Path path : testDirs) { fc1.mkdir(path, FsPermission.getDefault(), true); } // test listStatus that returns an array of FileStatus FileStatus[] paths = fc1.util().listStatus(qualifiedPath("test", fc1)); Assert.assertEquals(1, paths.length); Assert.assertEquals(qualifiedPath(hPrefix, fc1), paths[0].getPath()); paths = fc1.util().listStatus(qualifiedPath(hPrefix, fc1)); Assert.assertEquals(6, paths.length); for (int i = 0; i < dirs.length; i++) { boolean found = false; for (int j = 0; j < paths.length; j++) { if (qualifiedPath(dirs[i], fc1).equals(paths[j].getPath())) { found = true; } } Assert.assertTrue(dirs[i] + " not found", found); } paths = fc1.util().listStatus(qualifiedPath(dirs[0], fc1)); Assert.assertEquals(0, paths.length); // test listStatus that returns an iterator of FileStatus RemoteIterator<FileStatus> pathsItor = fc1.listStatus(qualifiedPath("test", fc1)); Assert.assertEquals(qualifiedPath(hPrefix, fc1), pathsItor.next().getPath()); Assert.assertFalse(pathsItor.hasNext()); pathsItor = fc1.listStatus(qualifiedPath(hPrefix, fc1)); int dirLen = 0; for (; pathsItor.hasNext(); dirLen++) { boolean found = false; FileStatus stat = pathsItor.next(); for (int j = 0; j < dirs.length; j++) { if (qualifiedPath(dirs[j], fc1).equals(stat.getPath())) { found = true; break; } } Assert.assertTrue(stat.getPath() + " not found", found); } Assert.assertEquals(6, dirLen); pathsItor = fc1.listStatus(qualifiedPath(dirs[0], fc1)); Assert.assertFalse(pathsItor.hasNext()); }