이번 강좌에서는 하둡의 핵심 컴포넌트 중 하나인 MapReduce에 대해 다뤄보겠다.
MapReduce는 대규모 데이터를 처리하기 위한 프로그래밍 모델로, 데이터를 분산하여 처리하고 병합하는 과정을 효율적으로 수행할 수 있다.
이번 강의에서는 MapReduce의 기본 개념과 구조를 살펴본 후, 간단한 예제를 통해 실습해보자.
MapReduce는 크게 두 가지 작업으로 나뉜다:
MapReduce의 처리 과정은 다음과 같이 요약할 수 있다:
가장 기본적인 MapReduce 예제는 단어 빈도수를 세는 작업이다.
주어진 텍스트에서 각 단어가 몇 번 등장하는지 계산하는 것이다.
다음 예제를 통해 이 과정을 구현해보자.
먼저 Mapper 클래스를 정의해보자. Mapper는 입력 데이터를 처리하고, 각 단어를 키로, 값은 1
로 매핑한다.
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] words = line.split("\\s+"); // 공백을 기준으로 단어를 나눈다.
for (String w : words) {
word.set(w);
context.write(word, one); // (단어, 1) 형태로 출력
}
}
}
Reducer 클래스는 Mapper에서 생성된 (단어, 1)
형태의 쌍을 받아, 각 단어의 빈도수를 계산한다.
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get(); // 각 단어의 값(1)을 합산한다.
}
context.write(key, new IntWritable(sum)); // (단어, 빈도수) 형태로 출력
}
}
마지막으로 Driver 클래스는 MapReduce 작업을 설정하고 실행하는 역할을 한다.
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCountDriver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCountDriver.class);
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0])); // 입력 경로 설정
FileOutputFormat.setOutputPath(job, new Path(args[1])); // 출력 경로 설정
System.exit(job.waitForCompletion(true) ? 0 : 1); // 작업 완료 여부에 따라 종료
}
}
Hadoop MapReduce 프로그램은 Java로 작성되므로, 컴파일한 후 JAR 파일로 패키징해야 한다.
다음 명령어를 통해 Maven을 사용하여 빌드하자.
mvn clean package
이 명령어는 Maven 프로젝트를 빌드하고, target/
디렉토리에 JAR 파일을 생성한다.
MapReduce를 실행하기 전에, 처리할 입력 데이터를 HDFS에 업로드해야 한다. input.txt
라는 파일을 HDFS에 업로드하자.
hdfs dfs -put input.txt /user/hadoop/wordcount/input
이제 MapReduce 작업을 실행할 차례이다. 다음 명령어를 통해 WordCount 작업을 실행한다.
hadoop jar target/wordcount-1.0.jar WordCountDriver /user/hadoop/wordcount/input /user/hadoop/wordcount/output
/user/hadoop/wordcount/input
은 입력 데이터가 있는 경로이고,/user/hadoop/wordcount/output
은 출력 데이터가 저장될 경로이다.MapReduce 작업이 완료되면, 출력 경로에 결과가 저장된다.
결과 파일을 확인해보자.
hdfs dfs -cat /user/hadoop/wordcount/output/part-r-00000
이 명령어는 MapReduce 작업의 결과를 출력한다.
이번 강좌에서는 MapReduce의 개념을 배우고, 간단한 WordCount 예제를 통해 MapReduce 작업을 실행하는 방법을 익혔다.
MapReduce는 하둡의 핵심 컴포넌트 중 하나로, 대규모 데이터를 병렬로 처리할 수 있는 강력한 도구이다.
앞으로의 강좌에서는 더 복잡한 MapReduce 작업을 다루고, 다양한 실습을 통해 이를 익히는 시간을 가져보겠다.
다음 강좌에서는 하둡 에코시스템의 다른 중요한 컴포넌트들, 예를 들어 YARN과 Hive 등을 다뤄보겠다.