복붙노트

[HADOOP] 의 MapReduce에서 어떻게 감속기 매퍼 값 [중복] ArrayList를 보낼

HADOOP

의 MapReduce에서 어떻게 감속기 매퍼 값 [중복] ArrayList를 보낼

우리는 어떻게 감속기에 매퍼에서 값으로 ArrayList에 전달할 수 있습니다.

내 코드는 기본적으로 작업하고 목록에서 (규칙 실행 후 생성 된) 모든 출력을 유지하고있는 그 규칙에 따라 새 값 (String)를 만들 것이며, 지금이 출력 (매퍼 값)를 보낼 필요가 특정 규칙이있다 감속기와 그렇게 할 수있는 방법이 없습니다.

어떤 사람이 방향으로 날 지점시겠습니까

코드 추가

package develop;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;

import utility.RulesExtractionUtility;

public class CustomMap{


    public static class CustomerMapper extends Mapper<Object, Text, Text, Text> {
        private Map<String, String> rules;
        @Override
        public void setup(Context context)
        {

            try
            {
                URI[] cacheFiles = context.getCacheFiles();
                setupRulesMap(cacheFiles[0].toString());
            }
            catch (IOException ioe)
            {
                System.err.println("Error reading state file.");
                System.exit(1);
            }

        }

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

//          Map<String, String> rules = new LinkedHashMap<String, String>();
//          rules.put("targetcolumn[1]", "ASSIGN(source[0])");
//          rules.put("targetcolumn[2]", "INCOME(source[2]+source[3])");
//          rules.put("targetcolumn[3]", "ASSIGN(source[1]");

//          Above is the "rules", which would basically create some list values from source file

            String [] splitSource = value.toString().split(" ");

            List<String>lists=RulesExtractionUtility.rulesEngineExecutor(splitSource,rules);

//          lists would have values like (name, age) for each line from a huge text file, which is what i want to write in context and pass it to the reducer.
//          As of now i havent implemented the reducer code, as m stuck with passing the value from mapper.

//          context.write(new Text(), lists);---- I do not have a way of doing this


        }




        private void setupRulesMap(String filename) throws IOException
        {
            Map<String, String> rule = new LinkedHashMap<String, String>();
            BufferedReader reader = new BufferedReader(new FileReader(filename));
            String line = reader.readLine();
            while (line != null)
            {
                String[] split = line.split("=");
                rule.put(split[0], split[1]);
                line = reader.readLine();

                // rules logic
            }
            rules = rule;
        }
    }
    public static void main(String[] args) throws IllegalArgumentException, IOException, ClassNotFoundException, InterruptedException, URISyntaxException {


    Configuration conf = new Configuration();
    if (args.length != 2) {
        System.err.println("Usage: customerMapper <in> <out>");
        System.exit(2);
    }
    Job job = Job.getInstance(conf);
    job.setJarByClass(CustomMap.class);
    job.setMapperClass(CustomerMapper.class);
    job.addCacheFile(new URI("Some HDFS location"));


    URI[] cacheFiles= job.getCacheFiles();
    if(cacheFiles != null) {
        for (URI cacheFile : cacheFiles) {
            System.out.println("Cache file ->" + cacheFile);
        }
    }
    // job.setReducerClass(Reducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);

    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));

    System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

해결법

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

    1.감속기에 매퍼에서 ArrayList에 통과하기 위해서는 객체가 쓰기 가능한 인터페이스를 구현해야합니다 분명하다. 왜이 라이브러리를 시도하지?

    감속기에 매퍼에서 ArrayList에 통과하기 위해서는 객체가 쓰기 가능한 인터페이스를 구현해야합니다 분명하다. 왜이 라이브러리를 시도하지?

    <dependency>
        <groupId>org.apache.giraph</groupId>
        <artifactId>giraph-core</artifactId>
        <version>1.1.0-hadoop2</version>
    </dependency>
    

    그것은 추상 클래스가 있습니다 :

    public abstract class ArrayListWritable<M extends org.apache.hadoop.io.Writable>
    extends ArrayList<M>
    implements org.apache.hadoop.io.Writable, org.apache.hadoop.conf.Configurable
    

    당신은 당신의 자신의 클래스와 소스 코드를 추상 메소드를 작성하고 코드와 인터페이스 메소드를 구현을 만들 수 있습니다. 예를 들어 :

    public class MyListWritable extends ArrayListWritable<Text>{
        ...
    }
    
  2. ==============================

    2.방법이,하는 것입니다 (아마도에만도 최고의 하나되지 않음) 그렇게하기

    방법이,하는 것입니다 (아마도에만도 최고의 하나되지 않음) 그렇게하기

    이렇게하면, 당신은 또한 직렬화 된 목록이 포함 된 문자열 (예를 들어 \ n 또는 \의 t와 같은 문자)의 모든 특수 기호를 제거해야한다. 그것을 달성하는 쉬운 방법은 base64로 인코딩 된 문자열을 사용하는 것입니다.

  3. ==============================

    3.당신은 텍스트 객체 대신 String 객체를 보내야합니다. 그런 다음 당신은 당신의 감속기에는 Object.toString ()를 사용할 수 있습니다. 제대로 드라이버를 config (설정)해야합니다.

    당신은 텍스트 객체 대신 String 객체를 보내야합니다. 그런 다음 당신은 당신의 감속기에는 Object.toString ()를 사용할 수 있습니다. 제대로 드라이버를 config (설정)해야합니다.

    당신이 당신의 코드를 게시 할 경우 우리는 추가로 도움이 될 것입니다.

  4. from https://stackoverflow.com/questions/30945769/in-a-mapreduce-how-to-send-arraylist-as-value-from-mapper-to-reducer by cc-by-sa and MIT license