Skip to content

Commit

Permalink
feat: 增加hadoop使用用例
Browse files Browse the repository at this point in the history
  • Loading branch information
TomShiDi committed Feb 4, 2024
1 parent 44650de commit 77b7d99
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
38 changes: 38 additions & 0 deletions hadoop-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.tomshidi</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>hadoop-demo</artifactId>
<name>hadoop示例</name>

<properties>
<hadoop-version>2.7.5</hadoop-version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop-version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop-version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop-version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>${hadoop-version}</version>
</dependency>
</dependencies>
</project>
76 changes: 76 additions & 0 deletions hadoop-demo/src/test/java/com/tomshidi/hadoop/HadoopCurdTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.tomshidi.hadoop;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* @author TomShiDi
* @since 2024/2/4 16:25
*/
public class HadoopCurdTest {

private static FileSystem fileSystem;

@BeforeAll
public static void init() throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://node1:8020");
fileSystem = FileSystem.get(conf);
}

@Test
public void getFileSystem() throws IOException {
System.out.println(fileSystem.toString());
}

@Test
public void listFiles() throws IOException {
RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("/"), true);
while (files.hasNext()) {
LocatedFileStatus fileStatus = files.next();
System.out.println(fileStatus.getPath().toString());
}
fileSystem.close();
}

/**
* 创建目录
* @throws IOException IO异常
*/
@Test
public void createDir() throws IOException {
fileSystem.create(new Path("/xxx/yyy/ccc"));
}

@Test
public void downloadFile1() throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(new File("E:\\hadoop-data.txt"));
FSDataInputStream inputStream = fileSystem.open(new Path("/data.txt"));
IOUtils.copy(inputStream, fileOutputStream);
IOUtils.closeQuietly(fileOutputStream);
IOUtils.closeQuietly(inputStream);
}

@Test
public void downloadFile2() throws IOException {
fileSystem.copyToLocalFile(new Path("/data.txt"), new Path("E:\\hadoop-data.txt"));
}

@Test
public void uploadFile() throws IOException {
fileSystem.copyFromLocalFile(new Path("E:\\hadoop-conf.zip"), new Path("/hadoop-conf.zip"));
}

@AfterAll
public static void destroy() throws IOException {
fileSystem.close();
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<module>base</module>
<module>mybatis-demo</module>
<module>zookeeper-demo</module>
<module>hadoop-demo</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
Expand Down

0 comments on commit 77b7d99

Please sign in to comment.