hdfs java客户端编写

1、导入jar包
1)common包中的主要核心包,还有lib中的所有jar包
在这里插入图片描述
2)hdfs包中的主要核心包,还有lib中的所有jar包
在这里插入图片描述
导入包之后开始编写java客户端

查看hdfs上的所有文件,

(1)
查看hdfs上的所有文件
	public void list() throws IOException, InterruptedException, URISyntaxException {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.100.11:9000/");
		FileSystem fs  = FileSystem.get(new URI("hdfs://192.168.100.11:9000/"), conf, "root");
		// listFiles(Path path ,boolean recursive),recursive为true则递归文件夹查找文件。false则查找当前目录下的所有文件
		RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("hdfs://192.168.100.11:9000/"), false);
		while(listFiles.hasNext()) {
			LocatedFileStatus next = listFiles.next();
			System.out.println(next.getPath().getName());
		}
		
	}
(2)
查看hdfs上的所有文件夹和文件
	public void list2() throws Exception {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.100.11:9000/");
		FileSystem fs  = FileSystem.get(new URI("hdfs://192.168.100.11:9000/"), conf, "root");
		FileStatus[] listStatus = fs.listStatus(new Path("hdfs://192.168.100.11:9000/"));
		for(FileStatus f : listStatus) {
			System.out.println(f.getPath().getName());
		}
		
	}

上传下载文件

public void upLoad() throws Exception {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.100.11:9000/");
		FileSystem fs  = FileSystem.get(new URI("hdfs://192.168.100.11:9000/"), conf, "root");
//		hdfs封装底层对文件上传下载的方法
//		copyFromLocalFile(Path src,Path dst)上传文件,src上传文件的原路径,dst为上传后的路径
		fs.copyFromLocalFile(new Path("D:\\Zoo.mp3"), new Path("hdfs://192.168.100.11:9000/lovess1.mp3"));
//		copyToLocalFile(Path dst,Path src)下载文件,src下载文件到本地的路径,dst为下载的源路径
		fs.copyToLocalFile(new Path("hdfs://192.168.100.11:9000/lovess1.mp3"), new Path("D:\\lovess1.mp3"));

删除用delete,移动、改名rname

猜你喜欢

转载自blog.csdn.net/qq_41854797/article/details/90025969
今日推荐