hadoop2.7之hdfs常用操作及Java操作

前面已经搭建了hadoop的完全分布式,本文介绍分布式文件系统的常用操作

进入hadoop安装目录的bin目录下,我的路径是;/soft/hadoop/bin

1 创建文件夹

格式:hdfs dfs -mkdir -p 文件夹名称

hdfs dfs -mkdir -p /user/centos/hadoop

2 查看文件

格式:hdfs dfs -ls 查看路径(/ :表示根路径)

hdfs dfs -ls /user
hdfs dfs -ls -R /user  递归查看

3 上传文件

格式:hdfs dfs -put 本地文件路径 要上传到的文件路径

hdfs dfs -put index.html /user/centos/hadoop

在这里插入图片描述

4 下载文件

格式:hdfs dfs -get 要下载的文件路径 下载后的文件路径

hdfs dfs -get /user/centos/hadoop/index.html a.html

在这里插入图片描述
这里也可以下载
在这里插入图片描述

5 删除文件

格式 :hdfs dfs -rm -r -f 要删除的文件路径

hdfs dfs -rm -r -f /user/centos/hadoop

JAVA 操作 HDFS

需要hadoop的jar包支持,楼主这里就不贴出来了

package hdfs;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;

public class TestHDFS {
	
	/**
	 * javazIO 读取hdfs文件
	 */
	@Test
	public void readFile() throws Exception{
		//注册url流处理器工厂(hdfs)
		URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
		URL url = new URL("hdfs://192.168.1.38:8020/user/centos/hadoop/index.html");
		URLConnection conn = url.openConnection();
		InputStream is = conn.getInputStream();
		byte[] buf = new byte[is.available()];
		is.read(buf);
		is.close();
		String str = new String(buf);
		System.out.println(str);
	}
	
	/**
	 * 通过hadoop API访问文件
	 */
	@Test
	public void readFileByAPI() throws Exception{
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.1.38:8020/");
		FileSystem fs = FileSystem.get(conf) ;
		Path p = new Path("/user/centos/hadoop/index.html");
		FSDataInputStream fis = fs.open(p);
		byte[] buf = new byte[1024];
		int len = -1 ; 
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		while((len = fis.read(buf)) != -1){
			baos.write(buf, 0, len);
		}
		fis.close();
		baos.close();
		System.out.println(new String(baos.toByteArray()));
	}
	
	/**
	 * 通过hadoop API访问文件2
	 */
	@Test
	public void readFileByAPI2() throws Exception{
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.1.38:8020/");
		FileSystem fs = FileSystem.get(conf) ;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		Path p = new Path("/user/centos/hadoop/index.html");
		FSDataInputStream fis = fs.open(p);
		IOUtils.copyBytes(fis, baos, 1024);
		System.out.println(new String(baos.toByteArray()));
	}
	
	/**
	 * 创建文件夹
	 * 如果创建失败,给/user/centos 这个路径添加777 权限: hdfs dfs -chmod 777 /user/centos
	 */
	@Test
	public void mkdir() throws Exception{
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.1.38:8020/");
		FileSystem fs = FileSystem.get(conf) ;
		fs.mkdirs(new Path("/user/centos/myhadoop"));
	}
	
	/**
	 * 创建文件,并添加内容
	 */
	@Test
	public void putFile() throws Exception{
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.1.38:8020/");
		FileSystem fs = FileSystem.get(conf) ;
		FSDataOutputStream out = fs.create(new Path("/user/centos/myhadoop/a.txt"));
		out.write("helloworld".getBytes());
		out.close();
	}
	
	/**
	 * 删除文件
	 */
	@Test
	public void removeFile() throws Exception{
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://192.168.1.38:8020/");
		FileSystem fs = FileSystem.get(conf) ;
		Path p = new Path("/user/centos/myhadoop");
		fs.delete(p, true);
	}

}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值