public class HdfsClient { FileSystem fs = null; @Before public void init() throws Exception { Configuration conf = new Configuration(); // conf.set("fs.defaultFS","hdfs://mini01:9000"); fs = FileSystem.get(new URI("hdfs://mini01:9000"), conf, "root"); } @Test public void testUpload() throws IOException { fs.copyFromLocalFile(new Path("/home/lzq/250.txt"), new Path("/")); fs.close(); } @Test public void testDownload() throws IOException { fs.copyToLocalFile(new Path("/250.txt"), new Path("/home/lzq/Test")); fs.close(); } @Test public void testMkdir() throws IOException { boolean mkdir = fs.mkdirs(new Path("/home/lzq")); System.out.println(mkdir); } @Test public void deletetest() throws IOException { boolean b = fs.delete(new Path("/home/lzq"), true); System.out.println(b); } public void testLs() throws IOException { //返回是迭代器,而不是list因为list等占内存,那么多数据内存一下就爆掉了,所以返回是 //iterator,每次next返回一个值,类是与py yield generator RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fs.listFiles(new Path("/"), true); while (locatedFileStatusRemoteIterator.hasNext()) { LocatedFileStatus file = locatedFileStatusRemoteIterator.next(); System.out.println(file.getAccessTime()); System.out.println(file.getOwner()); System.out.println(file.getReplication()); System.out.println(file.getBlockLocations()); BlockLocation[] blockLocations = file.getBlockLocations(); // 一个文件可能会有多个块,所以是数组 for (BlockLocation bl : blockLocations) { System.out.println(bl.getOffset()); System.out.println(bl.getLength()); // 一个快在多个主机上 String[] hosts = bl.getHosts(); for (String host : hosts) { System.out.println(host); } } } } public void testl() throws IOException { FileStatus[] fileStatuses = fs.listStatus(new Path("/")); // 返回目录 for (FileStatus file : fileStatuses) { System.out.println(); } } }
hdfs client api
最新推荐文章于 2024-04-04 15:32:00 发布