public static void readFile(String uri) { Configuration configuration = new Configuration(); InputStream inputStream = null; try { // String uri = "hdfs://hadoop:9000/hadoop/temp/1901"; FileSystem fileSystem = FileSystem.get(URI.create(uri), configuration); inputStream = fileSystem.open(new Path(uri)); IOUtils.copyBytes(inputStream, System.out, 4096, false); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeStream(inputStream); } }
public static void readFileByFSDATAInputStream(String uri) { Configuration configuration = new Configuration(); FSDataInputStream stream = null; try { FileSystem fileSystem = FileSystem.get(URI.create(uri), configuration); stream = fileSystem.open(new Path(uri)); IOUtils.copyBytes(stream, System.out, 4096, false); stream.seek(0); IOUtils.copyBytes(stream, System.out, 4096, false); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeStream(stream); } }
public static void writeData(String uri, String local) { try { InputStream inputStream = new BufferedInputStream(new FileInputStream(local)); FileSystem fileSystem = FileSystem.get(configuration); // 建立一個新文件 必須保證uri不存在 若是向一個已經存在的文件添加內容,使用fileSystem.append(Path p) //fileSystem.exists(new Path("")); //判斷文件是否存在 //fileSystem.mkdirs(new Path("")); //建立文件夾 FSDataOutputStream outputStream = fileSystem.create(new Path(uri), new Progressable() { // 報告寫入過程 @Override public void progress() { System.out.print("."); } }); IOUtils.copyBytes(inputStream, outputStream, 4096, true); } catch (IOException e) { e.printStackTrace(); } }
public static void listFiles(String uri) { try { FileSystem fileSystem = FileSystem.get(configuration); // FileStatus fileStatus = fileSystem.getFileStatus(new Path(uri)); FileStatus[] statuses = fileSystem.listStatus(new Path(uri)); Path[] listPaths = FileUtil.stat2Paths(statuses); for (Path path : listPaths){ System.out.println(path); } // fileSystem.listStatus(Path[] files); //批量讀取 } catch (IOException e) { e.printStackTrace(); } }
public static void deleteFile(String uri){ try { FileSystem fileSystem = FileSystem.get(configuration); fileSystem.delete(new Path(uri),true); } catch (IOException e) { e.printStackTrace(); } }
/** Delete a file. * * @param f the path to delete. * @param recursive if path is a directory and set to * true, the directory is deleted else throws an exception. In * case of a file the recursive can be set to either true or false. * @return true if delete is successful else false. * @throws IOException */ public abstract boolean delete(Path f, boolean recursive) throws IOException;
若是刪除文件夾須要 設置爲true,若是未flase會拋出異常。app