在mapreduce中进行作业链接时,如何解决chainmapper不适用于参数错误?

i2loujxw  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(198)

我正在使用hadoop1.2.1,eclipsejuno。我试图在一个mapreduce作业中链接三个map任务。在eclipse中编写mapreduce代码时,我遇到了一个错误,比如chainmapper不适用于参数,而且我不能设置inputpath。下面是我的mapreduce代码,

package org.myorg;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.util.StringTokenizer;

import javax.security.auth.login.Configuration;

import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.MapRunnable;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.apache.hadoop.mapred.lib.ChainMapper;
import org.apache.hadoop.mapred.lib.ChainReducer;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.net.StaticMapping;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class Recommand extends Configured implements Tool {

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

        public void map(LongWritable key, Text val, OutputCollector<Text, Text> output,Reporter reporter)throws IOException{
            String[] ids;
            String ln=val.toString();
            ids=ln.split("\t");
            output.collect(new Text(ids[0]),new Text(ids[1]));

    }
}
    public static class FtrMapper extends MapReduceBase implements Mapper<Text, Text, Text, Text>{
        public void map(Text key, Text val, OutputCollector<Text, Text>output, Reporter reporter) throws IOException{
            String[] str;

            String lne=val.toString();
        while(lne.contains("M1024")){
                str=lne.split(",");
            String[] str1=new String[str.length];
                for(int i=0;i<str.length;i++){
                                if(str[i]=="M1024"){   //hre need to give id which we need to split;
                                    continue;
                                        }
                                str1[i]=str[i];
                                output.collect(key,new Text(str1[i]));                  
//                          System.out.println("str1 out:"+str[i]); 
                                    }
                            }

        }
    }

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

    private final static IntWritable one=new IntWritable(1);
    private  Text word=new Text();
    public void map(Text key, Text val, OutputCollector<Text, IntWritable>output, Reporter reporter)throws IOException{
        String line = val.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, Iterable<IntWritable>values, OutputCollector<Text, IntWritable>output, Reporter reporter)throws IOException{
        int sum=0;
        for(IntWritable val:values){
            sum+=val.get();
                    }
        output.collect(key,new IntWritable(sum));
                    }
                }   

static int printUsage() {
    System.out.println("recommand  ");
    ToolRunner.printGenericCommandUsage(System.out);
    return -1;
}

public int run(String[] args) throws Exception {
    JobConf conf = new JobConf(getConf(), Recommand.class);
    conf.setJobName("wordcount");

    if (args.length != 2) {
        System.out.println("ERROR: Wrong number of parameters: " +
                args.length + " instead of 2.");
        return printUsage();
    }
    FileInputFormat.setInputPaths(conf, args[0]);
    FileOutputFormat.setOutputPath(conf, new Path(args[1]));

    conf.setInputFormat(TextInputFormat.class);
    conf.setOutputFormat(TextOutputFormat.class);

    JobConf mapAConf = new JobConf(false);
    ChainMapper.addMapper(conf, IdIndexMapper.class, LongWritable.class, Text.class, Text.class, Text.class, true, mapAConf);

    JobConf mapBConf = new JobConf(false);
    ChainMapper.addMapper(conf, FtrMapper.class, Text.class, Text.class, Text.class, Text.class, true, mapBConf);

    JobConf mapCConf = new JobConf(false);
    ChainMapper.addMapper(conf, CntMapper.class, Text.class, Text.class, Text.class, IntWritable.class, true, mapBConf);

    JobConf reduceConf = new JobConf(false);
    ChainReducer.setReducer(conf, Reduce.class, Text.class, IntWritable.class, Text.class, IntWritable.class, true, reduceConf);

    JobClient.runJob(conf);
    return 0;
}

public static void main(String[] args) throws Exception {
    int res = ToolRunner.run(new org.apache.hadoop.conf.Configuration(), Recommand(), args);
    System.exit(res);
}
}

有人能帮我解决这个问题吗?

rjzwgtxy

rjzwgtxy1#

请确保执行以下操作以避免此错误
两个类都扩展了 Mapper 班级。
这个 ChainMapper 所使用的类来自正确的api,以适用于您的代码的api为准。 org.apache.hadoop.mapreduce.lib.chain.ChainMapper 或导入 org.apache.hadoop.mapred.lib.ChainMapper

相关问题