前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hadoop的hdfs api操作 原

Hadoop的hdfs api操作 原

作者头像
用户2603479
发布2018-08-15 16:08:22
2340
发布2018-08-15 16:08:22
举报
文章被收录于专栏:JAVA技术站JAVA技术站
代码语言:javascript
复制
public static void listFile(String path) throws IOException{
		//读取配置文件
		Configuration conf = new Configuration();
		//获取文件系统
		FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);
		//获取文件或目录状态
		FileStatus[] fileStatus = fs.listStatus(new Path(path));
		//打印文件的路径
		for (FileStatus file : fileStatus) {
			System.out.println(file.getPath());
		}
	 
		//关闭文件系统
		fs.close();
	 }

一、包依赖

代码语言:javascript
复制
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>2.7.6</version>
</dependency>

二、API的操作

1.创建目录

代码语言:javascript
复制
	public static void mkdir(String path) throws IOException {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://127.0.0.1:9000");
		FileSystem fs = FileSystem.get(conf);
		Path srcPath = new Path(path);
		boolean isok = fs.mkdirs(srcPath);
		if (isok) {
			System.out.println("create dir ok!");
		} else {
			System.out.println("create dir failure");
		}
		fs.close();
	}

2.删除目录

代码语言:javascript
复制
/**
	 * 删除目录
	 * @param path
	 */
	public static void rmdir(String path)throws Exception {
		Configuration configuration = new Configuration();
		FileSystem fs = FileSystem.get(URI.create("hdfs://127.0.0.1:9000"), configuration);
	    boolean flag = fs.deleteOnExit(new Path("/test"));
	    if(flag) {
			 System.out.println("delete ok!");
		}else {
			 System.out.println("delete failure");
		}
		
		//关闭文件系统
		fs.close();

	}

3.创建文件

代码语言:javascript
复制
public static void createFile(String dst , byte[] contents) throws IOException{
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
		Path dstPath = new Path(dst);  
		FSDataOutputStream outputStream = fs.create(dstPath);
		outputStream.write(contents);
		outputStream.close();
		fs.close();
		System.out.println("文件创建成功!");
		
	 }

4.读取文件内容

代码语言:javascript
复制
public static void readFile(String uri) throws IOException {
		//读取配置文件
		Configuration conf = new Configuration();
		//获取文件系统
		FileSystem fs = FileSystem.get(URI.create("hdfs://localhost:9000"),conf);
		
		InputStream in = null;
		try {
			in = fs.open(new Path(uri));
			//复制到标准输出流
			IOUtils.copyBytes(in, System.out, 4096,false);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			IOUtils.closeStream(in);
		}
	}

5.查看文件目录

代码语言:javascript
复制
public static void listFile(String path) throws IOException{
		//读取配置文件
		Configuration conf = new Configuration();
		//获取文件系统
		FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop1:9000"),conf);
		//获取文件或目录状态
		FileStatus[] fileStatus = fs.listStatus(new Path(path));
		//打印文件的路径
		for (FileStatus file : fileStatus) {
			System.out.println(file.getPath());
		}
	 
		//关闭文件系统
		fs.close();
	 }

其它操作查看应的FileSystem的api

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档