Hadoop的hdfs api操作
阿新 • • 發佈:2018-12-10
public static void listFile(String path) throws IOException{ //讀取配置檔案 Configuration conf = new Configuration(); //獲取檔案系統 FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf); //獲取檔案或目錄狀態 FileStatus[] fileStatus = fs.listStatus(new Path(path)); //列印檔案的路徑 for (FileStatus file : fileStatus) { System.out.println(file.getPath()); } //關閉檔案系統 fs.close(); }
一、包依賴
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.6</version>
</dependency>
二、API的操作
1.建立目錄
public static void mkdir(String path) throws IOException { Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://127.0.0.1:9000"); FileSystem fs = FileSystem.get(conf); Path srcPath = new Path(path); boolean isok = fs.mkdirs(srcPath); if (isok) { System.out.println("create dir ok!"); } else { System.out.println("create dir failure"); } fs.close(); }
2.刪除目錄
/** * 刪除目錄 * @param path */ public static void rmdir(String path)throws Exception { Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(URI.create("hdfs://127.0.0.1:9000"), configuration); boolean flag = fs.deleteOnExit(new Path("/test")); if(flag) { System.out.println("delete ok!"); }else { System.out.println("delete failure"); } //關閉檔案系統 fs.close(); }
3.建立檔案
public static void createFile(String dst , byte[] contents) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
Path dstPath = new Path(dst);
FSDataOutputStream outputStream = fs.create(dstPath);
outputStream.write(contents);
outputStream.close();
fs.close();
System.out.println("檔案建立成功!");
}
4.讀取檔案內容
public static void readFile(String uri) throws IOException {
//讀取配置檔案
Configuration conf = new Configuration();
//獲取檔案系統
FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
InputStream in = null;
try {
in = fs.open(new Path(uri));
//複製到標準輸出流
IOUtils.copyBytes(in, System.out, 4096,false);
} catch (Exception e) {
e.printStackTrace();
}finally{
IOUtils.closeStream(in);
}
}
5.檢視檔案目錄
public static void listFile(String path) throws IOException{
//讀取配置檔案
Configuration conf = new Configuration();
//獲取檔案系統
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);
//獲取檔案或目錄狀態
FileStatus[] fileStatus = fs.listStatus(new Path(path));
//列印檔案的路徑
for (FileStatus file : fileStatus) {
System.out.println(file.getPath());
}
//關閉檔案系統
fs.close();
}
其它操作檢視應的FileSystem的api