DistributedFileSystem dfs = new DistributedFileSystem(); dfs.initialize(new URI("URI to HDFS"), new Configuration()); Path path = new Path("/user/hadoop-user/bar.txt"); if (!dfs.exists(path)) dfs.createNewFile(path); FSDataOutputStream dos = dfs.create(path);
@Test public void testFsImageCorruption() throws Exception { final Path st = new Path("/st"); final Path nonst = new Path("/nonst"); final Path stY = new Path(st, "y"); final Path nonstTrash = new Path(nonst, "trash"); hdfs.mkdirs(stY); hdfs.allowSnapshot(st); hdfs.createSnapshot(st, "s0"); Path f = new Path(stY, "nn.log"); hdfs.createNewFile(f); hdfs.createSnapshot(st, "s1"); Path f2 = new Path(stY, "nn2.log"); hdfs.rename(f, f2); hdfs.createSnapshot(st, "s2"); Path trashSt = new Path(nonstTrash, "st"); hdfs.mkdirs(trashSt); hdfs.rename(stY, trashSt); hdfs.delete(nonstTrash, true); hdfs.deleteSnapshot(st, "s1"); hdfs.deleteSnapshot(st, "s2"); hdfs.setSafeMode(SafeModeAction.SAFEMODE_ENTER); hdfs.saveNamespace(); hdfs.setSafeMode(SafeModeAction.SAFEMODE_LEAVE); cluster.restartNameNodes(); }
@Test public void testSetRepWithStoragePolicyOnEmptyFile() throws Exception { Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build(); DistributedFileSystem dfs = cluster.getFileSystem(); try { Path d = new Path("/tmp"); dfs.mkdirs(d); dfs.setStoragePolicy(d, "HOT"); Path f = new Path(d, "foo"); dfs.createNewFile(f); dfs.setReplication(f, (short) 4); } finally { dfs.close(); cluster.shutdown(); } } }
Path dir = new Path(userDir, "/dir"); Path link = new Path(userDir, "/link"); hdfs.createNewFile(file); hdfs.mkdirs(dir); hdfs.createSymlink(file, link, false);