您的位置:首页 > 其它

mapreduce实现QQ好友推荐

2017-05-22 11:25 204 查看
文件

hadoop hello

hdfs world

tom cat

cat dog

hello world
hello hdfs

输出

tom dog

dog tom

hello world

world hello

hdfs world

hdfs hadoop

world hdfs

world hadoop

hadoop hdfs

hadoop world

hello hdfs

hdfs hello

public class QQMapper extends Mapper<LongWritable, Text, Text, Text> {
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {

String line = value.toString();
String[] ss = StringUtils.split(line, "\\s+");

context.write(new Text(ss[0]), new Text(ss[1]));
context.write(new Text(ss[1]), new Text(ss[0]));

}

}

public class QQReducer extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
Set<String> set = new HashSet<String>();

for (Text text : values) {
set.add(text.toString());
}

if (set.size() > 1) {
for (Iterator j = set.iterator(); j.hasNext();) {
String name =  j.next();
for (Iterator i = set.iterator(); i.hasNext();) {
String other =  i.next();
if (!name.equals(other)) {
context.write(new Text(name), new Text(other));
}
}

}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: