MapReduce实现共同朋友问题

答案:
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实现共同朋友问题的更多相关文章
- 【hadoop2.2(yarn)】基于yarn成功执行分布式map-reduce,记录问题解决过程。
hadoop2.x改进了hadoop1.x的架构, 具体yarn如何工作以及改进了什么可以在网上学, 这里仅记录我个人搭建的问题和理解,希望能帮助遇到困难的朋友. 在开始前,必须了解yarn版本的ma ...
- MapReduce实现二度好友关系
一.问题定义 我在网上找了些,关于二度人脉算法的实现,大部分无非是通过广度搜索算法来查找,犹豫深度已经明确了2以内:这个算法其实很简单,第一步找到你关注的人:第二步找到这些人关注的人,最后找出第二步结 ...
- SQL Server优化技巧之SQL Server中的"MapReduce"
日常的OLTP环境中,有时会涉及到一些统计方面的SQL语句,这些语句可能消耗巨大,进而影响整体运行环境,这里我为大家介绍如何利用SQL Server中的”类MapReduce”方式,在特定的统计情形中 ...
- MapReduce:详解Shuffle过程(转)
/** * author : 冶秀刚 * mail : dennyy99@gmail.com */ Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapRedu ...
- MapReduce:详解Shuffle过程
Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle是必须要了解的.我看过很多相关的资料,但每次看完都云里雾里的绕着,很难理清大致的逻辑, ...
- [大牛翻译系列]Hadoop(5)MapReduce 排序:次排序(Secondary sort)
4.2 排序(SORT) 在MapReduce中,排序的目的有两个: MapReduce可以通过排序将Map输出的键分组.然后每组键调用一次reduce. 在某些需要排序的特定场景中,用户可以将作业( ...
- mapreduce编程模型你知道多少?
上次新霸哥给大家介绍了一些hadoop的相关知识,发现大家对hadoop有了一定的了解,但是还有很多的朋友对mapreduce很模糊,下面新霸哥将带你共同学习mapreduce编程模型. mapred ...
- 【原创】MapReduce编程系列之二元排序
普通排序实现 普通排序的实现利用了按姓名的排序,调用了默认的对key的HashPartition函数来实现数据的分组.partition操作之后写入磁盘时会对数据进行排序操作(对一个分区内的数据作排序 ...
- MapReduce:Shuffle过程的流程
Shuffle过程是MapReduce的核心,Shuffle描述着数据从map task输出到reduce task输入的这段过程. 1.map端
随机推荐
- PMP私有广告交易市场
[资源]互联网广告新知:半小时读懂PMP私有广告交易市场是什么? https://socialbeta.com/t/resource-what-is-pmp.html SocialBeta | 201 ...
- hbctf---whatiscanary学习
题目中除了能栈溢出实在找不到其他能泄露信息的地方了.而且也没法修改GOT表,始终绕不过stack_chk_fail函数.感到无从下手.只到官方给WP了,才觉得自己基础太过浅薄了. 如果我们仔细观察崩溃 ...
- 出现unmapped spring configuration files found
intell idea启动出现unmapped spring configuration files found提示. 把spring里面的内容都打勾.
- SpringBoot与Mybatis整合实例详解
介绍 从Spring Boot项目名称中的Boot可以看出来,SpringBoot的作用在于创建和启动新的基于Spring框架的项目,它的目的是帮助开发人员很容易的创建出独立运行的产品和产品级别的基于 ...
- 一个父亲的教育札记——leo鉴书58
由于年纪和工作的原因.绝大部分小说我都不看--没空,如今小说写的也太空.但对文笔有提高的文章我是非常关注的,知道韩寒不是由于<三重门>(我报纸也不怎么看).而是此前编辑感觉我文笔差. ...
- leetcode第一刷_Simplify Path
这道题的思路还是比較清晰的,用栈嘛,麻烦是麻烦在这些层次的细节上.主要有以下几个: ./和/:当前路径,遇到这样的,应该将后面的文件夹或文件入栈. ../:上一层路径.遇到这样的.应该做一次出栈操作, ...
- 001-shell基础,创建,运行
一.概述 Shell 是一个用 C 语言编写的程序.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服 ...
- spring MVC学习(二)---配置相关的东西
1.在上一节中我们提到过每一个DispatcherServlet都会有一个上下文 (WebApplictionContext),并且继承了这些上下文中的bean,其中以一些"特殊" ...
- Nginx+tomcat配置负载均衡集群
操作系统版本:Centos 6.4 Nginx版本:nginx-1.3.15.tar.gz wget http://nginx.org/download/nginx-1.5.9.tar.gz JDK版 ...
- Hadoop2.7.3 HA高可靠性集群搭建
1.背景介绍 Hadoop2.0.0之前,在一个HDFS集群中,NameNode存在单节点故障(SPOF):因为集群中只有一个NameNode,所以在使用过程中,如果该NameNode出现故障或数据丢 ...