真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

編寫Java代碼對HDFS進行增刪改查操作代碼實例

本文實例為大家分享了Java代碼對HDFS進行增刪改查操作的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設,黃巖企業(yè)網(wǎng)站建設,黃巖品牌網(wǎng)站建設,網(wǎng)站定制,黃巖網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,黃巖網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

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("主機名:"+location.getHosts()[0]);
					System.out.println("主機名:"+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("下載成功!");
	}
}

思想:

一、定義虛擬機接口

二、先拿到HDFS遠程調用接口對象Configuration

三、定義分布式文件系統(tǒng)FileSystem對象獲取對象

四、給定路徑

五、用FileSystem對象調用操作

以上所述是小編給大家介紹的Java代碼對HDFS進行增刪改查操作詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!


網(wǎng)站名稱:編寫Java代碼對HDFS進行增刪改查操作代碼實例
本文網(wǎng)址:http://weahome.cn/article/pjggos.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部