Maven 프로젝트를 통해 HDFS에서 사용할 수 있는 Java API를 개발한다.
기본적인 dependency는 hadoop-client가 필요하며, java compiler의 version은 1.8로 정해둔다.
<!-- pom.xml -->
<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.fastcampus.hadoop</groupId>
<artifactId>hadoop-ws-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<hadoop.version>3.3.4</hadoop.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
</project>
XML
복사
간단한 Java API로 HDFS 내의 파일을 표준 출력하는 API를 구성한다.
package com.fastcampus.hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
public class FileSystemPrint {
public static void main(String[] args) throws IOException {
String url = args[0];
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(url), conf);
try(InputStream in = fs.open(new Path(url))) {
IOUtils.copyBytes(in, System.out, 4096, false);
}
}
}
Java
복사
mvn package 명령어를 통해 프로젝트를 빌드한다. 만들어진 API는 다음과 같이 실행할 수 있다.
# hadoop jar <api archive path> <api main class> <file path in hdfs>
> hadoop jar share/hadoop/client/hadoop-ws-api-1.0.0-SNAPSHOT.jar com.fastcampus.hadoop.FileSystemPrint /user/malachai/input/LICENSE.txt
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
...
Bash
복사
다음은 HDFS 내의 디렉토리 상태를 표준 출력하는 API를 구성한다.
package com.fastcampus.hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.URI;
public class FileSystemListStatus {
public static void main(String[] args) throws IOException {
String url = args[0];
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(url), conf);
Path path = new Path(url);
FileStatus[] status = fs.listStatus(path);
Path[] listPath = FileUtil.stat2Paths(status);
for(Path p : listPath) {
System.out.println(p);
}
}
}
Java
복사
mvn package 명령어를 통해 프로젝트를 빌드한 후, 다음과 같이 실행한다.
# hadoop jar <api archive path> <api main class> <directory path in hdfs>
> hadoop jar share/hadoop/client/hadoop-ws-api-1.0.0-SNAPSHOT.jar com.fastcampus.hadoop.FileSystemListStatus /user/malachai/input
hdfs://localhost:9000/user/malachai/input/LICENSE.txt
Bash
복사
로컬 파일을 HDFS 로 복사하는 API는 다음과 같다.
package com.fastcampus.hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import java.io.*;
import java.net.URI;
public class FileSystemCopyFromLocal {
public static void main(String[] args) throws IOException {
String localSrc = args[0];
String dest = args[1];
Configuration conf = new Configuration();
InputStream in = new BufferedInputStream(new FileInputStream(localSrc));
FileSystem fs = FileSystem.get(URI.create(dest), conf);
OutputStream out = fs.create(new Path(dest));
IOUtils.copyBytes(in, out, 4096, true);
}
}
Java
복사
로컬파일을 향하는 InputStream과 hdfs 파일을 향하는 OutputStream을 만들어둔다. 디후, IOUtils의 copyBytes 메소드를 통해 stream을 통해 읽어들이는 데이터를 InputStream에서 OutputStream으로 복사한다.
# hadoop jar <api archive path> <api main class> <file path in local> <file path in hdfs>
> hadoop jar share/hadoop/client/hadoop-ws-api-1.0.0-SNAPSHOT.jar com.fastcampus.hadoop.FileSystemCopyFromLocal README.txt /user/malachai/input/README.txt
> hadoop fs -ls /user/malachai/input
Found 2 items
-rw-r--r-- 1 root supergroup 15217 2022-11-29 05:55 /user/malachai/input/LICENSE.txt
-rw-r--r-- 1 root supergroup 175 2022-11-29 09:05 /user/malachai/input/README.txt
Bash
복사