大数据全系列 教程
1869个小节阅读:464.8k
目录
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
package com.itbaizhan;
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 java.io.IOException;
public class FOFMapper1 extends Mapper<LongWritable, Text,Text, IntWritable> {
//定义输出的key的对象
private Text outKey = new Text();
//定义输出value对象
private IntWritable outValue0 = new IntWritable(0);
private IntWritable outValue1 = new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
//tom hello hadoop cat
//按照空格进行拆分
String[] names = value.toString().trim().split(" ");
//先处理直接关系 tom 和后续的名称直接好
for(int i = 1;i<names.length;i++){
//names[0] 和 names[i] 包正无论是A和B还是B和A,都拼接为A:B
outKey.set(getFofName(names[0],names[i]));
//0 表示直接好友 1表示间接好友
context.write(outKey,outValue0);
//再处理间接好友 hello->hadoop cat hadoop->cat
for(int j = i+1;j<names.length;j++){
outKey.set(getFofName(names[i],names[j]));
context.write(outKey,outValue1);
}
}
}
private String getFofName(String name1, String name2) {
if(name1.compareTo(name2)<0){
return name1+":"+name2;
}else{
return name2+":"+name1;
}
}
}