java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java对HDFS进行增删改查

编写Java代码对HDFS进行增删改查操作代码实例

作者:Alvis zhao

这篇文章主要介绍了Java代码对HDFS进行增删改查操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

本文实例为大家分享了Java代码对HDFS进行增删改查操作的具体代码,供大家参考,具体内容如下

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

三、定义分布式文件系统FileSystem对象获取对象

四、给定路径

五、用FileSystem对象调用操作

以上所述是小编给大家介绍的Java代码对HDFS进行增删改查操作详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:
阅读全文