加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Hadoop – mapper的构造函数args

发布时间:2020-12-14 14:26:26 所属栏目:Java 来源:网络整理
导读:有没有办法在Hadoop中为Mapper提供构造函数args?可能通过一些包装创造就业的图书馆? 这是我的情景: public class HadoopTest { // Extractor turns a line into a "feature" public static interface Extractor { public String extract(String s); } //
有没有办法在Hadoop中为Mapper提供构造函数args?可能通过一些包装创造就业的图书馆?

这是我的情景:

public class HadoopTest {

    // Extractor turns a line into a "feature"
    public static interface Extractor {
        public String extract(String s);
    }

    // A concrete Extractor,configurable with a constructor parameter
    public static class PrefixExtractor implements Extractor {
        private int endIndex;

        public PrefixExtractor(int endIndex) { this.endIndex = endIndex; }

        public String extract(String s) { return s.substring(0,this.endIndex); }
    }

    public static class Map extends Mapper<Object,Text,Text> {
        private Extractor extractor;

        // Constructor configures the extractor
        public Map(Extractor extractor) { this.extractor = extractor; }

        public void map(Object key,Text value,Context context) throws IOException,InterruptedException {
            String feature = extractor.extract(value.toString());
            context.write(new Text(feature),new Text(value.toString()));
        }
    }

    public static class Reduce extends Reducer<Text,Text> {
        public void reduce(Text key,Iterable<Text> values,InterruptedException {
            for (Text val : values) context.write(key,val);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = new Job(conf,"test");
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        FileInputFormat.addInputPath(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        job.waitForCompletion(true);
    }
}

应该清楚,因为Mapper仅作为类引用(Map.class)提供给配置,所以Hadoop无法传递构造函数参数并配置特定的Extractor.

有一些Hadoop包装框架就像Scoobi,Crunch,Scrunch(可能还有更多我不知道的)似乎有这种能力,但我不知道他们是如何实现的.编辑:在与Scoobi合作之后,我发现我对此有些不对劲.如果在“映射器”中使用外部定义的对象,则Scoobi要求它是可序列化的,并且如果不是,则会在运行时进行抱怨.所以也许正确的方法就是让我的Extractor可以在Mapper的设置方法中进行序列化和反序列化……

此外,我实际上在Scala工作,所以非常欢迎基于Scala的解决方案(如果不鼓励!)

解决方法

我建议通过您正在创建的配置对象告诉您的映射器使用哪个提取器.映射器在其设置方法(context.getConfiguration())中接收配置.看起来您不能将对象放在配置中,因为它通常是从XML文件或命令行构造的,但您可以设置枚举值并让映射器自己构造其提取器.在创建映射器之后定制映射器并不是很漂亮,但这就是我对API的解释.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读