当前位置: 首页 > 知识库问答 >
问题:

Hadoop中的MapReduce程序,它实现了一个简单的“您可能认识的人”

陶涵育
2023-03-14

输入文件包含邻接列表,并具有以下格式的多行:

import java.io.IOException; 
import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.io.IntWritable; 
import org.apache.hadoop.io.LongWritable; 
import org.apache.hadoop.io.Text; 
import org.apache.hadoop.mapreduce.Job; 
import org.apache.hadoop.mapreduce.Mapper; 
import org.apache.hadoop.mapreduce.Reducer; 
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class Friends 
{

public class FriendsMap extends Mapper < LongWritable, Text, Text, IntWritable >
    {
    private Text friendsAB;
    private Text friendsBA;
    private IntWritable one = new IntWritable(1);
    private IntWritable oneLess = new IntWritable(-999999999);
        //@SuppressWarnings("null")
        @Override 
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException 
        { 
            String friendsOfA[] = null;     //This will be all of the friends of the user in this row
            String oneRow[] = value.toString().split("\t,");    //Break the row up into users IDs
            String userA = oneRow[0];       //This is the main user for this row
            for (int i=1; i < oneRow.length; i++)   //Create an array of the rest of the users in this row
            {
                friendsOfA[i-1] = oneRow[i];
            }
            for (int i=0; i < oneRow.length; i++)   //Output the main user in pairs with all friends plus a lagre negative #
            {
                friendsAB.set(userA + " " + friendsOfA[i]);
                context.write(friendsAB, oneLess);
                System.out.println(friendsAB + " " + oneLess);
            }
            for (int i = 0; i < friendsOfA.length; i++)     //Output each friend pair plus the number 1
            {
                for (int j = i + 1; j < friendsOfA.length; j++) 
                {
                    friendsAB.set(friendsOfA[i] + " " + friendsOfA[j]);
                    friendsBA.set(friendsOfA[j] + " " + friendsOfA[i]);
                    context.write(friendsAB, one);
                    context.write(friendsBA, one);
                    System.out.println(friendsAB + " " + one);
                    System.out.println(friendsBA + " " + one);
                }
            }
        }
    }

class FriendReducer extends Reducer < Text, IntWritable, Text, IntWritable > 
    { 
        private IntWritable result = new IntWritable(); 
        @Override 
        public void reduce( Text key, Iterable < IntWritable > values, Context context) throws IOException, InterruptedException 
        { 
            int sum = 0; 
            for (IntWritable val : values) 
            { 
                sum += val.get(); 
            } 
            if (sum > 1)
            {
                result.set( sum); 
                context.write( key, result);
            }
            //At this point I have all pairs of users with recomenede friends and a count of how many times they each
            //friend has been recomended to a user.
            //I need to sort by user and then by number of recomendations.
            //Then print the user <tab> all recomendations with commas between them.
        } 
    }


public static void main( String[] args) throws Exception 
    { 
        Configuration conf = new Configuration();
        Job job = Job.getInstance( conf, "Friends");
        job.setJarByClass(Friends.class);
        FileInputFormat.addInputPath( job, new Path("input")); 
        FileOutputFormat.setOutputPath( job, new Path("output")); 
        job.setMapperClass( FriendsMap.class); 
        job.setCombinerClass( FriendReducer.class); 
        job.setReducerClass( FriendReducer.class);
        job.setOutputKeyClass( Text.class); 
        job.setOutputValueClass( IntWritable.class);

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

将类更改为静态后,这就是新的错误。

我觉得这是最麻烦的部分。

某些错误的屏幕快照

public static class FriendsMap extends Mapper < LongWritable, Text, Text, IntWritable >
    {
        //@SuppressWarnings("null")
        @Override 
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException 
        { 

            String friendsOfA[];    //This will be all of the friends of the user in this row
            friendsOfA = new String[] {};
            String friendsAB        = "1";      //This will be used to create pairs of users
            String friendsBA        = "2";      //This will be used to create pairs of users
            Text pairA;
            Text pairB; 
            IntWritable one     = new IntWritable(1);           //1 if they are not an existing pair here
            IntWritable oneLess = new IntWritable(-999999999);  // if they are an existing pair

            String oneRow[] = value.toString().split("\t,");    //Break the row up into users IDs
            Text userA = new Text(oneRow[0]);                                   //This is the main user for this row
            for (int i=1; i < oneRow.length; i++)   //Create an array of the rest of the users in this row
            {
                friendsOfA[i-1] = oneRow[i];
            }
            for (int i=0; i < oneRow.length; i++)   //Output the main user in pairs with all friends plus a large negative #
            {                                       //We do not want to recommend them as friends because they are friends 
                Text FOA = new Text (friendsOfA[i]);
                friendsAB = (userA + " " + FOA);
                Text pair = new Text (friendsAB);
                context.write(pair, oneLess);
                System.out.println(pair + " " + oneLess);
            }
            for (int i = 0; i < friendsOfA.length; i++)     //Output each friend pair plus the number 1
            {                                               //We want to recommend them as potential friends
                for (int j = i + 1; j < friendsOfA.length; j++) 
                {
                    Text FOA = new Text (friendsOfA[i]);
                    Text FOB = new Text (friendsOfA[j]);
                    friendsAB = (FOA + " " + FOB);
                    friendsBA = (FOB + " " + FOA);
                    pairA = new Text (friendsAB);
                    pairB = new Text (friendsBA);
                    context.write(pairA, one);
                    context.write(pairB, one);
                    System.out.println(pairA + " " + one);
                    System.out.println(pairB + " " + one);
                }
            }
        }
    }

共有1个答案

易星纬
2023-03-14

您已经将类作为内部类进行了处理,这可能会引起一些问题。内部类只能存在于封闭类的实例中。

将它们更改为静态类可能更容易。

public class Friends {

    public static class FriendsMap extends Mapper <...> {}

    public static class FriendReducer extends Reducer <...> {}

    public static void main( String[] args) throws Exception { 
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "Friends");
        job.setJarByClass(Friends.class);
        FileInputFormat.addInputPath(job, new Path("input")); 
        FileOutputFormat.setOutputPath(job, new Path("output")); 
        job.setMapperClass(FriendsMap.class); 
        job.setCombinerClass(FriendReducer.class); 
        job.setReducerClass(FriendReducer.class);
        job.setOutputKeyClass(Text.class); 
        job.setOutputValueClass(IntWritable.class);

        System.exit( job.waitForCompletion( true) ? 0 : 1); 
    }
}
 类似资料:
  • 问题内容: 当我填写标题字段时,看到上述 可怕的警告 时,我并不感到惊讶。 我阅读了几乎所有谈论的话题,或者不确定我是否找到了我想做的正确解决方案。 对不起,我不擅长英语或SQL。 在不擅长两种语言的情况下,如何找到正确的答案? 我决定要问。我不会为s或任何s失望。 当我想要答案时,我将尽可能真诚地写下来,以帮助您解决其他任何类似的问题。 我有一张与朋友关系的桌子。 两者也是表中的外键。 我问并从

  • 问题内容: 我正在研究“您可能认识的人”功能。我有两个表: 用户 id email name etc 朋友 user_id friend_id 对于每一次友谊,我都会做两个记录。假设用户7和9成为朋友…我要在友谊表中记录一条user_id = 7,friend_id = 9,另一条user_id = 9,friend_id = 7的记录。 如何根据朋友中的朋友进行sql查询,以建议我可能认识的人?

  • 我想知道为什么像这样的简单循环无法达到我的CPU时钟速度(4,2Ghz): 直观地说,我希望在不到1ms(如0238ms)的时间内实现这一点,每秒进行42亿次迭代。但我得到了大约3ms,即每秒大约3.33亿次迭代。 我假设做数学是两个周期,一个用于乘法,另一个用于求和。所以假设我在做6.66亿操作…看起来仍然很慢。然后我假设循环比较需要一个周期,循环计数器需要另一个周期… 所以我创建了以下代码来删

  • 问题内容: 我学过: ‘Application Context’的三种常用实现是- −此容器从XML文件加载bean的定义。在这里,您需要向构造函数提供XML bean配置文件的完整路径。 −此容器从XML文件加载bean的定义。在这里,您无需提供XML文件的完整路径,但需要正确设置CLASSPATH,因为此容器将在CLASSPATH中查找bean配置XML文件。 −此容器从Web应用程序中加载带

  • -这个容器从XML文件加载bean的定义。这里不需要提供XML文件的完整路径,但需要正确设置类路径,因为这个容器将在类路径中看到bean配置XML文件。 -这个容器从web应用程序中加载带有所有bean定义的XML文件。 Spring靴怎么样?我读过一些文章,如何获得ApplicationContext:

  • 我正试图用Java编写一个非常简单的merkle树实现。 我使用比特币区块链上方框170中的TXID值作为参考,因此我可以看到正确的结果。 与该块对应的TXID如下所示: 据我了解,比特币的merkle树实现方式如下: 将块中的事务拆分为成对的事务 有一个警告是: 我的代码在一个开关语句中,它看起来像这样: 我编写的swapEndianness方法不是真正的“字节级”交换,而是更改字符串的顺序,如