본문 바로가기
Old Posts/Hadoop

[Hadoop] HDFS, Yarn, MapReduce를 위한 유닛테스트 및 JUnit 사용법 및 예제 - MiniCluster

by A6K 2021. 8. 8.

하둡 클러스터를 사용하는 클라이언트 프로그램을 작성하다보면 유닛테스트를 어떻게 해야하는지 고민이 생긴다. 유닛 테스트를 위한 테스트 클러스터를 별도로 구축해놓는 것도 이상하고, 실제 운영되는 클러스터에 유닛 테스트를 돌리는 것도 애매하다. 유닛 테스트는 대부분 간단한 동작만 테스트하는데 가벼운 방법이 필요하다.

다행히 하둡은 JUnit 같은 유닛 테스트 케이스 작성을 위한 '미니클러스터(Mini cluster)'를 제공한다. 하둡 클라이언트를 테스트하기위해 'MiniDFS', 'MiniYARN', 'MiniMR' 클러스터가 제공되며 이 포스트에서는 MiniDFS 클러스터를 이용해서 테스트케이스를 작성해보겠다.


MiniCluster 의존성 설정

메이븐 프로젝트를 사용하는 경우 다음 의존성을 pom.xml 파일에 추가해야한다. 테스트케이스 작성을 위한 JUnit과 테스트용 클러스터 사용을 위한 minicluster를 추가한다. 

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-minicluster</artifactId>
    <version>2.7.3</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

MiniDFSCluster를 이용한 JUnit 테스트 케이스

MiniCluster를 이용한 테스트 케이스는 다음과 같다.

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Random;

public class TestHDFSClusterExample {

    private static MiniDFSCluster cluster;

    @BeforeClass
    public static void setup() {

        File baseDir = new File("/tmp/testDir");

        FileUtil.fullyDelete(baseDir);

        Configuration conf = new Configuration();
        conf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, baseDir.getAbsolutePath());

        MiniDFSCluster.Builder builder = new MiniDFSCluster.Builder(conf);

        try {
            cluster = builder.build();

            String uri = "hdfs://localhost:" + cluster.getNameNodePort();

            Path path = new Path(uri + "/test/testFile");

            FileSystem fs = FileSystem.get(URI.create(uri), conf);

            FSDataOutputStream fos = fs.create(path, true);

            // Create random content
            Random random = new Random();

            for (int i = 0; i < 1024; i++)
                fos.write(random.nextInt());

            fos.close();

        } catch (IOException e) {
            Assert.fail();
        }
    }

    @AfterClass
    public static void cleanup() {

        cluster.shutdown();
    }

    @Test
    public void testReadData() {

        Configuration conf = new Configuration();

        String uri = "hdfs://localhost:" + cluster.getNameNodePort();

        Path path = new Path(uri + "/test/testFile");

        try {
            FileSystem fs = FileSystem.get(URI.create(uri), conf);

            FSDataInputStream fis = fs.open(path);

            long size = fs.getContentSummary(path).getLength();

            for (long i = 0; i < size; i++)
                System.out.println(fis.readByte());

            fis.close();

        } catch (IOException e) {
            Assert.fail();
        }
    }
}

임의의 바이트를 컨텐츠로 갖는 HDFS 파일을 생성하고 정상적으로 Read가 동작하는지 확인하는 테스트 케이스다. 읽는 동작이 제대로 되는지 System.out으로 출력해서 확인해보는 코드다.

@BeforeClass 애노테이션으로 우선 미니 클러스터를 생성하고, 파일을 만들어 둔다. 그리고 @Test 애노테이션이 달려있는 테스트 케이스를 수행하고, @AfeterClass 애노테이션이 달려있는 정리 작업을 통해 만들어둔 미니 클러스터를 정리해준다.

댓글