HDFS中JAVA API的使用
HDFS是一個分布式文件系統(tǒng),既然是文件系統(tǒng),就可以對其文件進行操作,比如說新建文件、刪除文件、讀取文件內(nèi)容等操作。下面記錄一下使用JAVA API對HDFS中的文件進行操作的過程。
對分HDFS中的文件操作主要涉及一下幾個類:
Configuration類:該類的對象封轉(zhuǎn)了客戶端或者服務(wù)器的配置。
FileSystem類:該類的對象是一個文件系統(tǒng)對象,可以用該對象的一些方法來對文件進行操作。FileSystem fs = FileSystem.get(conf);通過FileSystem的靜態(tài)方法get獲得該對象。
FSDataInputStream和FSDataOutputStream:這兩個類是HDFS中的輸入輸出流。分別通過FileSystem的open方法和create方法獲得。
具體如何對文件操作清下下面例子:
package com.hdfs;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class HdfsTest {
//創(chuàng)建新文件
public static void createFile(String dst , byte[] contents) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path dstPath = new Path(dst); //目標(biāo)路徑
//打開一個輸出流
FSDataOutputStream outputStream = fs.create(dstPath);
outputStream.write(contents);
outputStream.close();
fs.close();
System.out.println("文件創(chuàng)建成功!");
}
//上傳本地文件
public static void uploadFile(String src,String dst) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path(src); //原路徑
Path dstPath = new Path(dst); //目標(biāo)路徑
//調(diào)用文件系統(tǒng)的文件復(fù)制函數(shù),前面參數(shù)是指是否刪除原文件,true為刪除,默認(rèn)為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 rename(String oldName,String newName) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path oldPath = new Path(oldName);
Path newPath = new Path(newName);
boolean isok = fs.rename(oldPath, newPath);
if(isok){
System.out.println("rename ok!");
}else{
System.out.println("rename failure");
}
fs.close();
}
//刪除文件
public static void delete(String filePath) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path path = new Path(filePath);
boolean isok = fs.deleteOnExit(path);
if(isok){
System.out.println("delete ok!");
}else{
System.out.println("delete failure");
}
fs.close();
}
//創(chuàng)建目錄
public static void mkdir(String path) throws IOException{
Configuration conf = new Configuration();
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();
}
//讀取文件的內(nèi)容
public static void readFile(String filePath) throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path(filePath);
InputStream in = null;
try {
in = fs.open(srcPath);
IOUtils.copyBytes(in, System.out, 4096, false); //復(fù)制到標(biāo)準(zhǔn)輸出流
} finally {
IOUtils.closeStream(in);
}
}
public static void main(String[] args) throws IOException {
//測試上傳文件
//uploadFile("D:\\c.txt", "/user/hadoop/test/");
//測試創(chuàng)建文件
/*byte[] contents = "hello world 世界你好\n".getBytes();
createFile("/user/hadoop/test1/d.txt",contents);*/
//測試重命名
//rename("/user/hadoop/test/d.txt", "/user/hadoop/test/dd.txt");
//測試刪除文件
//delete("test/dd.txt"); //使用相對路徑
//delete("test1"); //刪除目錄
//測試新建目錄
//mkdir("test1");
//測試讀取文件
readFile("test1/d.txt");
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用JavaBean根據(jù)指定條件設(shè)置屬性值默認(rèn)值方式
這篇文章主要介紹了使用JavaBean根據(jù)指定條件設(shè)置屬性值默認(rèn)值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Springboot四種事件監(jiān)聽的實現(xiàn)方式詳解
這篇文章主要介紹了Springboot四種事件監(jiān)聽的實現(xiàn)方式,事件監(jiān)聽是一種機制,可以定義和觸發(fā)自定義的事件,以及在應(yīng)用程序中注冊監(jiān)聽器來響應(yīng)這些事件,需要的朋友可以參考下2022-06-06
從Hello?World開始理解GraphQL背后處理及執(zhí)行過程
這篇文章主要為大家介紹了從Hello?World開始理解GraphQL背后處理過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
IDEA2022版本創(chuàng)建maven?web項目的兩種方式詳解
創(chuàng)建maven?web項目有兩種方式,一種是使用骨架方式,一種是不使用骨架的方式,本文結(jié)合實例代碼給大家介紹了IDEA2022版本創(chuàng)建maven?web項目的兩種方式,需要的朋友可以參考下2023-02-02
Java編程小實例—數(shù)字時鐘的實現(xiàn)代碼示例
正所謂拳不離手曲不離口,java學(xué)習(xí)的過程中,練習(xí)還是要多一點比較好。接下來分享給大家一個Java編程的小實例,供朋友們參考。2017-10-10
SpringMVC修改返回值類型后的消息轉(zhuǎn)換器處理方式
這篇文章主要介紹了SpringMVC修改返回值類型后的消息轉(zhuǎn)換器處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09

