MapReduce编程实例之倒排索引 1
来源:互联网 发布:淘宝steam游戏安全吗 编辑:程序博客网 时间:2024/05/16 06:13
任务描述:
有一批电话清单,记录了用户A拨打给用户B的记录
做一个倒排索引,记录拨打给用户B所有的用户A、
18940084808 10086
13342445911 10001
13614004876 120
18940084808 1008611
13342445911 110
15847985621 10000
有一批电话清单,记录了用户A拨打给用户B的记录
做一个倒排索引,记录拨打给用户B所有的用户A、
example data:
13614004876 11018940084808 10086
13342445911 10001
13614004876 120
18940084808 1008611
13342445911 110
15847985621 10000
code:
<span style="font-size:14px;">package mrTest;import java.io.IOException;import java.util.Date;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import com.ibm.icu.text.SimpleDateFormat;public class daopaisuoyin {enum Counter{ LINESKIP, //出错的行 } public static class Map extends Mapper<Object, Text, Text, Text>{public void map(Object key,Text value,Context context){String line = value.toString();try{String[] lineSplit = line.split(" ");String newKey = lineSplit[0];String newValue = lineSplit[1];context.write(new Text(newKey), new Text(newValue));}catch(Exception e){ context.getCounter(Counter.LINESKIP).increment(1); return;}}}public static class Reduce extends Reducer<Text, Text, Text, Text>{public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException{String result = "";for (Text value : values) {result += value.toString() + " # ";}context.write(key, new Text(result));}}public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {// TODO Auto-generated method stubJob job = new Job(new Configuration(), " 倒排索引 ");job.setJarByClass(daopaisuoyin.class);job.setNumReduceTasks(1);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);job.setMapperClass(Map.class);job.setReducerClass(Reduce.class);FileInputFormat.addInputPath(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));//记录时间SimpleDateFormat sdf = new SimpleDateFormat(); Date start = new Date(); //开始时间 int result = job.waitForCompletion(true)? 0 : 1; //任务开始Date end = new Date(); //结束时间float time = (float)((end.getTime() - start.getTime()) / 60000.0); //任务开始到结束经历的时间System.out.println("Job 开始的时间为:" + start);System.out.println("Job 结束的时间为:" + end);System.out.println("Job 经历的时间为:" + time + "分钟");System.out.println("Job 的名字:" + job.getJobName());System.out.println("Job 是否成功:" + job.isSuccessful() );System.out.println("Job 输入的行数:" + job.getCounters().findCounter("org.apache.hadoop.mapred.Task$Counter", "MAP_INPUT_RECORDS").getValue());System.out.println("Job 输出的行数:" + job.getCounters().findCounter("org.apache.hadoop.mapred.Task$Counter", "MAP_OUTPUT_RECORDS").getValue());System.exit(result); //判断是否结束}}</span>
结果显示:
0 0
- MapReduce编程实例之倒排索引 1
- MapReduce 编程之 倒排索引
- MapReduce编程之倒排索引
- MapReduce实例----倒排索引
- MapReduce编程之倒排索引的实现
- MapReduce编程之倒排索引的实现
- MapReduce编程——倒排索引
- MapReduce编程(七) 倒排索引构建
- MapReduce--倒排索引
- mapreduce--倒排索引
- Hadoop之MapReduce-倒排索引案例
- mapreduce之倒排索引代码
- 浅谈MapReduce之倒排索引
- Mapreduce实例---倒排索引(含job串联)
- mapreduce实现倒排索引
- MapReduce实现倒排索引
- mapreduce实现倒排索引
- MapReduce倒排索引概要
- 简单的Ajax 技术
- Spring中,在继承JdbcDaoSupport时,出现The type org.springframework.dao.support.DaoSupport cannot be resolved.
- oracle is not in the sudoers file.This incident will be reported
- iOS切换window根控制器
- Android 对回调的理解
- MapReduce编程实例之倒排索引 1
- HTML5的WebSocket
- html select下拉菜单 从数据库中读取 ajax
- hdu 1710 Binary Tree Traversals
- 数组的基本操作(一)
- Spring MVC防御CSRF、XSS和SQL注入攻击
- 移动App测试实战——专项测试
- Android软键盘遮挡的四种解决方案
- SQL*Loader-522: lfiopn failed for file (xxx.log)