package com.fickler.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.mapreduce.util.HostUtil;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
/**
* @author dell
* @version 1.0
*/
public class HdfsClient {
@Test
public void testMkdirs() throws URISyntaxException, IOException, InterruptedException {
//连接的集群nn地址
URI uri = new URI("hdfs://hadoop102:8020");
//配置文件
Configuration configuration = new Configuration();
//用户
String user = "fickler";
//获得客户端对象
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
//创建一个文件夹
fileSystem.mkdirs(new Path("/xiyou/huaguoshan"));
//关闭资源
fileSystem.close();
}
@Test
public void testPut() throws URISyntaxException, IOException, InterruptedException {
//连接到集群nn
URI uri = new URI("hdfs://hadoop102:8020");
//用户
String user = "fickler";
//配置文件
Configuration configuration = new Configuration();
//获得客户端对象
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
//上传文件
//第一个参数:是否删除元数据,第二个参数:是否覆盖写入,第三个参数:元数据路径,第四个参数:拷贝路径
fileSystem.copyFromLocalFile(true, true, new Path("d:/sunwukong.txt"), new Path("/xiyou/huaguoshan"));
//关闭资源
fileSystem.close();
}
@Test
public void testCopyToLocalFile() throws URISyntaxException, IOException, InterruptedException {
URI uri = new URI("hdfs://hadoop102:8020");
String user = "fickelr";
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
//第一个参数:是否将原文件删除,第二个参数:要下载的文件路径,第三个参数:文件将要下载到的路径,第四个参数:是否开启文件校验
fileSystem.copyToLocalFile(false, new Path("/xiyou/huaguoshan/sunwukong.txt"), new Path("d:/sunwukong.txt"), true);
fileSystem.close();
}
@Test
public void testRename() throws URISyntaxException, IOException, InterruptedException {
URI uri = new URI("hdfs://hadoop102:8020");
String user = "fickler";
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
fileSystem.rename(new Path("/xiyou/huaguoshan/sunwukong.txt"), new Path("/xiyou/huaguoshan/meihouwang.txt"));
fileSystem.close();
}
@Test
public void testDelete() throws URISyntaxException, IOException, InterruptedException {
URI uri = new URI("hdfs://hadoop102:8020");
String user = "fickler";
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
//第一个参数:要删除的目录,第二个参数:是否递归删除
fileSystem.delete(new Path("/xiyou"), true);
fileSystem.close();
}
@Test
public void testListFiles() throws URISyntaxException, IOException, InterruptedException {
URI uri = new URI("hdfs://hadoop102:8020");
String user = "fickler";
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true);
while (listFiles.hasNext()) {
LocatedFileStatus next = listFiles.next();
System.out.println(next.getPermission());
System.out.println(next.getOwner());
System.out.println(next.getGroup());
System.out.println(next.getLen());
System.out.println(next.getModificationTime());
System.out.println(next.getReplication());
System.out.println(next.getBlockSize());
System.out.println(next.getPath().getName());
//获取块信息
BlockLocation[] blockLocations = next.getBlockLocations();
System.out.println(Arrays.toString(blockLocations));
}
fileSystem.close();
}
@Test
public void testListStatus() throws URISyntaxException, IOException, InterruptedException {
URI uri = new URI("hdfs://hadoop102:8020");
String user = "fickler";
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(uri, configuration, user);
FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
if (fileStatus.isFile()) {
System.out.println("f:" + fileStatus.getPath().getName());
} else {
System.out.println("d:" + fileStatus.getPath().getName());
}
}
fileSystem.close();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码
共12个文件
xml:7个
properties:2个
class:1个
需积分: 19 1 下载量 169 浏览量
2022-09-10
11:57:49
上传
评论
收藏 10KB ZIP 举报
温馨提示
Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码
资源详情
资源评论
资源推荐
收起资源包目录
HDFSClient.zip (12个子文件)
HDFSClient
pom.xml 1KB
target
classes
log4j.properties 451B
com
fickler
hdfs
HdfsClient.class 5KB
hdfs-site.xml 231B
generated-sources
annotations
src
test
java
main
resources
log4j.properties 451B
hdfs-site.xml 231B
java
com
fickler
hdfs
HdfsClient.java 5KB
.idea
misc.xml 541B
jarRepositories.xml 864B
compiler.xml 543B
workspace.xml 6KB
.gitignore 184B
共 12 条
- 1
在人间负债^
- 粉丝: 1w+
- 资源: 44
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0