答案:

  

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. Genymotion 模拟器的sd卡的位置

    今天用genymotion测试一个例子,发现要用sdcard,虽然可以再DDMS的 File Explore 下看到 sdcard目录,也可以看到/mnt/sdcard 目录,但是往他那里传文件,建目 ...

  2. 全面介绍Windows内存管理机制及C++内存分配实例(四):内存映射文件

    本文背景: 在编程中,很多Windows或C++的内存函数不知道有什么区别,更别谈有效使用:根本的原因是,没有清楚的理解操作系统的内存管理机制,本文企图通过简单的总结描述,结合实例来阐明这个机制. 本 ...

  3. bat批处理异备文件、压缩文件

    1.压缩本地文件,并把压缩后的文件复制到其他机器 net use Z: \\192.168.135.1\share_linux a123456! /user:chaoqun.guo set bath= ...

  4. 测试:safenet提供的CheckKey函数 内存泄漏。具体来说是句柄.

    unsigned char vendor_code[] = "7XSQT4jxlSkDJhwqpxxfLwbuxgrYw93OMy+K5sc5pyfTa7HQo1ikLyg7FDuEpgUK ...

  5. Android中三种超实用的滑屏方式汇总(转载)

    Android中三种超实用的滑屏方式汇总   现如今主流的Android应用中,都少不了左右滑动滚屏这项功能,(貌似现在好多人使用智能机都习惯性的有事没事的左右滑屏,也不知道在干什么...嘿嘿),由于 ...

  6. 网页采集利器 phpQuery

    网页采集利器 phpQuery 2012-02-28 11:43:24|  分类: php|举报|字号 订阅     在网页采集的时候,通常都会用到正则表达式.但是有时候对于正则不太好的同学,比如我, ...

  7. Extjs3.4 grid中添加一列复选框

    var sm = new Ext.grid.CheckboxSelectionModel(); var cm = new Ext.grid.ColumnModel( [ sm, new Ext.gri ...

  8. __all__方法的作用

    在__all__里面写了谁,到时候就只能用谁,其他的用不了,from 模块 import *时就只能用__all__里的 __all__=['test1','Test'] def test1(): p ...

  9. java模拟http/https post请求

    1.Post请求失败的代码 try { HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = respon ...

  10. vue2+koa2+mongodb分页

    后端 const Koa = require('koa2'); const Router = require('koa-router'); const Monk = require('monk');/ ...