編寫Java代碼對HDFS進(jìn)行增刪改查操作代碼實例
本文實例為大家分享了Java代碼對HDFS進(jìn)行增刪改查操作的具體代碼,供大家參考,具體內(nèi)容如下
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class FileOpreation {
public static void main(String[] args) throws IOException {
//CreateFile();
//DeleteFile();
//CopyFileToHDFS();
//MkDirs();
//DelDirs();
ListDirectory();
DownLoad();
}
public static void CreateFile() throws IOException {
String uri = "hdfs://Alvis:9000";
Configuration configuration =new Configuration();
FileSystem fSystem = FileSystem.get(URI.create(uri), configuration);
byte[] file_content_buff="hello hadoop world, test write file !\n".getBytes();
Path dfs = new Path("/home/test.txt");
FSDataOutputStream outputStream = fSystem.create(dfs);
outputStream.write(file_content_buff.length);
}
public FileOpreation() {
// TODO Auto-generated constructor stub
}public static void DeleteFile() throws IOException {
String uri = "hdfs://Alvis:9000";
Configuration configuration =new Configuration();
FileSystem fSystem = FileSystem.get(URI.create(uri), configuration);
Path deletf = new Path("/home/test.txt");
boolean delResult = fSystem.delete(deletf,true);
System.out.println(delResult==true?"刪除成功":"刪除失敗");
}
public static void CopyFileToHDFS() throws IOException {
String uri = "hdfs://Alvis:9000";
Configuration configuration =new Configuration();
FileSystem fSystem = FileSystem.get(URI.create(uri), configuration);
Path src = new Path("E:\\SerializationTest\\APITest.txt");
Path dest_src = new Path("/home");
fSystem.copyFromLocalFile(src, dest_src);
}
public static void MkDirs() throws IOException {
String uri = "hdfs://Alvis:9000";
Configuration configuration =new Configuration();
FileSystem fSystem = FileSystem.get(URI.create(uri), configuration);
Path src = new Path("/Test");
fSystem.mkdirs(src);
}
public static void DelDirs() throws IOException {
String uri = "hdfs://Alvis:9000";
Configuration configuration = new Configuration();
FileSystem fSystem = FileSystem.get(URI.create(uri), configuration);
Path src = new Path("/Test");
fSystem.delete(src);
}
public static void ListDirectory() throws IOException {
String uri = "hdfs://Alvis:9000";
Configuration configuration = new Configuration();
FileSystem fSystem = FileSystem.get(URI.create(uri), configuration);
FileStatus[] fStatus = fSystem.listStatus(new Path("/output"));
for(FileStatus status : fStatus)
if (status.isFile()) {
System.out.println("文件路徑:"+status.getPath().toString());
System.out.println("文件路徑 getReplication:"+status.getReplication());
System.out.println("文件路徑 getBlockSize:"+status.getBlockSize());
BlockLocation[] blockLocations = fSystem.getFileBlockLocations(status, 0, status.getBlockSize());
for(BlockLocation location : blockLocations){
System.out.println("主機(jī)名:"+location.getHosts()[0]);
System.out.println("主機(jī)名:"+location.getNames()[0]);
}
}
else {
System.out.println("directory:"+status.getPath().toString());
}
}
public static void DownLoad() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://Alvis:9000");
FileSystem fSystem =FileSystem.get(configuration);
FSDataInputStream inputStream =fSystem.open( new Path("/input/wc.jar"));
FileOutputStream outputStream = new FileOutputStream(new File("E:\\LearnLife\\DownLoad\\wc.jar"));
IOUtils.copy(inputStream, outputStream);
System.out.println("下載成功!");
}
}
思想:
一、定義虛擬機(jī)接口
二、先拿到HDFS遠(yuǎn)程調(diào)用接口對象Configuration
三、定義分布式文件系統(tǒng)FileSystem對象獲取對象
四、給定路徑
五、用FileSystem對象調(diào)用操作
以上所述是小編給大家介紹的Java代碼對HDFS進(jìn)行增刪改查操作詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java Swing JButton按鈕的實現(xiàn)示例
這篇文章主要介紹了Java Swing JButton按鈕的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
java批量下載將多個文件(minio中存儲)壓縮成一個zip包代碼示例
在Java應(yīng)用程序中有時我們需要從多個URL地址下載文件,并將這些文件打包成一個Zip文件進(jìn)行批量處理或傳輸,這篇文章主要給大家介紹了關(guān)于java批量下載將多個文件(minio中存儲)壓縮成一個zip包的相關(guān)資料,需要的朋友可以參考下2023-11-11
SpringBoot如何獲取當(dāng)前操作用戶的id/信息
在一般性的基設(shè)需求中,有需要獲取當(dāng)前用戶操作記錄的情況,也就是說我們需要記錄當(dāng)前用戶的信息,如:id、昵稱、賬號等信息,這篇文章主要介紹了SpringBoot獲取當(dāng)前操作用戶的id/信息,需要的朋友可以參考下2023-10-10
Springboot使用異步請求提高系統(tǒng)的吞吐量詳解
這篇文章主要介紹了Springboot使用異步請求提高系統(tǒng)的吞吐量詳解,和同步請求相對,異步不需要等待響應(yīng),隨時可以發(fā)送下一次請求,如果是同步請求,需要將信息填寫完整,再發(fā)送請求,服務(wù)器響應(yīng)填寫是否正確,再做修改,需要的朋友可以參考下2023-08-08

