eclipse 实现 Hdfs java API

Hadoop集群搭建及配置④ —— JDK简介及其安装

Hadoop集群搭建及配置⑤ —— Zookeeper 讲解及安装

Hadoop集群搭建及配置⑥ —— Hadoop组件安装及配置

Hadoop集群搭建及配置⑦—— Spark&Scala安装配置

Hadoop集群搭建及配置⑧——Hbase的安装配置

eclipse配置连接Hadoop


eclipse实现Hdfs功能

实现以下功能:

  1. 获取文件系统信息
  2. 列出所有DataNode的名字信息
  3. 创建文件目录
  4. 删除文件或文件目录
  5. 查看文件是否存在
  6. 文件上传至HDFS
  7. copyToL ocalFile表示从HDFS下载文件到本地系统
  8. 文件重命名
  9. 遍历文件和目录
  10. 主函数实现调用
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;

class Data {
	// (1)获取文件系统
	public static FileSystem getFileSystem(){
		// 读取配置文件
		Configuration conf = new Configuration();
		// 文件系统
		FileSystem fs = null;
		conf.set("fs.defaultFS","hdfs://192.168.142.128:8020");
		System.setProperty("HADOOP_USER_NAME","root");
		try {
			fs = FileSystem.get(conf);
		}catch(IOException e) {
			e.printStackTrace();
		}
		return fs;
	}

	// (2)列出所有DataNode的名字信息
	public static void listDataNodeInfo() {
		try {
			// 
			Configuration conf = new Configuration();
			String uri = "hdfs://192.168.142.128:8020";
			// FileSystem 基于 Configuration 创建 FileSystem对象
			// 为了调用FileSystem 
			//为了调用FileSystem里面的相关方法对应具体的(上传、创建、删除、下载等一系列对HDFS文件系统的操作)
			FileSystem fs = FileSystem.get(URI.create(uri),conf);
			DistributedFileSystem hdfs = (DistributedFileSystem)fs;
			DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
			String[] names = new String[dataNodeStats.length];
			System.out.println("List of all the datanodes in the HDFS cluster.");
			for(int i=0; i<names.length; i++) {
				names[i] = dataNodeStats[i].getHostName();
				System.out.println(names[i]);
			}
			System.out.print(hdfs.getUri().toString());
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	// (3)创建文件目录
	public static void mkdir(String path) throws IOException {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.142.128:8020");
		FileSystem fs = FileSystem.get(conf);
		Path srcPath = new Path(path);
		boolean isok = fs.mkdirs(srcPath);
		if (isok) {
			System.out.println("creat dir ok.");
		}
		else {
			System.out.println("create dir failure.");
		}
		fs.close();
	}
	
	// (4)删除文件或文件目录
	private static void delete(String filePath) throws IOException {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.142.128:8020");
		FileSystem fs = FileSystem.get(conf);
		Path path = new Path(filePath);
		boolean isok = fs.deleteOnExit(path);
		if (isok) {
			System.out.println("delete ok");
		} else {
			System.out.println("delete failure");
		}
	}

	// (5)查看文件是否存在
	// 判断一个文件或目录是否存在可以通过exists 方法实现.该方法会返回一个布尔值,
	// true表示存在,false 表示不存在.
	public static void checkFileExist(Path path) {
		try{
			Configuration conf = new Configuration();
			conf.set("fs.defaultFS","hdfs://192.168.142.128:8020");
			FileSystem fs = FileSystem.get(conf); 
			DistributedFileSystem hdfs = (DistributedFileSystem) fs;
			Path f = hdfs.getHomeDirectory();
			System.out.println("main path"+f.toString());
			boolean exist = fs.exists(path);
			System.out.println("Whether exist of this file:" + exist);
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	// (6)文件上传至HDFS
	// copyFroml ocalFile表示文件的复制,该方法有3个参数:
	// 第1个参数是布尔值,表示是否删除源文件, true 为删除,默认为false 不删除;
	// 第2个参数是要复制的文件,
	// 第3个参数是要复制到的目的地。
	public static void uploadFile(String src, String dst) throws IOException {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.142.128:8020");
		FileSystem fs = FileSystem.get(conf);
		Path srcPath = new Path(src);
		Path dstPath = new Path(dst);
		fs.copyFromLocalFile(false, srcPath,dstPath);
		fs.close();
		System.out.println("uploadFile ok");
	}

	// (7)copyToL ocalFile表示从HDFS下载文件到本地系统.该方法有两个参数:
	// 第1个表示要下载的HDFS文件;
	// 第2个表示要下载到目录或文件。
	public static void download(String remote,String local)throws IOException ,URISyntaxException{
		Configuration conf = new Configuration();
		String uri = "hdfs://192.168.142.128:8020";
		FileSystem fs = FileSystem.get(new URI("hdfs://192.168.142.128:8020"), conf);
		Path path = new Path(remote);
		fs.copyToLocalFile(path, new Path(local));
		System.out.println("download : from "+remote+" to "+local);
		fs.close();
	}
	
	// (8)文件重命名
	// 文件重命名可以使用rename方法.该方法有两个参数:
	// 第1个参数表示要重命名的文件;第2个参数表示文件的新名字.
	// 该方法返回-个布尔值,表示是否成功,具体实现代码如下.
	public static void rename(String oldName, String newName) throws IOException,
	URISyntaxException {
		Configuration conf = new Configuration();
		String uri = "hdfs://192.168.142.128:8020";
		FileSystem fs = FileSystem.get(new URI("hdfs://192.168.142.128:8020"), conf);
		Path oldPath = new Path(oldName);
		Path newPath = new Path(newName);
		boolean isok = fs.rename(oldPath, newPath);
		if (isok) {
			System.out.println("rename ok.");
		}else{
			System.out.println("rename failure.");
		}
		fs.close();
	}

	// (9)遍历文件和目录
	// FileStatus包含了HDFS 文件的常用信息,而该对象的使用需要通过
	// DistributedFileSystem对象来访问,因此需要先将FileSystem 对象强制转换为DistributedFileSystem.
	public static void showDir(Path path) throws IOException, URISyntaxException {
		Configuration conf = new Configuration();
		String uri = "hdfs://192.168.142.128:8020";
		FileSystem fs = FileSystem.get(new URI("hdfs://192.168.142.128:8020"), conf);
		DistributedFileSystem hdfs =(DistributedFileSystem) fs;
		FileStatus[] fileStatus = hdfs.listStatus(path);
		if (fileStatus.length>0) {
			for(FileStatus status:fileStatus) {
				Path f = status. getPath();
				System.out.println(f);
				
				// 判断是否为目录,是的话循环遍历嵌套访问
				if (status.isDirectory()) {
					FileStatus[] files = hdfs.listStatus(f);
					if (files.length>0) {
						for(FileStatus file:files)
							showDir(file.getPath());
					}
				}
			}
		}
	}

	public static void main(String[] arg)throws IOException,URISyntaxException,IllegalArgumentException {
		listDataNodeInfo(); // 列出所有DataNode的名字信息
		mkdir("/hdfs"); // 创建文件夹hdfs
//		delete("/aa"); // 删除 aa文件
		checkFileExist(new Path("/test")); // 是否存在test
//		uploadFile("C:\\Users\\锦樽\\Desktop\\大数据\\hbase.docx","/user/hbasecopy.docx"); // 上传文件到HDFS
//		download("/test/word.txt" ,"C:\\Users\\锦樽\\Desktop"); 
		rename("/test/word.txt","/hdfs/w.txt");
		showDir(new Path("/hdfs"));
	}
}

在这里插入图片描述
操作完毕。


版权声明:本文为weixin_44775255原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。