本文调试环境: ubuntu 10.04 , hadoop-1.0.2

hadoop装的是伪分布模式,就是只有一个节点,集namenode, datanode, jobtracker, tasktracker...于一体。

本文实现了简单的倒排索引,单词,文档路径,词频,重要的解释都会在代码注视中。

第一步,启动hadoop, 开发环境主要是用eclipse. 在本地文件系统中新建三个文本文档作为数据源:并且上传到HDFS文件系统上:

如上图,在HDFS上新建了一个输入路径文件夹:input1,此路径将会作为后面程序的输入参数;

如果你打开了eclipse,你同样会在DFSLocation中看到此目录文件信息:

好了,然后开始写代码吧,代码的详细解释,你可以在刘鹏老师的hadoop实战中第三章找到:

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.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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; public class InvertedIndex { // Map过程
public static class InvertedIndexMapper extends
Mapper<Object, Text, Text, Text>{ private Text keyInfo = new Text();
private Text valueInfo = new Text();
private FileSplit split; public void map(Object key, Text value, Context context)
throws IOException, InterruptedException{ //获得<key,value>对所属的FileSplit对象
split = (FileSplit)context.getInputSplit(); StringTokenizer itr = new StringTokenizer(value.toString()); while(itr.hasMoreTokens()){
//key值由单词和文档URL组成,如:word:filename.txt
//至于为什么采用这样的格式,因为可以利用MapRedeuce框架自带的Map端排序
keyInfo.set(itr.nextToken() + ":" + split.getPath().toString());
//初始词频为1
valueInfo.set("1");
context.write(keyInfo, valueInfo);
}
}
} //Combine 过程
public static class InvertedIndexCombiner extends
Reducer<Text, Text, Text, Text>{ private Text info= new Text(); public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException{ int sum = 0;
for(Text value : values){
sum += Integer.parseInt(value.toString());
} int splitIndex = key.toString().indexOf(":");
//现在value由如下格式组成:文档URL:词频,为保证在Shuffle过程的key 值相同,这样才能
//哈希到同一个reducer.
info.set(key.toString().substring(splitIndex + 1) + ":" + sum); //key由单词组成
key.set(key.toString().substring(0, splitIndex));
context.write(key, info);
}
} //Reducer过程
public static class InvertedIndexReducer extends
Reducer<Text, Text, Text, Text>{ private Text result = new Text(); // 输入端键值 value格式是一个列表,进入Reducer的key是经过排序的,因为相同的key则产生了一个列表{values}
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException{ String fileList = new String();
for(Text value : values){
fileList += value.toString() + ";";
}
//合并每个单词的所有values
result.set(fileList); context.write(key, result);
}
} public static void main(String[] args) throws Exception{ Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if(otherArgs.length != 2){
System.err.println("args is wrong!");
System.exit(2);
} Job job = new Job(conf, "InvertedIndex");
job.setJarByClass(InvertedIndex.class); job.setMapperClass(InvertedIndexMapper.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setCombinerClass(InvertedIndexCombiner.class); job.setReducerClass(InvertedIndexReducer.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); //程序添加的两个参数指定输入输出文件路径
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1);
} }

注意新建的不是JAVA工程哦,是M-R工程,还要注意输出目录路径在程序运行之前不应该存在,此处我指定为output1

在运行配置里,输入两个参数,即输入路径和输出路径。

运行时你将会看到如此信息:

运行结束后你将在你的HDFS文件系统上的output1 文件夹下看到程序的执行结果:

这就是正确的结果了,统计出每个单词出现的文档路径及词频数。一个简单的M-P编程是列就是这样子了的。

笔者刚开始学习hadoop,欢迎交流,如有错误,恳请指出。

[置顶] MapReduce 编程之 倒排索引的更多相关文章

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

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

  2. MapReduce编程之倒排索引

    任务要求: //输入文件格式 18661629496 110 13107702446 110 1234567 120 2345678 120 987654 110 2897839274 1866162 ...

  3. [置顶] Java编程笔试题之一 ----文件操作

    题目:给定一个文件和一个字符串,判断文件是否包含该字符串,如果包含,请打印出包含该字符串的行号以及该行的全部内容. 思路: ①使用缓冲流(BufferedReader)读取文件,定义初始行号为0.   ...

  4. Hadoop MapReduce编程学习

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

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

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

  6. [置顶] Android开发笔记(成长轨迹)

    分类: 开发学习笔记2013-06-21 09:44 26043人阅读 评论(5) 收藏 Android开发笔记 1.控制台输出:called unimplemented OpenGL ES API ...

  7. Hadoop MapReduce编程 API入门系列之挖掘气象数据版本2(十)

    下面,是版本1. Hadoop MapReduce编程 API入门系列之挖掘气象数据版本1(一) 这篇博文,包括了,实际生产开发非常重要的,单元测试和调试代码.这里不多赘述,直接送上代码. MRUni ...

  8. 批处理引擎MapReduce编程模型

    批处理引擎MapReduce编程模型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. MapReduce是一个经典的分布式批处理计算引擎,被广泛应用于搜索引擎索引构建,大规模数据处理 ...

  9. VC++实现窗口置顶

    最近在跟着Visual C++网络编程开发与实战视频教程做HttpSourceViewer这个MFC项目时,可以看我Github上的项目HttpSourceViewer,目前基本实现了所有功能,就是关 ...

随机推荐

  1. update多表陷阱

    今天同学发了个sql题目 A1表 B1表 id num id snum 1 10 1 90 2 2000 3 4000 3 30 B表的数据插入A表当中 最后的结果 A表 1 90 2 2000 3 ...

  2. VMware下ubuntu与win8共享文件时/mnt/hgfs目录为空的解决办法

    VMware下ubuntu(guest)与win8共享文件时/mnt/hgfs目录为空的解决办法 环境:VMware-player-5.0.2-1031769 + ubuntu13.04 1.安装vm ...

  3. init进程解析rc文件的相关函数分析

    init进程的源码文件位于system/core/init,其中解析rc文件语法的代码放在五个函数中, init_parse_config_file (init_parser.c), read_fil ...

  4. 【HDOJ】2388 Playground Hideout

    优先级队列直接AC. /* 2388 */ #include <iostream> #include <sstream> #include <string> #in ...

  5. sizeof(数组)

    这里就不讨论一般的数组长度计算了,只说明一下任何数据到了函数的形参中都将退化为指针,所以计算大小的时候,也是计算的指针的大小 直接上代码了 // class sizeof测试.cpp : 定义控制台应 ...

  6. bzoj2245

    这道题还是比较简单的费用流,由于w是递增的 实际上,这题数据还可以强一点,比如说分段函数不保证费用递增, 就要加一点技巧了(要保证函数的顺序) ; type node=record        ne ...

  7. ↗☻【HTML5秘籍 #BOOK#】第1章 HTML5简介

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  8. Java [leetcode 11] Container With Most Water

    问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

  9. spring+mybatis 多数据源整合

    <!-- 数据源配置 -->   <bean id="ds1" class="org.apache.commons.dbcp.BasicDataSour ...

  10. 转载-KMP算法前缀数组优雅实现

    转自:http://www.cnblogs.com/10jschen/archive/2012/08/21/2648451.html 我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见 ...