JAVA操作Hadoop

写在前面:

我的博客已迁移至自建服务器:博客传送门,CSDN博客暂时停止,如有机器学习方面的兴趣,欢迎来看一看。

此外目前我在gitHub上准备一些李航的《统计学习方法》的实现算法,目标将书内算法全部手打实现,欢迎参观并打星。GitHib传送门

一、客户端环境准备

1.jar 包及Eclipse 准备

该部分可网上查阅相关资料

2.相关Demo

1.获取文件系统

//1.获取文件系统
@Test
public void initHDFS() throws IOException {
	// 1获取文件系统
	Configuration configuration = new Configuration();
	FileSystem fs = FileSystem.get(configuration);

	//2.打印文件系统到控制台
	System.out.println(fs.toString());
		
	fs.close();
}

1.首先需要获取到Hadoop的文件系统,以便对Hadoop进行操作。
2.打印获取到的文件系统
3.控制台有信息输出,说明获取成功

2.文件上传

//2.文件上传
@Test
public void CopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
	//获取文件系统
	Configuration configuration = new Configuration();
	FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
	
	//上传
	fs.copyFromLocalFile(false, new Path("e:/hello.txt"), new Path("/hello1.txt"));
	
	//释放资源
	fs.close();
	
}

3.文件下载

//3.文件下载
@Test
public void CopyToLoaclFile() throws IOException, InterruptedException, URISyntaxException{
	//获取文件系统
	Configuration configuration = new Configuration();
	FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
	
	//下载文件
	fs.copyToLocalFile(false, new Path("/hello1.txt"), new Path("e:\\hello1.txt"), true);
	
	fs.close();
}

4.创建目录

//4.创建目录
@Test
public void Mkdirs() throws IOException, InterruptedException, URISyntaxException {
	Configuration configuration = new Configuration();
	FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
	
	//创建目录
	fs.mkdirs(new Path("/test/haha/heihei"));
	
	fs.close();
}

5.删除文件夹

//5.删除文件夹
@Test
public void Delete() throws IOException, InterruptedException, URISyntaxException {
	//获取文件系统
	Configuration configuration = new Configuration();
	FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
	
	//删除文件夹
	fs.delete(new Path("/test"), true);
	
	//释放资源
	fs.close();
	
}

6.更改文件名

//6.更改文件名
@Test
public void ReName() throws IOException, InterruptedException, URISyntaxException {
	//获取文件系统
	Configuration configuration = new Configuration();
	FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
	
	//更改文件名
	fs.rename(new Path("/hello.txt"), new Path("/hello2.txt"));
	
	//释放资源
	fs.close();
}

7.查看文件详情

//7.查看文件详情
@Test
public void ListFiles() throws IOException, InterruptedException, URISyntaxException {
	//获取文件系统
	Configuration configuration = new Configuration();
	FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
	
	RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(new Path("/"), true);
	
	while(remoteIterator.hasNext()) {
		LocatedFileStatus status = remoteIterator.next();
		
		//文件名
		System.out.println(status.getPath().getName());
		//文件长度
		System.out.println(status.getLen());
		//组
		System.out.println(status.getGroup());
		//副本数
		System.out.println(status.getReplication());
		
		//获取块位置信息
		BlockLocation[] blockLocations = status.getBlockLocations();
		for(BlockLocation bl: blockLocations) {
			//偏移量
			System.out.println(bl.getOffset());
			//副本所在主机
			System.out.println(bl.getHosts());
			
			System.out.println(bl.getNames());
		}
		
		System.out.println("-------------------------------------------");
	}
	
	fs.close();
}

8.判断是否文件

//8.判断是否文件
@Test
public void IsFile() throws IOException, InterruptedException, URISyntaxException {
	//获取文件系统
	Configuration configuration = new Configuration();
	FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");
	
	FileStatus[] listStatus = fs.listStatus(new Path("/"));
	for(FileStatus status : listStatus) {
		if(status.isFile()) {
			System.out.println("f:" + status.getPath().getName());
		}
		else {
			System.out.println("d:" + status.getPath().getName());
		}
	}
	fs.close();
}
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值