最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

編寫Java代碼對(duì)HDFS進(jìn)行增刪改查操作代碼實(shí)例

 更新時(shí)間:2019年04月10日 11:10:09   作者:Alvis zhao  
這篇文章主要介紹了Java代碼對(duì)HDFS進(jìn)行增刪改查操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本文實(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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Java Swing JButton按鈕的實(shí)現(xiàn)示例

    Java Swing JButton按鈕的實(shí)現(xiàn)示例

    這篇文章主要介紹了Java Swing JButton按鈕的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • springboot 文件上傳大小配置的方法

    springboot 文件上傳大小配置的方法

    本篇文章主要介紹了springboot 文件上傳大小配置的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • springboot 日志彩色消失的2種解決方案

    springboot 日志彩色消失的2種解決方案

    這篇文章主要介紹了springboot 日志彩色消失的2種解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • JavaSE中Lambda表達(dá)式的使用與變量捕獲

    JavaSE中Lambda表達(dá)式的使用與變量捕獲

    這篇文章主要介紹了JavaSE中Lambda表達(dá)式的使用與變量捕獲,Lambda表達(dá)式允許你通過表達(dá)式來代替功能接口, 就和方法一樣,它提供了一個(gè)正常的參數(shù)列表和一個(gè)使用這些參數(shù)的主體,下面我們來詳細(xì)看看,需要的朋友可以參考下
    2023-10-10
  • java批量下載將多個(gè)文件(minio中存儲(chǔ))壓縮成一個(gè)zip包代碼示例

    java批量下載將多個(gè)文件(minio中存儲(chǔ))壓縮成一個(gè)zip包代碼示例

    在Java應(yīng)用程序中有時(shí)我們需要從多個(gè)URL地址下載文件,并將這些文件打包成一個(gè)Zip文件進(jìn)行批量處理或傳輸,這篇文章主要給大家介紹了關(guān)于java批量下載將多個(gè)文件(minio中存儲(chǔ))壓縮成一個(gè)zip包的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • 一篇文章帶你了解Java方法的使用

    一篇文章帶你了解Java方法的使用

    這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • SpringBoot如何獲取當(dāng)前操作用戶的id/信息

    SpringBoot如何獲取當(dāng)前操作用戶的id/信息

    在一般性的基設(shè)需求中,有需要獲取當(dāng)前用戶操作記錄的情況,也就是說我們需要記錄當(dāng)前用戶的信息,如:id、昵稱、賬號(hào)等信息,這篇文章主要介紹了SpringBoot獲取當(dāng)前操作用戶的id/信息,需要的朋友可以參考下
    2023-10-10
  • springboot使用kafka事務(wù)的示例代碼

    springboot使用kafka事務(wù)的示例代碼

    Kafka?同數(shù)據(jù)庫一樣支持事務(wù),當(dāng)發(fā)生異常的時(shí)候可以進(jìn)行回滾,確保消息監(jiān)聽器不會(huì)接收到一些錯(cuò)誤的或者不需要的消息,本文就來介紹一下springboot使用kafka事務(wù)的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-06-06
  • Java操作文件輸出為字符串以及字符串輸出為文件的方法

    Java操作文件輸出為字符串以及字符串輸出為文件的方法

    今天小編就為大家分享一篇Java操作文件輸出為字符串以及字符串輸出為文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Springboot使用異步請(qǐng)求提高系統(tǒng)的吞吐量詳解

    Springboot使用異步請(qǐng)求提高系統(tǒng)的吞吐量詳解

    這篇文章主要介紹了Springboot使用異步請(qǐng)求提高系統(tǒng)的吞吐量詳解,和同步請(qǐng)求相對(duì),異步不需要等待響應(yīng),隨時(shí)可以發(fā)送下一次請(qǐng)求,如果是同步請(qǐng)求,需要將信息填寫完整,再發(fā)送請(qǐng)求,服務(wù)器響應(yīng)填寫是否正確,再做修改,需要的朋友可以參考下
    2023-08-08

最新評(píng)論

轮台县| 罗江县| 大宁县| 玛纳斯县| 元阳县| 措勤县| 虎林市| 仁布县| 樟树市| 湘潭市| 阿合奇县| 承德市| 天台县| 大同县| 同江市| 普兰县| 喀喇沁旗| 建平县| 安图县| 理塘县| 内丘县| 太和县| 常熟市| 宜州市| 陆川县| 纳雍县| 竹溪县| 哈巴河县| 望奎县| 鄂托克前旗| 融水| 新民市| 潼关县| 吉林市| 精河县| 曲阳县| 汉阴县| 莱州市| 双流县| 交口县| 赤壁市|