hadoop mapreduce reducer合并器输入

lvmkulzt  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(390)

我正在学习一些mapreduce,但遇到了一些问题,情况是这样的:我有两个文件:“用户”包含一个用户列表,其中包含一些数据(性别、年龄、国家等)。该文件如下所示:

user_000003  m  22  United States   Oct 30, 2005

“歌曲”包含所有用户收听的歌曲的数据(用户id、收听日期和时间、艺术家id、艺术家名称、歌曲id、歌曲标题):

user_000999 2008-12-11T22:52:33Z    b7ffd2af-418f-4be2-bdd1-22f8b48613da    Nine Inch Nails 1d1bb32a-5bc6-4b6f-88cc-c043f6c52509    7 Ghosts I

我们的目标是在某些国家找到最受欢迎的歌曲。与k和一个国家名单提供的投入。
我决定对Map器使用multipleinputs类,这样一个Map器将输出一组键值对,如下所示:。另一个Map器将输出。据我所知,我应该能够读取reducer中与某个键配对的所有值(因此我应该在与userid相关联的值列表中找到国家和一定数量的歌曲),并输出一组带有配对的文件,以供另一个mapreduce作业使用。
我很肯定Map程序能完成他们的工作,因为我能用reducer编写它们的输出。
更新:文件通过以下代码传递给Map器:

Job job = Job.getInstance(conf);

        MultipleInputs.addInputPath(job, new Path(songsFile), TextInputFormat.class, SongMapper.class);
        MultipleInputs.addInputPath(job, new Path(usersFile), TextInputFormat.class, UserMapper.class);

        FileOutputFormat.setOutputPath(job, new Path(outFile));
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        job.setJarByClass(Songs.class);

        job.setCombinerClass(Combiner.class);
        job.setReducerClass(UserSongReducer.class);

Map器代码:

public static class UserMapper extends Mapper<LongWritable, Text, Text, Text>{

        //empty cleanUp()

        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {

            String record = value.toString();               
            String[] userData = record.split("\t");
            if(userData.length>3 && !userData[3].equals(""))
            {
                context.write(new Text(userData[0]), new Text(userData[3]));
            }        

        }

        //empty run() and setup()

    }

    public static class SongMapper extends Mapper<LongWritable, Text, Text, Text>{

        //empty cleanUp()

        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {

            String record = value.toString();               
            String[] songData = record.split("\t");
            if(songData.length>3 && !songData[3].equals(""))
            {
                context.write(new Text(songData[0]), new Text(songData[5]+" |||  "+songData[3]));
            }        

        }

        //empty run() and setup()

    }

组合器代码:

public static class Combiner extends Reducer<Text, Text, Text, Text> 
    {

        private boolean isCountryAllowed(String toCheck, String[] countries)
        {

            for(int i=0; i<countries.length;i++)
            {
                if(toCheck.equals(countries[i]))
                    return true;
            }
            return false;
        }

        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException 
        {

            ArrayList<String> list = new ArrayList<String>();

            String country = "foo";
            for(Text value : values) 
            {
                if(!value.toString().contains(" ||| "))
                {
                    country = value.toString();
                }else
                {
                    list.add(value.toString());
                }

            }

            if(isCountryAllowed(country, context.getConfiguration().getStrings("countries")))
            {

                for (String listVal : list) 
                {
                    context.write(new Text(country), 
                            new Text(listVal));
                }
            }

         }
    }

当我尝试用减速机输出对时,问题来了:

public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException 
        {

             for (Text value : values) 
            {
                context.write(key,value);
            }
            }

         }

我使用“| | |”来构建艺术家+标题字符串问题是国家仍然是“foo”。我想我应该至少看到一行输出,并以正确的国家作为键,但输出总是“foo”(2,5kb歌曲文件):

foo Deep Dish |||  Fuck Me Im Famous (Pacha Ibiza)-09-28-2007
foo Vnv Nation |||  Kingdom
foo Les Fleur De Lys |||  Circles
foo Home Video |||  Penguin
foo Of Montreal |||  Will You Come And Fetch Me
foo Godspeed You! Black Emperor |||  Bbf3
foo Alarum |||  Sustained Connection
foo Sneaker Pimps |||  Walking Zero
foo Cecilio And Kapono |||  I Love You
foo Garbage |||  Vow
foo The Brian Setzer Orchestra |||  Gettin' In The Mood
foo Nitin Sawhney |||  Sunset (J-Walk Remix)
foo Nine Inch Nails |||  Heresy
foo Collective Soul |||  Crowded Head
foo Vicarious Bliss |||  Limousine
foo Noisettes |||  Malice In Wonderland
foo Black Rebel Motorcycle Club |||  Lien On Your Dreams
foo Mae |||  Brink Of Disaster
foo Michael Andrews |||  Rosie Darko
foo A Perfect Circle |||  Blue

我做错什么了?
ps我应该可以避免第二个工作,如果我使用一个自定义组合器,组合器的行为完全像一个减速机?

uttx8gqw

uttx8gqw1#

从你的代码我看到,这个国家应该永远是“foo”,你到底想达到什么目的?

// IN YOUR MAPPER THE VALUE IS WRITTEN USING |||

context.write(new Text(songData[0]), new Text(songData[5]+" |||  "+songData[3]));

// THEN VALUE WILL ALWAYS CONTAIN ||| AND WILL NEVER CHANGE THE COUNTRY THAT WAS SET TO TRUE
 String country = "foo";
     if(!value.toString().contains(" ||| ")) //never ---

然后输出国家变量:

context.write(new Text(country),

相关问题