SpringBoot HDFS 的整合及使用

SpringBoot HDFS 的整合及使用。~~~xml <?xml version="1.0" encoding="UTF-8"?>

整合

 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
42
43
44
45
46
47
<?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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
    </parent>

    <groupId>org.gear</groupId>
    <artifactId>springboot-hdfs-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <hadoop.version>3.2.1</hadoop.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>${hadoop.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
    </dependencies>

</project>

使用

 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
final static String hdfsUrl = "hdfs://localhost:8020";


public void testLargeNumberFileUpload() throws URISyntaxException, IOException, InterruptedException {
    long startTime = System.currentTimeMillis();
    System.out.println(new DateTime(startTime));
    var rootPath = "E:/Data/wuhan-polygon-tile";
    Configuration configuration = new Configuration();
    configuration.set("fs.defaultFS", hdfsUrl);
    FileSystem fs = FileSystem.get(new URI(hdfsUrl), new Configuration(), "test");
    FileUtil.walkFiles(new File(rootPath), file -> {
        if (file.isFile()) {
            String fileName = file.getName();
            String filePath = file.getAbsolutePath();
            String targetPath = file.getAbsolutePath().replace("E:\\", "").replace("\\", "/");
            try {
                fs.copyFromLocalFile(false, new Path(filePath), new Path(targetPath));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
    long allTime = System.currentTimeMillis() - startTime;
    System.out.println(new DateTime(allTime));
}

封装

Gear(夕照)的博客。记录开发、生活,以及一些不足为道的思考……