本文最后更新于:2022-02-18T20:30:28+08:00
客户端环境配置
首先需要在本地安装和配置Hadoop。
下载一份hadoop在本地解压,然后配置HADOOP_HOME。idea运行时会读这个环境变量然后找到他里面的bin文件,所以本地的hadoop只需要有bin目录即可。
并且里面缺少了winutils.exe和hadoop.dll,需要额外下载
steveloughran/winutils:
Windows binaries for Hadoop versions (built from the git commit ID used
for the ASF relase) (github.com)
之后创建Maven工程,并导入相应的依赖坐标(其中hadoop-client的版本需要和服务器上对应)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.syh</groupId> <artifactId>hadoop</artifactId> <version>1.0-SNAPSHOT</version>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties>
<dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.30</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>3.1.3</version> <scope>test</scope> </dependency>
</dependencies>
</project>
|
API操作
创建HdfsClient类完成对应的API操作测试
首先是集群的连接和断开
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class HdfsClient {
private FileSystem fs;
@Before public void init() throws URISyntaxException, IOException, InterruptedException { URI uri = new URI("hdfs://hadoop102:8020"); Configuration configuration = new Configuration(); String user = "syh"; fs = FileSystem.get(uri, configuration, user); }
@After public void close() throws IOException { fs.close(); } }
|
1. 文件夹创建
1
| fs.mkdirs(new Path("/xiyou/huaguoshan"));
|
2. 文件上传和下载
1 2
| fs.copyFromLocalFile(true, true, new Path("d:/sunwukong.txt"), new Path("/xiyou/huaguoshan/")); fs.copyToLocalFile(false, new Path("/xiyou/huaguoshan/sunwukong.txt"), new Path("d:/zhubajie.txt"), false);
|
- copyFromLocalFile可以指定对源文件是否删除,是否允许覆盖
- copyToLocalFile可以指定对源文件是否删除,是否开启校验
- 最后一个参数指定为false,则开启校验,下载来的文件会有
.crc
文件
3. 文件更名和移动
1 2
| fs.rename(new Path("/xiyou"), new Path("/xiyou-temp"));
|
4. 文件删除
1 2
| fs.delete(new Path("/xiyou-temp/temphaha"), true);
|
5. 文件详情查看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true); while (files.hasNext()) { LocatedFileStatus fileStatus = files.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.getReplication()); System.out.println(fileStatus.getBlockSize()); System.out.println(fileStatus.getPath().getName());
BlockLocation[] blockLocations = fileStatus.getBlockLocations(); System.out.println(Arrays.toString(blockLocations)); }
|
6. 判断文件或者文件夹
1 2 3 4 5 6 7 8 9
| FileStatus[] fileStatuses = fs.listStatus(new Path("/xiyou-temp"));
for (FileStatus fileStatus : fileStatuses) { if (fileStatus.isFile()) { System.out.println("文件 + " + fileStatus.getPath().getName()); } else { System.out.println("目录 + " + fileStatus.getPath().getName()); } }
|