HDFS客户端的API操作

     下面的代码对HDFS的客户端操作,即HDFS文件的上传,HDFS文件的下载,HDFS文件夹的删除,HDFS文件名的更改,HDFS文件详情的查看。代码如下:

package com.fczheng.hdfs;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Test;

public class HDFSClient {
	/**
	 * 测试连接HDFS(创建一个目录)
	 * @throws IOException
	 * @throws InterruptedException
	 * @throws URISyntaxException
	 */
	@Test
	public void testMkdirs() throws IOException, InterruptedException, URISyntaxException {
		//1.用于配置HDFS相关的参数
		Configuration conf = new Configuration();
		//设置NameNode地址
		conf.set("fs.defaultFS", "hdfs://hadoop112:9000");
		//2.创建一个用于操作HDFS集群的FileSystem对象
		FileSystem fs = FileSystem.get(conf);
		
		//FileSystem fs = FileSystem.get(new URI("hdfs://hadoop112:9000"), conf, "hadoop");
		//3.创建目录
		fs.mkdirs(new Path("/user/fczheng/0520"));
		//4.释放资源
		fs.close();
	}
	
	/**
	 * 文件的上传
	 * @throws IOException
	 * @throws InterruptedException
	 * @throws URISyntaxException
	 */
	@Test
	 public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
		//获取配置文件系统
		Configuration conf = new Configuration();
		//创建一个用于操作HDFS集群的FileSystem对象
		FileSystem fs = FileSystem.get(new URI("hdfs://hadoop112:9000"), conf, "hadoop");
		//上传文件
		fs.copyFromLocalFile(new Path("F:\\mysqldb.sql"), new Path("/user/atguigu/input"));
		//释放资源
		fs.close();
	}
	
	/**
	 * 文件下载
	 * @throws IOException
	 * @throws InterruptedException
	 * @throws URISyntaxException
	 */
	@Test
	public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException {
		//获取配置文件系统
		Configuration conf = new Configuration();
		//创建一个用于操作HDFS集群的FileSystem对象
		FileSystem fs = FileSystem.get(new URI("hdfs://hadoop112:9000"), conf, "hadoop");
		//文件下载
		fs.copyToLocalFile(false, new Path("/user/atguigu/input/mysqldb.sql"), new Path("e:/mysql"), true);
		//释放资源
		fs.close();
	}
	
	/**
	 * 文件删除
	 * @throws IOException
	 * @throws InterruptedException
	 * @throws URISyntaxException
	 */
	@Test
	public void testDelete() throws IOException, InterruptedException, URISyntaxException {
		//1.获取文件系统
		Configuration conf = new Configuration();
		//2.配置在集群上运行
		FileSystem fs = FileSystem.get(new URI("hdfs://hadoop112:9000"), conf, "hadoop");
		//4.执行删除
		fs.delete(new Path("/user/fczheng/0520"),true);
		//释放资源
		fs.close();
	}
	
	/**
	 * 文件重命名
	 * @throws URISyntaxException 
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	@Test
	public void testRename() throws IOException, InterruptedException, URISyntaxException {
		//HDFS参数配置
		Configuration conf = new Configuration();
		//创建操作HDFS的文件系统对象
		FileSystem fs = FileSystem.get(new URI("hdfs://hadoop112:9000"), conf, "hadoop");
		//修改文件名
		fs.rename(new Path("/user/fczheng/0520"), new Path("/user/fczheng/6666"));
		//释放资源
		fs.close();
	}
	
	/**
	 * 显示文件信息
	 * @throws URISyntaxException 
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	@Test
	public void testListFiles() throws IOException, InterruptedException, URISyntaxException {
		//1.获取文件系统
		Configuration conf = new Configuration();
		//2.配置在集群上运行
		FileSystem fs = FileSystem.get(new URI("hdfs://hadoop112:9000"), conf, "hadoop");
		//获取文件系统所有文件
		RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
		//迭代
		while(listFiles.hasNext()) {
			System.out.println("--------------------start---------------------");
			LocatedFileStatus fileStatus = listFiles.next();
			System.out.println("文件名:" + fileStatus.getPath().getName());
			System.out.println("文件路径:" + fileStatus.getPath());
			System.out.println("访问时间:" + new Date(fileStatus.getAccessTime()));
			System.out.println("块大小:" + fileStatus.getBlockSize());
			System.out.println("所属组:" + fileStatus.getGroup());
			System.out.println("文件大小:" +fileStatus.getLen() );
			System.out.println("修改时间	:" + new Date(fileStatus.getModificationTime()));
			System.out.println("所有者:" + fileStatus.getOwner());
			System.out.println("副本数:" + fileStatus.getReplication());
			System.out.println("块位置:");
			BlockLocation[] blockLocations = fileStatus.getBlockLocations();
			for (BlockLocation blockLocation : blockLocations) {
				String[] hosts = blockLocation.getHosts();
				for (String host : hosts) {
					System.out.println("-->" + host);
				}
			}
			System.out.println("权限:" + fileStatus.getPermission());
			System.out.println("--------------------end---------------------");

		}
		//释放资源
		fs.close();
	}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值