티스토리 뷰


하둡 맵리듀스 프로그래밍을 위해 이클립스 플러그 인을 컴파일하고 추가하는 작업 없이 


이클립스에서 맵리듀스 프로그래밍과 jar file 생성을 위한 방법들을 소개하고자 합니다.


이 작업은 리눅스 우분투 13.04에서 진행되었습니다.



1. 새 프로젝트 생성





먼저 File -> New -> Java Project 클릭후




프로젝트를 생성합니다.


hadoop 에서 제공하는 conf/  파일이 필요하기 때문에 이를 복사할 수 있도록 conf/라는 디렉토리를 생성합니다.






conf라는 디렉토리가 생성된 것을 확인할 수 있습니다.




2. 개발 환경 설정


생성한 프로젝트에 필요한 jar 파일들을 불러들여 lib에 추가시키는 작업을 합니다.


프로젝트 우클릭후 Properties를 클릭합니다. 


좌측메뉴의 Java Build Path 선택후 -> Libraries를 클릭합니다.





우측 메뉴에 있는 Add External JARs... 를 클릭하여 다음 파일들을 라이브러리에 추가 시킵니다.

hadoop/hadoop*.jar

hadoop/lib/*.jar





그리고 우측 메뉴의 Add Class Folder를 클릭하여 conf/ 디렉토리를 지정합니다.





그리고 hadoop/conf 파일들을 프로젝트 위에서 만들어두었던 conf 디렉토리로 복사해줍니다.




필요한 개발환경 설정은 완료되었습니다. 



3. WordCount 예제


하둡이 작동되고 있다는 가정하에 진행하도록 하겠습니다.



src폴더에 wordcount 클래스를 만든 후 jar 파일로 생성해보도록 합니다.







여기가면 소스코드가 있습니다. 참조하세요 

http://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html



Source Code 


import java.io.IOException;

import java.util.*;


import org.apache.hadoop.fs.Path;

import org.apache.hadoop.conf.*;

import org.apache.hadoop.io.*;

import org.apache.hadoop.mapred.*;

import org.apache.hadoop.util.*;


public class wordcount {

  public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {

    private final static IntWritable one = new IntWritable(1);

    private Text word = new Text();

    

    public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {

      String line = value.toString();

      StringTokenizer tokenizer = new StringTokenizer(line);

      while (tokenizer.hasMoreTokens()) {

        word.set(tokenizer.nextToken());

        output.collect(word, one);

      }

    }

  }

  public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {

    public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {

      int sum = 0;

      while (values.hasNext()) {

        sum += values.next().get();

      }

      output.collect(key, new IntWritable(sum));

    }

  }


  public static void main(String[] args) throws Exception{

    // TODO Auto-generated method stub

    JobConf conf = new JobConf(wordcount.class);

    conf.setJobName("wordcount");

    conf.setOutputKeyClass(Text.class);

    conf.setOutputValueClass(IntWritable.class);

    conf.setMapperClass(Map.class);

    conf.setCombinerClass(Reduce.class);

    conf.setReducerClass(Reduce.class);

    conf.setInputFormat(TextInputFormat.class);

    conf.setOutputFormat(TextOutputFormat.class);

    FileInputFormat.setInputPaths(conf, new Path(args[0]));

    FileOutputFormat.setOutputPath(conf, new Path(args[1]));

    JobClient.runJob(conf);


  }

}


위의 소스코드를 jar 파일로 생성해보도록 합니다




소스코드를 작성후 File -> Export 를 누르거나 


class 파일을 오른쪽키를 누른후 Export 메뉴를 선택합니다.





그리고 난 뒤 JAR file을 선택합니다.




그리고 나서 위치와 이름을 설정한후 finish를 눌러줍니다.




경고가 뜨지만 가볍게 무시해줍니다.




jar file이 잘 생성되어 있는 것을 확인 할 수 있습니다.





만들어 놓은 wordcount 프로그램이 올바로 작동하는지 확인해 볼 차례입니다.


먼저 input 파일을 생성합니다.


cat 명령어를 이용하여 file01과 file02를 생성합니다.





hadoop dfs  명령어를 이용해서 파일을 올립니다. 





그리고 난뒤 hadoop jar 명령어를 통해 wordcount를 실행하도록 합니다.


hadoop jar jar파일 패키지명.클래스명 인풋폴더 아웃풋폴더


이런식으로 명령어가 되어 있습니다.


명령어를 실행시키면 


hadoop@master:/usr/local/hadoop-1.2.1/wordcount$ ../bin/hadoop jar wordcount.jar wordcount.WordCount input output000

13/10/14 19:47:50 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.

13/10/14 19:47:50 INFO util.NativeCodeLoader: Loaded the native-hadoop library

13/10/14 19:47:50 WARN snappy.LoadSnappy: Snappy native library not loaded

13/10/14 19:47:50 INFO mapred.FileInputFormat: Total input paths to process : 2

13/10/14 19:47:50 INFO mapred.JobClient: Running job: job_201310141855_0003

13/10/14 19:47:51 INFO mapred.JobClient:  map 0% reduce 0%

13/10/14 19:47:56 INFO mapred.JobClient:  map 66% reduce 0%

13/10/14 19:47:57 INFO mapred.JobClient:  map 100% reduce 0%

13/10/14 19:48:04 INFO mapred.JobClient:  map 100% reduce 33%

13/10/14 19:48:05 INFO mapred.JobClient:  map 100% reduce 100%

13/10/14 19:48:06 INFO mapred.JobClient: Job complete: job_201310141855_0003

13/10/14 19:48:06 INFO mapred.JobClient: Counters: 30

13/10/14 19:48:06 INFO mapred.JobClient:   Job Counters 

13/10/14 19:48:06 INFO mapred.JobClient:     Launched reduce tasks=1

13/10/14 19:48:06 INFO mapred.JobClient:     SLOTS_MILLIS_MAPS=10694

13/10/14 19:48:06 INFO mapred.JobClient:     Total time spent by all reduces waiting after reserving slots (ms)=0

13/10/14 19:48:06 INFO mapred.JobClient:     Total time spent by all maps waiting after reserving slots (ms)=0

13/10/14 19:48:06 INFO mapred.JobClient:     Launched map tasks=3

13/10/14 19:48:06 INFO mapred.JobClient:     Data-local map tasks=3

13/10/14 19:48:06 INFO mapred.JobClient:     SLOTS_MILLIS_REDUCES=8891

13/10/14 19:48:06 INFO mapred.JobClient:   File Input Format Counters 

13/10/14 19:48:06 INFO mapred.JobClient:     Bytes Read=76

13/10/14 19:48:06 INFO mapred.JobClient:   File Output Format Counters 

13/10/14 19:48:06 INFO mapred.JobClient:     Bytes Written=77

13/10/14 19:48:06 INFO mapred.JobClient:   FileSystemCounters

13/10/14 19:48:06 INFO mapred.JobClient:     FILE_BYTES_READ=160

13/10/14 19:48:06 INFO mapred.JobClient:     HDFS_BYTES_READ=364

13/10/14 19:48:06 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=228362

13/10/14 19:48:06 INFO mapred.JobClient:     HDFS_BYTES_WRITTEN=77

13/10/14 19:48:06 INFO mapred.JobClient:   Map-Reduce Framework

13/10/14 19:48:06 INFO mapred.JobClient:     Map output materialized bytes=172

13/10/14 19:48:06 INFO mapred.JobClient:     Map input records=2

13/10/14 19:48:06 INFO mapred.JobClient:     Reduce shuffle bytes=172

13/10/14 19:48:06 INFO mapred.JobClient:     Spilled Records=28

13/10/14 19:48:06 INFO mapred.JobClient:     Map output bytes=126

13/10/14 19:48:06 INFO mapred.JobClient:     Total committed heap usage (bytes)=464519168

13/10/14 19:48:06 INFO mapred.JobClient:     CPU time spent (ms)=2210

13/10/14 19:48:06 INFO mapred.JobClient:     Map input bytes=70

13/10/14 19:48:06 INFO mapred.JobClient:     SPLIT_RAW_BYTES=288

13/10/14 19:48:06 INFO mapred.JobClient:     Combine input records=14

13/10/14 19:48:06 INFO mapred.JobClient:     Reduce input records=14

13/10/14 19:48:06 INFO mapred.JobClient:     Reduce input groups=11

13/10/14 19:48:06 INFO mapred.JobClient:     Combine output records=14

13/10/14 19:48:06 INFO mapred.JobClient:     Physical memory (bytes) snapshot=382443520

13/10/14 19:48:06 INFO mapred.JobClient:     Reduce output records=11

13/10/14 19:48:06 INFO mapred.JobClient:     Virtual memory (bytes) snapshot=1567129600

13/10/14 19:48:06 INFO mapred.JobClient:     Map output records=14




에러없이 잘 수행되었습니다.


input 디렉토리에 있는 file01과 file02의 내용을 읽어서 wordcount를 계산후 output000 디렉토리에 결과값이 저장되었습니다.


output000디렉토리를 $hadoop dfs -ls output000 명령어로 살펴보면 part-00000 이라는 결과값과 로그등이 저장되어 있음을 확인할 수 있습니다.




cat 명령어를 통해 part-00000을 살펴보면 





결과값이 나온것을 확인할 수 있습니다 


이클립스를 통한 맵리듀스 컴파일이 잘 작동되는 것을 확인 할 수 있습니다. 


이것으로 글작성을 마치겠습니다 (_ _)





ps. 이 글을 작성하면서 수많은 에러를 겪었고(-_-....) 에러를 수정해가면서 이상적으로 수행된것처럼 보이도록 노력했습니다. 


(그림을 잘 보면 hostname이 다릅니다 -_-........)



참조 및 출처 : http://blog.acronym.co.kr/333



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함