任务要求:

//输入文件格式

18661629496 110

13107702446 110

1234567 120

2345678 120

987654 110

2897839274 18661629496

//输出文件格式格式

11018661629496|13107702446|987654|18661629496|13107702446|987654|

1201234567|2345678|1234567|2345678|

186616294962897839274|2897839274|

mapreduce程序编写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.io.IOException;
import java.util.StringTokenizer;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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;
 
public class Test2 {
    enum Counter
    {
        LINESKIP,//记录出错的行
    }
    public static class Map extends Mapper<LongWritable, Text, Text, Text>{
 
 
        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
             String line = value.toString();//读取源数据
             try
             {
                 //数据处理
                 String [] lineSplit = line.split(" ");//18661629496,110
                 String anum = lineSplit[0];
                 String bnum = lineSplit[1];  
                 //输出格式:110,18661629496             
 
                 context.write(new Text(bnum), new Text(anum));
                  
             }
             catch(ArrayIndexOutOfBoundsException e)
             {
                 context.getCounter(Counter.LINESKIP).increment(1);//出错时计数器+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 valueString;
            String out="";
            for(Text value:values)
            {
                valueString=value.toString();
                out+=valueString+"|";
            }
            context.write(key, new Text(out));
        }
    }
 
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        if (args.length != 2) {
            System.err.println("请配置输入输出路径 ");
            System.exit(2);
        }
        //各种配置
        Job job = new Job(conf, "telephone ");//作业名称配置
        //类配置
        job.setJarByClass(Test2.class);
        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);
        //map输出格式配置
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        //作业输出格式配置
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        //添加输入输出路径
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        //任务完毕时退出
        System.exit(job.waitForCompletion(true) ? 0 1);
 
    }
 
}

将mapreduce程序打包为jar文件:

1.右键项目名称->Export->java->jar file

2.配置jar文件存储位置

3.选择main calss

4.执行jar文件

[liuqingjie@master hadoop-0.20.2]$ bin/hadoop jar /home/liuqingjie/test2.jar /user/liuqingjie/in /user/liuqingjie/out

15/05/14 01:46:47 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.

15/05/14 01:46:47 INFO input.FileInputFormat: Total input paths to process : 2

15/05/14 01:46:48 INFO mapred.JobClient: Running job: job_201505132004_0005

15/05/14 01:46:49 INFO mapred.JobClient:  map 0% reduce 0%

15/05/14 01:46:57 INFO mapred.JobClient:  map 100% reduce 0%

15/05/14 01:47:09 INFO mapred.JobClient:  map 100% reduce 100%

……………………………………………………………………………………

查看结果

[liuqingjie@master hadoop-0.20.2]$ bin/hadoop dfs -cat ./out/*

cat: Source must be a file.

110 18661629496|13107702446|987654|18661629496|13107702446|987654|

120 1234567|2345678|1234567|2345678|

18661629496 2897839274|2897839274|

MapReduce编程之倒排索引的更多相关文章

  1. [置顶] MapReduce 编程之 倒排索引

    本文调试环境: ubuntu 10.04 , hadoop-1.0.2 hadoop装的是伪分布模式,就是只有一个节点,集namenode, datanode, jobtracker, tasktra ...

  2. MapReduce编程(七) 倒排索引构建

    一.倒排索引简单介绍 倒排索引(英语:Inverted index),也常被称为反向索引.置入档案或反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射. ...

  3. Hadoop MapReduce编程学习

    一直在搞spark,也没时间弄hadoop,不过Hadoop基本的编程我觉得我还是要会吧,看到一篇不错的文章,不过应该应用于hadoop2.0以前,因为代码中有  conf.set("map ...

  4. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  5. 三、MapReduce编程实例

    前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...

  6. Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)

    不多说,直接上代码. Hadoop MapReduce编程 API入门系列之小文件合并(二十九) 生成的结果,作为输入源. 代码 package zhouls.bigdata.myMapReduce. ...

  7. [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想

    Ubuntu系统 (我用到版本号是140.4) ubuntu系统是一个以桌面应用为主的Linux操作系统,Ubuntu基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一 ...

  8. mapreduce编程模型你知道多少?

    上次新霸哥给大家介绍了一些hadoop的相关知识,发现大家对hadoop有了一定的了解,但是还有很多的朋友对mapreduce很模糊,下面新霸哥将带你共同学习mapreduce编程模型. mapred ...

  9. 《Data-Intensive Text Processing with mapReduce》读书笔记之二:mapreduce编程、框架及运行

    搜狐视频的屌丝男士第二季大结局了,惊现波多野老师,怀揣着无比鸡冻的心情啊,可惜随着剧情的推进发展,并没有出现期待中的屌丝奇遇,大鹏还是没敢冲破尺度的界线.想百度些种子吧,又不想让电脑留下污点证据,要知 ...

随机推荐

  1. Python3爬虫--两种方法(requests(urllib)和BeautifulSoup)爬取网站pdf

    1.任务简介 本次任务是爬取IJCAI(国际人工智能联合会议)最新2018年的pdf论文文件. 本次编码用到了正则表达式从html里面提取信息,如下对正则表达式匹配规则作简要的介绍. 2.正则表达式规 ...

  2. 负载均衡获得真实源IP的6种方法

    除了X-FORWARD-FOR,负载均衡中获得真实源IP的方法还有很多种. 本文抛砖引玉,主要介绍获得真实源IP的多种方法,而不是具体配置. 负载均衡获得真实IP的方法有很多种,将形成专题文章. 本文 ...

  3. Dapper中数据表的字段(列)与实体属性不一致时,如何手动配置它们之间的映射?

    NET[C#]Dapper中数据表的字段(列)与实体属性不一致时,如何手动配置它们之间的映射? 问题描述 比如有如下的数据表结构:Person: person_id int first_name va ...

  4. logrotate配置文件

    一.logrotate配置文件 /etc/logrotate.conf

  5. python ansible api

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @File : test2.py # @Author: Anthony.waa # @Date : 20 ...

  6. Unity3d Vector3

    using UnityEngine; using System.Collections; public class test : MonoBehaviour { void Start () { Vec ...

  7. 第4章 部署模式 Deployment Plan(部署规划)

    已开发了基于组件的应用程序,该应用程序在逻辑上构造为多层结构,如 Three-Layered Services Application. 中所述.您希望将它分布到一组在物理上为多级结构的服务器上,如 ...

  8. html5左右滑动页面效果实现

    The Demo of h5 slider achiev by Myself 主要思路: 设置一个容器container,然后里面有几个page,获取到屏幕的宽度并将其赋值给page,然后contai ...

  9. 剖析Promise内部结构,一步一步实现一个完整的、能通过所有Test case的Promise类

    本文写给有一定Promise使用经验的人,如果你还没有使用过Promise,这篇文章可能不适合你,建议先了解Promise的使用 Promise标准解读 1.只有一个then方法,没有catch,ra ...

  10. 4) 十分钟学会android--建立第一个APP,启动另一个Activity

    在完成上一课(建立简单的用户界面)后,我们已经拥有了显示一个activity(一个界面)的app(应用),该activity包含了一个文本字段和一个按钮.在这节课中,我们将添加一些新的代码到MyAct ...