hadoop入门教程-程序小实例
来源:互联网 发布:网络跟踪 编辑:程序博客网 时间:2024/05/21 11:23
无论是在微信还是QQ,我们经常看到好友推荐这样的功能,其实这个功能是在大数据的基础上实现的,下面来看具体的代码实现:
在src下添加三个类:JobRun.java:
package com.lftgb.mr;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 JobRun {public static void main(String[] args) {Configuration conf = new Configuration();conf.set("mapred.job.tracker", "192.168.152.128:9001");conf.set("fs.default.name", "hdfs://192.168.152.128:9000");conf.set("mapred.jar", "C:\\Users\\志鹏\\Desktop\\hadoop程序\\qq.jar");try {Job job = new Job(conf);/* * job.setJarByClass(JobRun.class); * job.setMapperClass(WcMapper.class); * job.setReducerClass(WcReducer.class); * job.setMapOutputKeyClass(Text.class); * job.setMapOutputValueClass(IntWritable.class); * * // job.setNumReduceTasks(1);//设置reduce任务的个数 默认是一个 * * // mapreduce 输入数据所在的目录或者文件 FileInputFormat.addInputPath(job, new * Path("/usr/my2016")); // mr执行之后的数据数据目录 * FileOutputFormat.setOutputPath(job, new Path("/usr/output")); */job.setJobName("qq");job.setJarByClass(JobRun.class);job.setMapperClass(Test2Mapper.class);job.setReducerClass(Test2Reduce.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);// mapreduce 输入数据所在的目录或者文件FileInputFormat.addInputPath(job, new Path("/usr/input/qq/"));// mr执行之后的数据数据目录FileOutputFormat.setOutputPath(job, new Path("/usr/output/qq"));try {System.exit(job.waitForCompletion(true) ? 0 : 1);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}} catch (IOException e) {e.printStackTrace();}}}QQMapper.java:
package com.lftgb.mr;import java.io.IOException;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;public class Test2Mapper extends Mapper<LongWritable, Text, Text, Text> {protected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {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]));}}QQReduce.java:
package com.lftgb.mr;import java.io.IOException;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;public class Test2Reduce extends Reducer<Text, Text, Text, Text> {protected void reduce(Text key, Iterable<Text> i,Context arg2) throws IOException,InterruptedException {Set<String>set= new HashSet<String>();for(Text t:i){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)){arg2.write(new Text(name),new Text(other));}}}}}}在eclipse下,与hadoop结合可以更有效的实现大数据处理的功能,深入的研究请期待小编的下次博客!!
0 0
- hadoop入门教程-程序小实例
- hadoop入门教程及其实例代码
- 微信小程序入门教程及实例
- 微信小程序入门教程及实例
- CGI小程序实例
- 创建小程序实例
- 支付宝小程序入门教程+案例demo
- hadoop小程序---写入hdfs
- Hadoop小程序测试方法
- hadoop入门教程
- hadoop之hbase实现web 小实例
- hadoop之hbase实现web 小实例
- hadoop之hbase实现web 小实例
- java小程序实例 闰年
- 小程序调用接口实例
- 小程序调用接口实例
- hadoop入门教程-hadoop是什么
- 微信小程序 实战小程序实例
- 新建maven库时,提示Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:RELEA
- Android学习之设计模式之单例模式
- 打印特定星图案
- 使用sqlalchemy执行sql语句的方法
- elgg好文章地址
- hadoop入门教程-程序小实例
- 循环语句中continue和break充当“goto”角色
- 共有25款Windows 程序调试工具开源软件
- 登录式shell的执行顺序及简单应用
- 写出一条Sql语句,取出表A中的第31条到第40条记录。表A以自动增长的ID作为主键。(注意:ID可能不是连续的)
- 几种语言的计算速度问题
- c++中调用python脚本提示 error LNK2001: 无法解析的外部符号
- Linux平台RTL8188CUS驱动移植及测试
- POJ 2029 Get Many Persimmon Trees(二维树状数组)