博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【大数据系列】MapReduce示例好友推荐
阅读量:6695 次
发布时间:2019-06-25

本文共 3406 字,大约阅读时间需要 11 分钟。

package org.slp;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;import java.util.StringTokenizer;/** * Created by sanglp on 2017/7/17. */public class Test2Mapper extends Mapper
{ @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { //super.map(key, value, context); String line = value.toString();//一行数据代表一组好友关系 String[] ss = line.split("\t"); context.write(new Text(ss[0]),new Text(ss[1]));//主从分成两行输出 context.write(new Text(ss[1]),new Text(ss[0])); }}
package org.slp;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;import java.util.HashSet;import java.util.Iterator;import java.util.Set;/** * Created by sanglp on 2017/7/17. */public class Test2Reduce extends Reducer
{ @Override protected void reduce(Text key, Iterable
values, Context context) throws IOException, InterruptedException { //super.reduce(key, values, context); Set
set = new HashSet
(); for(Text t :values ){ set.add(t.toString()); } if (set.size()>1){ for(Iterator j = set.iterator();j.hasNext();){ String name = (String)j.next(); for(Iterator k = set.iterator();k.hasNext();){ String other = (String)k.next(); if(!name.equals(other)){ context.write(new Text(name),new Text(other)); } } } } }}
package org.slp;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;import java.io.IOException;/** * Created by sanglp on 2017/7/17. */public class JobRun2 {    public static void main(String[] args){        Configuration conf = new Configuration();        conf.set("mapred.job.tracker","node1:9001");        conf.set("mapred.job.tracker","node1:9001");        conf.set("mapred.jar","C:\\Users\\sanglp\\qq.jar");        try {            Job job = new Job(conf);            job.setJobName("qq");            job.setJarByClass(JobRun2.class);            job.setMapperClass(Test2Mapper.class);            job.setReducerClass(Test2Reduce.class);            job.setMapOutputKeyClass(Text.class);            job.setMapOutputValueClass(Text.class);            job.setNumReduceTasks(1);//设置reduce任务的个数            //mapreduce输入数据所在目录或文件            FileInputFormat.addInputPath(job,new Path("/usr/input/qq"));            //mr执行之后的输出数据的目录            FileOutputFormat.setOutputPath(job,new Path("/usr/out/qq"));            try {                System.exit(job.waitForCompletion(true)?0:1);            } catch (InterruptedException e) {                e.printStackTrace();            } catch (ClassNotFoundException e) {                e.printStackTrace();            }        } catch (IOException e) {            e.printStackTrace();        }    }}

文件内容例如:

小明  小李

小花  小白

转载于:https://www.cnblogs.com/dream-to-pku/p/7203448.html

你可能感兴趣的文章
取消Exchange数据库的自动分配功能
查看>>
Screen OS防火墙DoS攻击的检测和防御
查看>>
Dell-R410 配置raid1
查看>>
Go语言之单元测试
查看>>
Xargs用法详解 (Linux 命令使用)
查看>>
会议室邮箱的日历中显示会议主题和显示预约人
查看>>
tomcat中catalina是什么
查看>>
地球人常识手册2
查看>>
Redhat安装系统后开机一直停在进度条处
查看>>
18. Gradle编译其他应用代码流程(六) - 执行Task过程
查看>>
css优先级
查看>>
Dell Compellent的一些缺陷
查看>>
1. Android源代码下载编译
查看>>
2.命令行监控
查看>>
45.du命令
查看>>
resin学习随笔
查看>>
解决bug的技巧
查看>>
iptables详解
查看>>
Windows 中常用快捷键
查看>>
前端项目涉及(库)
查看>>