hadoop學習筆記--5.HDFS的java api接口訪問
一:幾個常用類介紹
(1):configuration類:此類封裝了客戶端或服務器的配置,通過配置文件來讀取類路徑實現(一般是core-site.xml)。
(2):FileSystem類:一個通用的文件系統api,用該對象的一些方法來對文件進行操作。
FileSystem fs = FileSystem.get(conf);通過FileSystem的靜態方法get獲得該對象。
(3):FSDataInputStream:HDFS的文件輸入流,FileSystem.open()方法返回的即是此類。
(4):FSDataOutputStream:HDFS的文件輸入出流,FileSystem.create()方法返回的即是此類。
二:創建文件目錄
public static void mkdir(String path) throws IOException {
//讀取配置文件
Configuration conf = new Configuration();
//獲取文件系統
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);
Path srcPath = new Path(path);
//調用mkdir()創建目錄,(可以一次性創建,以及不存在的父目錄)
boolean flag = fs.mkdirs(srcPath);
if(flag) {
System.out.println("create dir ok!");
}else {
System.out.println("create dir failure");
}
//關閉文件系統
fs.close();
}
三:刪除文件/目錄
/*** 刪除文件或者文件目錄
* @throws IOException **/
public static void rmdir(String filePath) throws IOException {
//讀取配置文件
Configuration conf = new Configuration();
//獲取文件系統
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);
Path path = new Path(filePath);
//調用deleteOnExit()
boolean flag = fs.deleteOnExit(path);
// fs.delete(path);
if(flag) {
System.out.println("delete ok!");
}else {
System.out.println("delete failure");
}
//關閉文件系統
fs.close();
}
四:創建文件
/**創建文件**/
public static void createFile(String dst , byte[] contents) throws IOException{
//讀取配置文件
Configuration conf = new Configuration();
//獲取文件系統
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);
//目標路徑
Path dstPath = new Path(dst);
//打開一個輸出流
FSDataOutputStream outputStream = fs.create(dstPath);
outputStream.write(contents);
//關閉文件系統
outputStream.close();
fs.close();
System.out.println("文件創建成功!");
}
五:列出目錄下的文件
/**列出文件**/
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();
}
六:上傳本地文件
/**上傳本地文件**/
public static void uploadFile(String src,String dst) throws IOException{
//讀取配置文件
Configuration conf = new Configuration();
//獲取文件系統
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);
Path srcPath = new Path(src); //原路徑
Path dstPath = new Path(dst); //目標路徑
//調用文件系統的文件複製函數,前面參數是指是否刪除原文件,true爲刪除,默認爲false
fs.copyFromLocalFile(false,srcPath, dstPath);
//打印文件路徑
System.out.println("Upload to "+conf.get("fs.default.name"));
System.out.println("------------list files------------"+"\n");
FileStatus [] fileStatus = fs.listStatus(dstPath);
for (FileStatus file : fileStatus) {
System.out.println(file.getPath());
}
//關閉文件系統
fs.close();
}
七:文件重命名
/**文件重命名**/
public static void renameFile(String oldName,String newName) throws IOException{
//讀取配置文件
Configuration conf = new Configuration();
//獲取文件系統
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);
Path oldPath = new Path(oldName);
Path newPath = new Path(newName);
boolean flag = fs.rename(oldPath, newPath);
if(flag) {
System.out.println("rename ok!");
}else {
System.out.println("rename failure");
}
//關閉文件系統
fs.close();
}
八:讀取文件內容
public static void readFile(String uri) throws IOException {
//讀取配置文件
Configuration conf = new Configuration();
//獲取文件系統
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1: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);
}
}
九:判斷目錄是否存在
//判斷目錄是否存在
public static boolean existDir(String filePath,boolean create) {
boolean flag = false;
//判斷是否存在
if(StringUtils.isEmpty(filePath)) {
return flag;
}
Path path = new Path(filePath);
//讀取配置文件
Configuration conf = new Configuration();
try {
//獲取文件系統
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);
//或者create爲true
if(create) {
//如果文件不存在
if(!fs.exists(path)) {
fs.mkdirs(path);
}
}
//判斷是否爲目錄
if(fs.isDirectory(path)) {
flag = true;
}
}catch (Exception e){
e.printStackTrace();
}
return flag;
}
十:追加到文件末尾
/**添加到文件的末尾(src爲本地地址,dst爲hdfs文件地址)
* @throws IOException */
public static void appendFile(String src,String dst) throws IOException {
//讀取配置文件
Configuration conf = new Configuration();
//獲取文件系統
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);
Path dstPath = new Path(dst);
//創建需要寫入的文件流
InputStream in = new BufferedInputStream(new FileInputStream(src));
//文件輸出流寫入
FSDataOutputStream out = fs.append(dstPath);
IOUtils.copyBytes(in, out, 4096,true);
fs.close();
}
十一:主函數
public class HdfsJavaApi {
public static void main(String[] args) throws IOException {
//讀取文件內容
//readFile(args[0]);
//創建文件目錄
/*String s= "hello";
byte[] bytes = s.getBytes();
createFile("/liu/h.txt",bytes);*/
//刪除文件
/*rmdir("/liu2");*/
//上傳文件
/*uploadFile("/home/liu/hello.text", "/liu/hello.text");*/
//列出文件
/*listFile("/liu");*/
//文科重命名
/*renameFile("/liu/hi.txt", "/liu/he1.text");*/
//查詢目錄是否存在
/*boolean existDir = existDir("/liu2", false);
System.out.println(existDir);*/
//寫入文件末尾
appendFile("/home/liu/hello.text","/liu1/hello.text");
}
十二:注意要點
1: FileSystem的get()方法有兩個。
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf); //默認在hdfs上讀取文件
FileSystem fs = FileSystem.get(conf); //默認從本地上讀取文件
hdfs://hadoop1:9000需要與core-site.xml配置文件一致。也可以寫成URI.create(uri),不過此種寫法在文件路徑最頭處一定要加上hdfs://hadoop1:9000。
2:參數從main函數中的args中獲得,Eclipse中選擇帶參運行