接上篇:https://developer.aliyun.com/article/1621726?spm=a2c6h.13148508.setting.20.49764f0eWaXqej
展示列表
public static void listList() throws IOException { Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", "hdfs://h121.wzk.icu:9000"); FileSystem fileSystem = FileSystem.get(configuration); RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true); while (listFiles.hasNext()) { LocatedFileStatus status = listFiles.next(); System.out.println("文件名字: " + status.getPath().getName()); System.out.println("文件长度: " + status.getLen()); System.out.println("文件块大小: " + status.getBlockSize()); System.out.println("权限: " + status.getPermission()); System.out.println("分组: " + status.getGroup()); BlockLocation[] blockLocations = status.getBlockLocations(); for (BlockLocation blockLocation : blockLocations) { String[] hosts = blockLocation.getHosts(); for (String host : hosts) { System.out.println(host); } } System.out.println("=========================="); } fileSystem.close(); }
扫描路径
public static void listStatus() throws IOException { Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", "hdfs://h121.wzk.icu:9000"); FileSystem fileSystem = FileSystem.get(configuration); FileStatus[] listStatus = fileSystem.listStatus(new Path("/")); for (FileStatus fileStatus : listStatus) { if (fileStatus.isFile()) { System.out.println("文件: " + fileStatus.getPath().getName()); } else { System.out.println("文件夹: " + fileStatus.getPath().getName()); } } fileSystem.close(); }
PUT 操作
public static void putFile() throws IOException { Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", "hdfs://h121.wzk.icu:9000"); FileSystem fileSystem = FileSystem.get(configuration); try (FileInputStream fis= new FileInputStream("wzk02.txt")) { FSDataOutputStream fos = fileSystem.create(new Path("/wzk02_io.txt")); IOUtils.copyBytes(fis, fos, configuration); IOUtils.closeStream(fos); IOUtils.closeStream(fis); fileSystem.close(); } }
GET 操作
public static void getFile() throws IOException { Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", "hdfs://h121.wzk.icu:9000"); FileSystem fileSystem = FileSystem.get(configuration); try (FileOutputStream fos = new FileOutputStream("wzk02_io_get.txt")) { FSDataInputStream fis = fileSystem.open(new Path("/wzk02_io.txt")); IOUtils.copyBytes(fis, fos, configuration); IOUtils.closeStream(fos); IOUtils.closeStream(fis); fileSystem.close(); } }
Seek操作
public static void seek() throws IOException { Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", "hdfs://h121.wzk.icu:9000"); FileSystem fileSystem = FileSystem.get(configuration); FSDataInputStream in = null; try { in = fileSystem.open(new Path("/wzk02_io.txt")); IOUtils.copyBytes(in, System.out, 4096, false); in.seek(0); IOUtils.copyBytes(in, System.out, 4096, false); } finally { IOUtils.closeStream(in); } }
进度显示
public static void uploadProgress() throws IOException { Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", "hdfs://h121.wzk.icu:9000"); FileSystem fileSystem = FileSystem.get(configuration); try (FileInputStream fis = new FileInputStream("music.mp3")) { FSDataOutputStream fos = fileSystem.create(new Path("/wzk/music.mp3"), () -> System.out.print("=")); IOUtils.copyBytes(fis, fos, configuration); IOUtils.closeStream(fos); IOUtils.closeStream(fis); fileSystem.close(); System.out.println("done!"); } }