복붙노트

[HADOOP] 드라이버에서 MapReduce로 객체 전달하기

HADOOP

드라이버에서 MapReduce로 객체 전달하기

나는 config 파일을 읽는 드라이버를 만들었고 (config에 기반한) 객체 목록을 만들고 MapReduce (MapReduce는 그 객체 목록에 대한 참조를 보유하는 정적 속성을가집니다)에 그 목록을 전달합니다.

작동하지만 로컬에서만 작동합니다. 클러스터 구성에서 작업을 실행하자마자 목록이 작성되지 않았다는 것을 나타내는 모든 종류의 오류가 발생합니다. 그것은 내가 잘못하고 있다고 생각하게 만들고 MapReduce는 드라이버와 독립적으로 실행됩니다.

내 질문은 올바르게 매퍼를 초기화하는 방법입니다.

(저는 Hadoop 2.4.1을 사용하고 있습니다)

해결법

  1. ==============================

    1.이것은 측면 데이터 분배의 문제점과 관련이 있습니다.

    이것은 측면 데이터 분배의 문제점과 관련이 있습니다.

    측면 데이터 배포에는 두 가지 방법이 있습니다.

    1) 분산 캐시

    2) 구성

    공유 할 객체가 있으므로 Configuration 클래스를 사용할 수 있습니다.

    이 토론은 모든 Mappers 및 (또는) Reducers에서 액세스 할 수있는 클러스터에서 Object를 사용할 수 있도록 Configuration 클래스에 따라 달라집니다. 여기서의 접근법은 매우 간단합니다. Configuration 클래스의 setString (String, String) 설정자가이 작업을 수행하는 데 사용됩니다. 전역에서 공유해야하는 객체는 드라이버 끝에서 Java 문자열로 직렬화되며 매퍼 또는 감속기에서 객체로 다시 직렬화 해제됩니다.

    아래의 예제 코드에서는 com.google.gson.Gson 클래스를 사용하여 손쉬운 직렬화 및 비 직렬화를 수행했습니다. Java 직렬화를 사용할 수도 있습니다.

    공유해야하는 객체를 나타내는 클래스

     public class TestBean {
        String string1;
        String string2;
        public TestBean(String test1, String test2) {
            super();
            this.string1 = test1;
            this.string2 = test2;
        }
        public TestBean() {
            this("", "");
        }
        public String getString1() {
            return string1;
        }
        public void setString1(String test1) {
            this.string1 = test1;
        }
        public String getString2() {
            return string2;
        }
        public void setString2(String test2) {
            this.string2 = test2;
        }
    }
    

    구성을 설정할 수있는 기본 클래스

    public class GSONTestDriver {
        public static void main(String[] args) throws Exception {
            System.out.println("In Main");
            Configuration conf = new Configuration();
            TestBean testB1 = new TestBean("Hello1","Gson1");
            TestBean testB2 = new TestBean("Hello2","Gson2");
            Gson gson = new Gson();
            String testSerialization1 = gson.toJson(testB1);
            String testSerialization2 = gson.toJson(testB2);
            conf.set("instance1", testSerialization1);
            conf.set("instance2", testSerialization2);
            Job job = new Job(conf, " GSON Test");
            job.setJarByClass(GSONTestDriver.class);
            job.setMapperClass(GSONTestMapper.class);
            job.setNumReduceTasks(0);
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(NullWritable.class);
            FileInputFormat.addInputPath(job, new Path(args[0]));
            FileOutputFormat.setOutputPath(job, new Path(args[1]));
            job.waitForCompletion(true);
        }
    }
    

    객체를 검색 할 수있는 매퍼 클래스

    public class GSONTestMapper extends
    Mapper<LongWritable, Text, Text, NullWritable> {
        Configuration conf;
        String inst1;
        String inst2;
        public void setup(Context context) {
            conf = context.getConfiguration();
            inst1 = conf.get("instance1");
            inst2 = conf.get("instance2");
            Gson gson = new Gson();
            TestBean tb1 = gson.fromJson(inst1, TestBean.class);
            System.out.println(tb1.getString1());
            System.out.println(tb1.getString2());
            TestBean tb2 = gson.fromJson(inst2, TestBean.class);
            System.out.println(tb2.getString1());
            System.out.println(tb2.getString2());
        } 
        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            context.write(value,NullWritable.get());
        }
    }
    

    bean은 com.google.gson.Gson 클래스의 toJson (Object src) 메소드를 사용하여 직렬화 된 Json String으로 변환됩니다. 그런 다음 직렬화 된 Json 문자열은 구성 인스턴스를 통해 값으로 전달되고 매퍼의 이름으로 액세스됩니다. 문자열은 동일한 Gson 클래스의 fromJson (String json, Class classOfT) 메서드를 사용하여 직렬화가 해제됩니다. 내 테스트 콩 대신에 객체를 배치 할 수 있습니다.

  2. from https://stackoverflow.com/questions/26983741/passing-objects-to-mapreduce-from-a-driver by cc-by-sa and MIT license