HDFS的API操作

HDFS的API操作

HDFS环境准备

  1. 准备Hadoop的Windows10的客户端
  2. 准备JDK8(也就是对应Java1.8)
  3. 配置Hadoop的Windows10的客户端的环境变量配置
  4. 创建maven工程
  5. 导入依赖
<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.8.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId>
			<version>2.7.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
			<version>2.7.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-hdfs</artifactId>
			<version>2.7.2</version>
		</dependency>
		<dependency>
			<groupId>jdk.tools</groupId>
			<artifactId>jdk.tools</artifactId>
			<version>1.8</version>
			<scope>system</scope>
			<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
		</dependency>
</dependencies>

公共部分代码解析

Declaration

Provides access to configuration parameters.

翻译过来就是:提供对配置参数的访问。

第一步 获取对配置参数的访问

Configuration conf = new Configuration();

第二步 创建文件系统的访问对象

FileSystem fs = FileSystem.get(new URI("hdfs://192.168.174.137:9000"), conf, "root");

第三步 操作文件系统

最后一步 关闭资源

fs.close();

部分代码解析

1文件上传

Delcaration

The src file is on the local disk. Add it to FS at

the given dst name and the source is kept intact afterwards

@param src path

@param dst path

翻译:

第一个参数src 来源路径

第二个参数dst 目标路径

从源路径上传到目标路径

fs.copyFromLocalFile(new Path("e:/banzhang.txt"), new Path("/banzhang.txt"));

2文件下载

The src file is under FS, and the dst is on the local disk.

Copy it from FS control to the local dst name.

delSrc indicates if the src will be removed or not.

@param delSrc whether to delete the src

@param src path

@param dst path

翻译:

第一个参数 是否删除源文件

第二个参数 来源文件

第三个参数 目标路径

fs.copyToLocalFile(false, new Path("/banzhang.txt"), new Path("e:/banhua.txt"), true);

3文件夹删除

Delete a file.
@param f the path to delete.
@param recursive if path is a directory and set to
true, the directory is deleted else throws an exception. In
case of a file the recursive can be set to either true or false.
@return true if delete is successful else false.
@throws IOException

翻译:
第一个参数 目标路径(操作路径)

第二个参数 是否递归删除 如果删除的是个目录,目录里面还有别的文件,则会报错

fs.delete(new Path("/0508/"), true);

4文件名更改

第一个参数 源路径

第二个路径 更名之后的路径

fs.rename(new Path("/banzhang.txt"), new Path("/banhua.txt"));

5文件详情查看

// 2 获取文件详情
	RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
		
	while(listFiles.hasNext()){
		LocatedFileStatus status = listFiles.next();
			
		// 输出详情
		// 文件名称
		System.out.println(status.getPath().getName());
		// 长度
		System.out.println(status.getLen());
		// 权限
		System.out.println(status.getPermission());
		// 分组
		System.out.println(status.getGroup());
			
		// 获取存储的块信息
		BlockLocation[] blockLocations = status.getBlockLocations();
			
		for (BlockLocation blockLocation : blockLocations) {
			// 获取块存储的主机节点
			String[] hosts = blockLocation.getHosts();
            
			for (String host : hosts) {
				System.out.println(host);
			}
		}
			
		System.out.println("-----------班长的分割线----------");
	}

6文件和文件夹判断

// 2 判断是文件还是文件夹
	FileStatus[] listStatus = fs.listStatus(new Path("/"));
		
	for (FileStatus fileStatus : listStatus) {
		
		// 如果是文件
		if (fileStatus.isFile()) {
				System.out.println("f:"+fileStatus.getPath().getName());
			}else {
				System.out.println("d:"+fileStatus.getPath().getName());
			}
		}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李南想做条咸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值