編寫Java代碼對(duì)HDFS進(jìn)行增刪改查操作代碼實(shí)例
本文實(shí)例為大家分享了Java代碼對(duì)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)用接口對(duì)象Configuration
三、定義分布式文件系統(tǒng)FileSystem對(duì)象獲取對(duì)象
四、給定路徑
五、用FileSystem對(duì)象調(diào)用操作
以上所述是小編給大家介紹的Java代碼對(duì)HDFS進(jìn)行增刪改查操作詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring boot學(xué)習(xí)教程之快速入門篇
這篇文章主要給大家介紹了關(guān)于Spring boot的相關(guān)資料,本文屬于基礎(chǔ)入門教程,對(duì)各位學(xué)習(xí)Spring boot的新手們具有一定的參考學(xué)習(xí)價(jià)值,,要的朋友們下面來(lái)一起看看吧。2017-04-04
springboot?實(shí)現(xiàn)不同context-path下的會(huì)話共享
這篇文章主要介紹了springboot?實(shí)現(xiàn)不同context-path下的會(huì)話共享,基于很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
使用restTemplate.postForEntity()的問(wèn)題
這篇文章主要介紹了使用restTemplate.postForEntity()的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
詳解Java中Array和ArrayList的比較和轉(zhuǎn)換
在 Java 編程中,arrays 和 arraylists 都是基本的數(shù)據(jù)結(jié)構(gòu),用來(lái)存放數(shù)據(jù)集合,雖然兩者的用途一樣,但是它們的特點(diǎn)極大地影響應(yīng)用的性能和靈活性,本文探討 arrays 和 arraylists 的重要特性,它們各自的強(qiáng)項(xiàng)和弱點(diǎn),,需要的朋友可以參考下2023-08-08
Java微服務(wù)開(kāi)發(fā)之Swagger詳解
Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)??傮w目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來(lái)更新。文件的方法,參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許API來(lái)始終保持同步2021-10-10
Java double轉(zhuǎn)BigDecimal的注意事項(xiàng)說(shuō)明
這篇文章主要介紹了Java double轉(zhuǎn)BigDecimal的注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
Spring RedisTemplate 批量獲取值的2種方式小結(jié)
這篇文章主要介紹了Spring RedisTemplate 批量獲取值的2種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Mybatis配置之typeAlias標(biāo)簽的用法
這篇文章主要介紹了Mybatis配置之typeAlias標(biāo)簽的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

