如何在mapreduce中作为参数传递文件

mznpcxlj  于 2021-06-02  发布在  Hadoop
关注(0)|答案(3)|浏览(262)

我想在文件中搜索特定单词并显示其计数。当要搜索的单词是单个单词时,我可以在驱动程序中设置如下配置:
驾驶员等级:

Configuration conf = new Configuration();
    conf.set("wordtosearch", "fun");

Map器类:

public static class SearchMapper extends
        Mapper<LongWritable, Text, Text, IntWritable> {
    // Map code goes here.
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map (LongWritable Key, Text value,Context context )throws IOException,InterruptedException{

         Configuration conf = context.getConfiguration();
            //retrieve the wordToSearch variable
            String wordToSearch = conf.get("wordtosearch");
        String txt= value.toString();

        if(txt.compareTo(wordToSearch)==0){
            word = context.getCurrentValue();
            context.getCurrentKey();
            word.set(txt);
            context.write(word, one);

        }

但是当文件中有一个单词列表时,我不知道如何传递它。有些帖子提到使用分布式缓存,但在这样做时,我得到了“分布式缓存已弃用”的错误。在新的api中有没有类似的方法来传递文件?

gkn4icbw

gkn4icbw1#

是的,在新的api中也有一种方法。
首先,将文件存储在hdfs中。然后,在driver类(在main方法中)中,执行以下操作:

Configuration conf = getConf(); 
... 
Job job = Job.getInstance(conf); ...   
job.addCacheFile(new Path(filename).toUri());

最后,在mapper类中(例如 setup() 方法),请执行以下操作:

URI[] localPaths = context.getCacheFiles();

如果只有一个文件,则应将其存储在 localPaths[0] .

dwbf0jvd

dwbf0jvd2#

你可以试试this:judge the 参数wether是一个文件,然后根据参数的类型分别执行操作

v1uwarro

v1uwarro3#

如果单词列表的大小合理,您仍然可以将其传递给配置:
驱动程序类:读取文件
driver类:在配置中添加单词列表,例如conf.set(“wordlisttosearch”,”fun:foo:巴“
mapper类:阅读配置并检索单词列表

相关问题