【尚学堂·Hadoop学习】MapReduce案例2--好友推荐
案例描述
根据好友列表,推荐好友的好友

数据集
tom hello hadoop cat world hadoop hello hive cat tom hive mr hive hello hive cat hadoop world hello mr hadoop tom hive world hello tom world hive mr
代码
MyFOF.class
package com.hadoop.mr.fof;
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.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class MyFOF {
public static void main(String[] args) {
try {
//Conf
Configuration conf = new Configuration(true);
Job job = Job.getInstance(conf);
job.setJarByClass(MyFOF.class);
//Map
job.setMapperClass(FMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//Reduce
job.setReducerClass(FReducer.class);
//Input&Output
Path in = new Path("/user/hadoop/input/friends.txt");
FileInputFormat.addInputPath(job, in);
Path out = new Path("/user/hadoop/output/friends/");
if(out.getFileSystem(conf).exists(out)){
out.getFileSystem(conf).delete(out,true);
}
FileOutputFormat.setOutputPath(job, out);
//Submit
job.waitForCompletion(true);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
FMapper.class
package com.hadoop.mr.fof;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.util.StringUtils;
public class FMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
Text mkey = new Text();
IntWritable mval = new IntWritable();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//tom hello hadoop cat
String [] strs = StringUtils.split(value.toString(),' ');
/*
* 找直接、间接关系
* value: 0-直接关系;1-间接关系
* 直接关系:tom:hello tom:hadoop tom:cat
* 间接关系:hello:hadoop hello:cat hadoop:cat
*/
for(int i=1;i<strs.length;i++){
//与好友清单中的好友为直接关系
mkey.set(getFof(strs[0], strs[i]));
mval.set(0);
context.write(mkey, mval);
//在好友列表内 好友之间为间接关系
for(int j = i+1;j < strs.length;j++){
mkey.set(getFof(strs[i],strs[j]));
mval.set(1);
context.write(mkey, mval);
}
}
}
//按字典序进行字符串拼接
public static String getFof(String s1,String s2){
if(s1.compareTo(s2) < 0){
return s1+":"+s2;
}
return s2+":"+s1;
}
}
FReducer.class
package com.hadoop.mr.fof;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class FReducer extends Reducer<Text,IntWritable, Text, IntWritable> {
IntWritable rval = new IntWritable();
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
/*
* 数据如下:
* hello:hadoop 0
* hello:hadoop 1
*/
int flg = 0; //标志
int sum = 0; //共同好友总数
for(IntWritable v :values){
if(v.get() == 0){
flg = 1;
}
sum += v.get();
}
if(flg == 0){
rval.set(sum);
context.write(key, rval);
}
}
}
运行结果
本次案例只是处理了两个人之间共同好友的数量。

【尚学堂·Hadoop学习】MapReduce案例2--好友推荐的更多相关文章
- 【尚学堂·Hadoop学习】MapReduce案例1--天气
案例描述 找出每个月气温最高的2天 数据集 -- :: 34c -- :: 38c -- :: 36c -- :: 32c -- :: 37c -- :: 23c -- :: 41c -- :: 27 ...
- 尚学堂xml学习笔记
1.打开eclipse,文件-新建java project,输入文件的名字,比如输入20181112. 2.对着src右键,选择new-file,输入文件名字,比如:book.xml. 3.开始写.x ...
- 大数据学习——mapreduce案例join算法
需求: 用mapreduce实现select order.orderid,order.pdtid,pdts.pdt_name,oder.amount from orderjoin pdtson ord ...
- 尚学堂 hadoop
mr spark storm 都是分布式计算框架,他们之间不是谁替换谁的问题,是谁适合做什么的问题. mr特点,移动计算,而不移动数据. 把我们的计算程序下发到不同的机器上面运行,但是不移动数据. 每 ...
- 尚学堂JAVA基础学习笔记
目录 尚学堂JAVA基础学习笔记 写在前面 第1章 JAVA入门 第2章 数据类型和运算符 第3章 控制语句 第4章 Java面向对象基础 1. 面向对象基础 2. 面向对象的内存分析 3. 构造方法 ...
- Hadoop学习之旅三:MapReduce
MapReduce编程模型 在Google的一篇重要的论文MapReduce: Simplified Data Processing on Large Clusters中提到,Google公司有大量的 ...
- hadoop 学习笔记:mapreduce框架详解
开始聊mapreduce,mapreduce是hadoop的计算框架,我学hadoop是从hive开始入手,再到hdfs,当我学习hdfs时候,就感觉到hdfs和mapreduce关系的紧密.这个可能 ...
- Hadoop学习笔记:MapReduce框架详解
开始聊mapreduce,mapreduce是hadoop的计算框架,我学hadoop是从hive开始入手,再到hdfs,当我学习hdfs时候,就感觉到hdfs和mapreduce关系的紧密.这个可能 ...
- 学习java的视频资源(尚学堂)(比较老旧,但是还是挺好用)
本人新手,转入IT,一开始在学校的时候看过尚学堂 马士兵讲过的java基础视频教程,这次深入学习呢,就从百度云盘找了一整套的视频资源.之后越深入的学习呢,发现这些视频资源VeryCD上都发布了,地址 ...
随机推荐
- Mysql 字符串指定位置插入空格
UPDATE flow_data_243 SET data_15=CONCAT(LEFT(data_15,10),' ',RIGHT(data_15,LENGTH(data_15)-10)) WHER ...
- MySQL Connector/C++ 8.0 源码编译
平台 ubuntu 16.04 参考文档: https://dev.mysql.com/doc/dev/connector-cpp/8.0/building.html 下载源码 访问 https:// ...
- linux 基本命令2
linux没有磁盘的概念,这一点不同于windows,Linux所有的文件系统采用树的结构完成(核心本质)树自然有根节点 也就是linux存在一个根目录,用/表示ls 表示查看命令 我们使用 ls / ...
- Linux下ansible的group模块
一.概述 group 模块可以帮助我们管理远程主机上的组. 二.常用参数 name参数:必须参数,用于指定要操作的组名称. state参数:用于指定组的状态,两个值可选,present,absent, ...
- 【翻译】asp.net core2.0中的token认证
原文地址:https://developer.okta.com/blog/2018/03/23/token-authentication-aspnetcore-complete-guide token ...
- sql 语句中as的用法和作用
我们的Sql语句在很多数据库中都是通用的,比如像Mysql数据库 Access数据库. Oracle数据库. Sqlite数据库 .甚至在我们的Excel中也可以使用Sql语句. 在我的数据库中有u ...
- 内存溢出OOM
如何避免OOM 异常? 想要避免OOM 异常首先我们要知道什么情况下会导致OOM 异常. 1.图片过大导致OOM Android 中用bitmap 时很容易内存溢出,比如报如下错误:Java.lang ...
- wx.request 使用数据
小程序中,怎么使用wx.request返回的数据??? 在你的js页面中 主要是这句话 var that=this; 为什么呢?因为使用过jquery的ajax的朋友都知道.在ajax函数中的this ...
- 机器学习---感知机(Machine Learning Perceptron)
感知机(perceptron)是一种线性分类模型,通常用于二分类问题.感知机由Rosenblatt在1957年提出,是神经网络和支持向量机的基础.通过修改损失函数,它可以发展成支持向量机:通过多层堆叠 ...
- python学习日记(异常)
异常和错误 错误 1.语法错误 这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正 #语法错误示范一 if #语法错误示范二 def test: pass #语法错误示范三 pr ...