【尚学堂·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上都发布了,地址 ...
随机推荐
- QTableWidget class
Help on class QTableWidget in module PyQt5.QtWidgets: class QTableWidget(QTableView) | QTableWidge ...
- iOS CATransition 自定义转场动画
https://www.jianshu.com/p/39c051cfe7dd CATransition CATransition 是CAAnimation的子类(如下图所示),用于控制器和控制器之间的 ...
- jQuery 与 Ajax 的应用
Ajax 全称为 "Asynchronous JavaScript and XML"(异步 JavaScript 和 XML ),它并不是指一种单一的技术,而是有机地利用了一系列交 ...
- 使用c++如何实现在gRPC中传输文件
使用c++实现gRPC远程调用框架中传输文件,proto文件如下: syntax = "proto3"; package transferfile; service Transfe ...
- es6中常用方法
查询数组中是否包含了某个元素keyword arr.includes(keyword)
- 使用excel整理脚本
的时候需要通过excel数据初始化脚本,当数据过多的时候,脚本也就很多.这里记录一个平时用excel初始化脚本的小技巧. excel中在空单元格中写如下值: ="INSERT INTO db ...
- Hadoop系列(一):Hadoop集群搭建
环境:CentOS 7 JDK: 1.7.0_80 hadoop:2.8.5 两台机器:master(192.168.56.101) slave(192.168.56.102) 配置基础环境 1. ...
- bash中打印文件每一行及其行号
#!/bin/bash linenumber=$(cat remoteIP.cfg |wc -l) currentline= for ip in $(cat remoteIP.cfg) do curr ...
- Alice and Bob HDU - 4111 (SG函数)
Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The ...
- M-BM-
今天拷贝了一段代码 struct Test { Test( int ) {} Test() {} ...