首先win环境配置
这里需要配置JDK,Hadoop的环境变量,windows依赖放在这里了
链接:https://pan.baidu.com/s/1nO8yHE07reOqNmyoZwhyAQ?pwd=ixep
提取码:ixep
放在一个非中文路径,然后配置环境变量HADOOP_HOME
idea创建工程并引入依赖
<dependencies>
<dependency>
<groupId>ch.cern.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>#</groupId>
<artifactId>log4</artifactId>
<version>log4j-2.17.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
这里引入hadoop-hdfs其实是为了看BlockPlacementPolicyDefault这个类里面对于上传文件的源码查看
java连接hadoop常见操作
/**
* 客户端代码相关流程
* 1获取一个客户端对象
* 2执行相关的操作命令
* 3关闭资源
* HDFS zookeeper都是这样执行的
*/
public class HdfsClient {
FileSystem fs = FileSystem.get(new Configuration());
public HdfsClient() throws IOException {
}
/**
* \
* 初始化客户端,便于以后调用
*/
@BeforeEach
public void init() throws URISyntaxException, IOException, InterruptedException {
URI uri = new URI("hdfs://192.168.10.102:8020");
// 集群的配置文件
Configuration configuration = new Configuration();
// configuration.set("dfs.client.use.datanode.hostname", "true");
// configuration.set();
String user = "caojun";
//获得了客户端对象
fs = FileSystem.get(uri, configuration, user);
}
/**
* 创建文件夹
*/
@Test
public void testMkdir() throws URISyntaxException, IOException, InterruptedException {
// 执行创建文件夹操作
boolean mkdirs = fs.mkdirs(new Path("/xiyou/huaguoshan2"));
}
/**
* 文件上传
* delsrc是否删除本地文件,overwrite是否可以覆盖
*/
@Test
public void testPut() throws IOException {
fs.copyFromLocalFile(false, true, new Path("D:\\xiyou\\huaguoshan1\\test.txt"), new Path("/xiyou/huaguoshan"));
}
/**
* 文件下载
* delSrc下载之后源文件是否删除,src源文件的路径,dst目标地址路径
* 下载之后会产生一个crc,是用来校验的
*/
@Test
public void testGet() throws IOException {
fs.copyToLocalFile(false, new Path("/xiyou/huaguoshan/test.txt"), new Path("d:\\"));
}
/**
* 文件删除
* b参数含义是是否递归删除rm-f
*/
@Test
public void testRm() throws IOException {
fs.delete(new Path("/xiyou/huaguoshan2"), true);
}
/**
* 文件的移动cp拷贝,mv移动
*/
@Test
public void testCp() throws IOException {
// 参数一源文件路径,参数二目标文件的路径
// 对文件名字的修改
fs.rename(new Path("/xiyou/huaguoshan/test.txt"), new Path("/xiyou/huaguoshan/ss.txt"));
// 移动的同时对文件进行更名
fs.rename(new Path("/xiyou/huaguoshan/ss.txt"), new Path("/input/ss2.txt"));
//目录更名
fs.rename(new Path("/input"), new Path("/output"));
}
/**
* 获取文件详情
*/
@Test
public void getDetail() throws IOException {
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
System.out.println("==========" + fileStatus.getPath() + "========");
System.out.println(fileStatus.getPermission());
System.out.println(fileStatus.getOwner());
System.out.println(fileStatus.getGroup());
System.out.println(fileStatus.getLen());
System.out.println(fileStatus.getModificationTime());
System.out.println(fileStatus.getBlockSize());
System.out.println(fileStatus.getPath().getName());
}
}
}
());
System.out.println(fileStatus.getBlockSize());
System.out.println(fileStatus.getPath().getName());
}
}
}