-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
76
hadoop-demo/src/test/java/com/tomshidi/hadoop/HadoopCurdTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters