答案:

  

package com.duking.mapreduce;

import java.io.IOException;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;
import org.apache.hadoop.util.GenericOptionsParser; public class FindFriends { /**
* map方法
* @author duking
*
*/
public static class Map extends Mapper<Object, Text, Text, Text> { /**
* 实现map方法
*/
public void map(Object key, Text value, Context context) throws IOException, InterruptedException { //将输入的每一行数据切分后存到persions中
StringTokenizer persions = new StringTokenizer(value.toString()); //定义一个Text 存放本人信息owner
Text owner = new Text(); //定义一个Set集合,存放朋友信息
Set<String> set = new TreeSet<String>(); //将这一行的本人信息存入owner中
owner.set(persions.nextToken()); //将所有的朋友信息存放到Set集合中
while(persions.hasMoreTokens()){
set.add(persions.nextToken());
} //定义一个String数组存放朋友信息
String[] friends = new String[set.size()];
//将集合转换为数组,并将集合中的数据存放到friend
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);
}
} } } /**
* Reduce方法
* @author duking
*
*/
public static class Reduce extends Reducer<Text, Text, Text, Text> { /**
* 实现Reduce方法
*/
public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException { String commonfriends = ""; for (Text val : values){
if(commonfriends == ""){
commonfriends = val.toString();
}else{
commonfriends = commonfriends + ":" +val.toString();
}
} context.write(key,new Text(commonfriends));
}
} /**
* main
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); conf.set("mapred.job.tracker", "192.168.60.129:9000"); //指定待运行参数的目录为输入输出目录
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); /* 指定工程目录下的input output为输入输出目录
String[] ioArgs = new String[] {"input", "output" };
String[] otherArgs = new GenericOptionsParser(conf, ioArgs).getRemainingArgs();
*/ if (otherArgs.length != 2) { //判断运行参数个数 System.err.println("Usage: Data Deduplication <in> <out>"); System.exit(2); } // set maprduce job name
Job job = new Job(conf, "findfriends");
job.setJarByClass(FindFriends.class); // 设置map reduce处理类
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class); // 设置输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); //设置输入输出路径
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }

结果

MapReduce实现共同朋友问题的更多相关文章

  1. 【hadoop2.2(yarn)】基于yarn成功执行分布式map-reduce,记录问题解决过程。

    hadoop2.x改进了hadoop1.x的架构, 具体yarn如何工作以及改进了什么可以在网上学, 这里仅记录我个人搭建的问题和理解,希望能帮助遇到困难的朋友. 在开始前,必须了解yarn版本的ma ...

  2. MapReduce实现二度好友关系

    一.问题定义 我在网上找了些,关于二度人脉算法的实现,大部分无非是通过广度搜索算法来查找,犹豫深度已经明确了2以内:这个算法其实很简单,第一步找到你关注的人:第二步找到这些人关注的人,最后找出第二步结 ...

  3. SQL Server优化技巧之SQL Server中的"MapReduce"

    日常的OLTP环境中,有时会涉及到一些统计方面的SQL语句,这些语句可能消耗巨大,进而影响整体运行环境,这里我为大家介绍如何利用SQL Server中的”类MapReduce”方式,在特定的统计情形中 ...

  4. MapReduce:详解Shuffle过程(转)

    /** * author : 冶秀刚 * mail     : dennyy99@gmail.com */ Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapRedu ...

  5. MapReduce:详解Shuffle过程

    Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle是必须要了解的.我看过很多相关的资料,但每次看完都云里雾里的绕着,很难理清大致的逻辑, ...

  6. [大牛翻译系列]Hadoop(5)MapReduce 排序:次排序(Secondary sort)

    4.2 排序(SORT) 在MapReduce中,排序的目的有两个: MapReduce可以通过排序将Map输出的键分组.然后每组键调用一次reduce. 在某些需要排序的特定场景中,用户可以将作业( ...

  7. mapreduce编程模型你知道多少?

    上次新霸哥给大家介绍了一些hadoop的相关知识,发现大家对hadoop有了一定的了解,但是还有很多的朋友对mapreduce很模糊,下面新霸哥将带你共同学习mapreduce编程模型. mapred ...

  8. 【原创】MapReduce编程系列之二元排序

    普通排序实现 普通排序的实现利用了按姓名的排序,调用了默认的对key的HashPartition函数来实现数据的分组.partition操作之后写入磁盘时会对数据进行排序操作(对一个分区内的数据作排序 ...

  9. MapReduce:Shuffle过程的流程

    Shuffle过程是MapReduce的核心,Shuffle描述着数据从map task输出到reduce task输入的这段过程. 1.map端

随机推荐

  1. flume jetty 进程关系 flume jetty 跨域问题 jetty 源码分析

    flume  jetty  跨域问题 13481 httpSource的端口进程号 = flume 启动后的进程号 [root@c log]# netstat -atp Active Internet ...

  2. TuShare获取K线数据

    Tushare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从数据采集.清洗加工 到 数据存储的过程,能够为金融分析人员提供快速.整洁.和多样的便于分析的数据,为他们在数据获取 ...

  3. linux下的时间管理概述

    2017/6/21 时间这一概念在生活中至关重要,而在操作系统中也同样重要,其在系统中的功能绝不仅仅是给用户提供时间这么简单,内核的许多机制都依赖于时间子系统.但凡是要在某个精确的时间执行某个事件,必 ...

  4. How to store scaling parameters for later use

    you can use sklearn's built-in tool: from sklearn.externals import joblib scaler_filename = "sc ...

  5. Delphi锁定鼠标 模拟左右键 静止一会自动隐藏鼠标

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  6. Mac使用操作

    快捷键退出程序:Command + Q 快捷键关闭窗口:Command + W(关闭程序的窗口不一定是退出程序) 单击左上角黑苹果,菜单里面有强制退出 Finder菜单的偏好设置,高级设置菜单里面可以 ...

  7. windows安装oracle client 18c 和plsql工具

    安装须知: (1)安装平台选择.linux/windows (2)软件位数选择.32/64,如果你的plsql工具是32位,那么你就安装32位客户端,如果是64位,你就安装64位客户端. 安装过程: ...

  8. HBase1.2.6 预分区后,数据不进入预定分区的一个 bug

    rowkey 如下: 19000015115042900001511504390000151150449000015115045900001511504690000151150479000015115 ...

  9. SparkSQL程序设计

    1.创建Spark Session val spark = SparkSession.builder . master("local") .appName("spark ...

  10. windows下mysql安装失败的一个解决案例

    操作系统:windows8.1,之前安装过mysql,这次安装在配置的最后一部执行“Apply security settings”的过程中弹出经典错误: Access denied for user ...