用MapReduce找共同朋友编程实现(Hadoop)

数据格式如下:

第一个字母代表本人,其他是他的朋友,找出共同朋友的人,和共同朋友是谁?

package FindFriend;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
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 FindFriend {
    final static String INPUT_PATH = "hdfs://master:8020/liguodong/test1";
    final static String OUTPUT_PATH = "hdfs://master:8020/liguodong/test1out";

    public static void main(String[] args) throws IOException,
    URISyntaxException, ClassNotFoundException, InterruptedException {

        Configuration conf = new Configuration();   

        final FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH),conf);
        if(fileSystem.exists(new Path(OUTPUT_PATH)))
        {
            fileSystem.delete(new Path(OUTPUT_PATH),true);
        }

        Job job = Job.getInstance(conf,"Find friend");
        job.setJarByClass(FindFriend.class);

        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReducer.class);

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

        FileInputFormat.addInputPath(job, new Path(INPUT_PATH));
        FileOutputFormat.setOutputPath(job,new Path(OUTPUT_PATH));

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

    }

    static class MyMapper extends Mapper<LongWritable,Text,Text,Text>{

        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            //分割字符串
            StringTokenizer stringTokenizer = new StringTokenizer(value.toString());
            Text owner = new Text();//存放自己

            Set<String> set = new TreeSet<String>();//存放朋友

            owner.set(stringTokenizer.nextToken());
            while(stringTokenizer.hasMoreTokens()){
                set.add(stringTokenizer.nextToken());
            }

            String[] friends = new String[set.size()];//朋友
            friends = set.toArray(friends);

            for(int i=0; i<friends.length;i++){
                for(int j=i+1; j<friends.length; j++){
                    String outputkey = friends[i]+friends[j];//朋友之间两两组合
                    context.write(new Text(outputkey), owner);//<朋友组合,自己>
                }
            }
        }

    }

    static class MyReducer extends Reducer<Text, Text ,Text, Text>
    {

        @Override
        protected void reduce(Text key, Iterable<Text> values,
                Context context)
                throws IOException, InterruptedException {
            //以朋友组合作为key值,自己作为value值。
            String commonFriends = "";
            for(Text val:values)
            {
                if(commonFriends == ""){
                    commonFriends = val.toString();
                }else{
                    commonFriends = commonFriends+"--"+val.toString();
                }
            }
            context.write(key, new Text(commonFriends));
        }
    }
}

运行结果:

时间: 2024-10-11 11:51:46

用MapReduce找共同朋友编程实现(Hadoop)的相关文章

MapReduce 单词统计案例编程

MapReduce 单词统计案例编程 一.在Linux环境安装Eclipse软件 1.   解压tar包 下载安装包eclipse-jee-kepler-SR1-linux-gtk-x86_64.tar.gz到/opt/software目录下. 解压到/opt/tools目录下: [[email protected] tools]$ tar -zxf /opt/sofeware/eclipse-jee-kepler-SR1-linux-gtk-x86_64.tar.gz -C /opt/tool

hdu-1286 找新朋友(欧拉函数,水题)

题目链接: 找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10120    Accepted Submission(s): 5344 Problem Description 新年快到了,“猪头帮协会”准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有

找新朋友(欧拉函数)

找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8976    Accepted Submission(s): 4736传送门 Problem Description 新年快到了,“猪头帮协会”准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有大于1的

Apache Hadoop YARN: Moving beyond MapReduce and Batch Processing with Apache Hadoop 2

Apache Hadoop YARN: Moving beyond MapReduce and Batch Processing with Apache Hadoop 2 .mobi: http://www.t00y.com/file/79497801 Apache Hadoop YARN: Moving beyond MapReduce and Batch Processing with Apache Hadoop 2.pdf: http://www.t00y.com/file/8034244

HDU1286 找新朋友

这个问题简单暴力的话会TLE,在这里我使用了筛法. 1 #include <cstdio> 2 #include <cstring> 3 #define MAXN 36000 4 int m[MAXN]; 5 bool f[MAXN]; 6 int solve(int n){ 7 if(m[n]) return m[n]; 8 int i,j,ans=1; 9 memset(f,true,sizeof(f)); 10 for(i=2;i<n;++i){ 11 if(f[i])

hduoj 1286 找新朋友

http://acm.hdu.edu.cn/showproblem.php?pid=1286 找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8694 Accepted Submission(s): 4592 Problem Description 新年快到了,“猪头帮协会”准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其

hdoj 1286 找新朋友 【数论之欧拉函数】

找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7912    Accepted Submission(s): 4157 Problem Description 新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有大

hdu_1286找新朋友(欧拉定理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1286 找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10969    Accepted Submission(s): 5818 Problem Description 新年快到了,“猪头帮协会”准备搞一个聚会,已经知道现有会员N人

HDU 1286 找新朋友(欧拉函数模板)

HDU 1286 找新朋友 题意:中文题. 思路:欧拉函数的纯模板题,没什么好说的,主要是理解欧拉函数的意义. 在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等. 例如φ(8)=4,因为1,3,5,7均和8互质.   ----by度娘. #include <stdio.h> int eular(int n){ int ret = 1; for(int i = 2; i*