本文调试环境: 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. POJ1068——Parencodings

    Parencodings Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encode ...

  2. ConfigurationManager配置操作

    /// <summary> /// 配置信息维护 /// </summary> public class AppConfig { public static Configura ...

  3. 几种必知的oracle结构图

    一.数据库结构 二.Oracle 内存结构 三.进程结构 1. 用户进程:在数据库用户请求连接到Oracle 服务器时启动 2. 服务器进程:可以连接到Oracle实例,它在用户建立会话时启动 3. ...

  4. SequenceSum

    SequenceSum Sum of 'n' Numbers sum_of_n (or SequenceSum.sumOfN in Java, SequenceSum.SumOfN in C#) ta ...

  5. linux 查看系统信息命令

    linux 查看系统信息命令是linux初学者必备的基础知识, 这些命令也非常有用, 因为进入linux第一件事就可能是首先查看系统信息, 因此必要的系统的学习一下这些linux系统信息命令还是非常有 ...

  6. 函数flst_init

    /** The null file address */UNIV_INTERN fil_addr_t fil_addr_null = {FIL_NULL, 0}; /***************** ...

  7. 设计模式 - interpreter

    interpreter模式在GOF中的定义是:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 定义看起来是非常晦涩.简单来讲,解释器模式通常用于处理特 ...

  8. [转]jBoss事务控制

    转自:http://blog.csdn.net/trendgrucee/article/details/8545512   一.基础知识 1.JTA,即Java Transaction API,译为J ...

  9. [NOI2005] 维修数列

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 8397  Solved: 2530 Description In ...

  10. 学习面试题Day08

    1.TCP/IP协议的理解.     TCP/IP定义了电子设备(如计算机)连入因特网的标准,以及数据如何在它们之间传输的标准.它既是互联网中的基本通信语言或协议,也是局域网的通信协议.     TC ...