上一个已经实现了反向索引,那么为什么不尝试下全文搜索呢。例如有了

Hello     file3.txt:1;
MapReduce     file3.txt:2;fil1.txt:1;fil2.txt:1;
bye     file3.txt:1; 
is     fil1.txt:1;fil2.txt:2;
powerful     fil2.txt:1;
simple     fil2.txt:1;fil1.txt:1;
那么我要找MapReduce is simple,那么就有file1 和file2有。基本的思想就是先按照MapReduce is simple一个个在索引上查找,例如
MapReduce 3,1,2
is 1,2
simple 2,1
接着以file作为key,word作为value输出
1 MapReduce is simple
2 MapReduce is simple
3 MapReduce
接下来在Reduce中对各个value的单词名进行统计,如果超过3个,那就说明有匹配的了。
 
这里主要的技术是map,Reduce如何获得命令行参数。在主类中可以通过
String[] pathargs= new GenericOptionsParser(conf, args).getRemainingArgs();
来获得参数,但是如何向map和reduce传参呢,这里有三种方法,只看了一种,因为感觉够用了。
我们通过在主类中的配置实例写参数conf.set(key,value)这里的key,value都是String。要记住一点,这个语句一定要在jog.getInstance(conf)之前,否则都实例化了一个job了还怎么配置呢。接着在map或者reduce中通过
Configuration  conf=context.getConfiguration()来获得主类的配置文件。接着就可以conf.get(key)了。
代码具体如下:
public class Find {
 
                 public static void main(String[] args) throws Exception {
                                Configuration conf = new Configuration();
                                String[] pathargs= new GenericOptionsParser(conf, args).getRemainingArgs();
                                 if(pathargs.length <2){
                                                System. err.println(pathargs.length );
                                                System. exit(2);
                                }
                                
                                conf.set( "argsnum",Integer.toString(pathargs. length));
                                 for(int i=2;i<pathargs.length;i++){  
                                                conf.set( "args"+i,pathargs[i]);
                                                System. out.println(pathargs[i]);
                                }
                                
                                Job job = Job. getInstance(conf, "JobName");
                                job.setJarByClass(org.apache.hadoop.examples10.Find. class);
                                 // TODO: specify a mapper
                                job.setMapperClass(MyMapper. class);
                                 // TODO: specify a reducer
                                job.setReducerClass(MyReducer. class);
 
                                 // TODO: specify output types
                                job.setOutputKeyClass(Text. class);
                                job.setOutputValueClass(Text. class);
 
                                 // TODO: specify input and output DIRECTORIES (not files)
                                FileInputFormat. setInputPaths(job, new Path(pathargs[0]));
                                FileOutputFormat. setOutputPath(job, new Path(pathargs[1]));
                                
 
                                
                                 if (!job.waitForCompletion(true))
                                                 return;
                }
 
}
 
 
public class MyMapper extends Mapper<LongWritable, Text, Text, Text> {
                 //String[] content={"MapReduce","is","simple"};
                 public void map(LongWritable ikey, Text ivalue, Context context)
                                                 throws IOException, InterruptedException {
                                Configuration conf=context.getConfiguration();
                                 int argsnum=Integer.parseInt(conf.get( "argsnum"));
                                 //int argsnum=conf.get(" argsnum");
                                 int i=0;
                                ArrayList<String> content= new ArrayList<String>();
                                 for(i=2;i<argsnum;i++ ){
                                                 //System.out.println(conf.get("args"+i));
                                                content.add(conf.get( "args"+i));
                                                                     
                                }                              
                                String line=ivalue.toString();
                                String key=line.split( "         " )[0];
                                String value=line.split( "      " )[1];
                                StringTokenizer st= new StringTokenizer(value,";" );
                                 for(i=0;i<content.size();i++){
                                                 if(content.get(i).compareTo(key)==0){
                                                                ArrayList<String> filelist=new ArrayList<String>();
                                                                 while(st.hasMoreTokens()){
                                                                                String file=st.nextToken();
                                                                                file=file.split( ":")[0];
                                                                                filelist.add(file);
                                                                }
                                                                 for(int j=0;j<filelist.size();j++){
                                                                                context.write( new Text(filelist.get(j)),new Text(key));
                                                                }
                                                }
                                }
                }
 
}
 
 
public class MyReducer extends Reducer<Text, Text, Text, Text> {
 
                 public void reduce(Text _key, Iterable<Text> values, Context context)
                                                 throws IOException, InterruptedException {
                                Configuration conf=context.getConfiguration();
                                 int argsnum=Integer.parseInt(conf.get( "argsnum"));
                                 // process values
                                 int sum=0;
                                String filename= new String();
                                
                                 for(int i =2;i<argsnum; i++ ){
                                                 //System.out.println(conf.get("args"+i));
                                                filename+=(conf.get( "args"+i));
                                                filename+= " ";       
                                }
                                
                                 for (Text val : values) {
                                                sum++;
                                }
                                 if(sum>=argsnum-2){
                                                context.write( new Text(filename),_key);
                                }
                }
 
}

MapReduce 简单的全文搜索的更多相关文章

  1. MapReduce 简单的全文搜索2

    上一个全文搜索实现了模糊查找,这个主要实现了精确查找,就是比如你查找mapreduce is simple那么他就只查找有这个句子的文章,而不是查找有这三个单词的文章. 这个版本需要重写反向索引,因为 ...

  2. Django 博客实现简单的全文搜索

    作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 搜索是一个复杂的功能,但对于一些简单的搜索任务,我们可以使用 Django Mode ...

  3. 如何在MySQL中获得更好的全文搜索结果

    如何在MySQL中获得更好的全文搜索结果 很多互联网应用程序都提供了全文搜索功能,用户可以使用一个词或者词语片断作为查询项目来定位匹配的记录.在后台,这些程序使用在一个SELECT 查询中的LIKE语 ...

  4. window环境下,php+sphinx+coreseek实现简单的中文全文搜索

    就以我个人理解来说,sphinx其实是介于客户端和mysql之间的一个索引表,把数据库的没一条记录假设为文档,那么这个索引表其实保存的就是这条记录的关键词及其对应的文档id 1.sphinx的安装 下 ...

  5. SQLSERVER全文搜索

    SQLSERVER全文搜索 看这篇文章之前请先看一下下面我摘抄的全文搜索的MSDN资料,基本上MSDN上关于全文搜索的资料的我都copy下来了 并且非常认真地阅读和试验了一次,并且补充了一些SQL语句 ...

  6. [Elasticsearch] 全文搜索 (一) 基础概念和match查询

    全文搜索(Full Text Search) 现在我们已经讨论了搜索结构化数据的一些简单用例,是时候开始探索全文搜索了 - 如何在全文字段中搜索来找到最相关的文档. 对于全文搜索而言,最重要的两个方面 ...

  7. MySQL 全文搜索支持, mysql 5.6.4支持Innodb的全文检索和类memcache的nosql支持

    背景:搞个个人博客的全文搜索得用like啥的,现在mysql版本号已经大于5.6.4了也就支持了innodb的全文搜索了,刚查了下目前版本号都到MySQL Community Server 5.6.1 ...

  8. MySQL 全文搜索支持

    MySQL 全文搜索支持 从MySQL 4.0以上 myisam引擎就支持了full text search 全文搜索,在一般的小网站或者blog上可以使用这个特性支持搜索. 那么怎么使用了,简单看看 ...

  9. Apache Solr采用Java开发、基于Lucene的全文搜索服务器

    http://docs.spring.io/spring-data/solr/ 首先介绍一下solr: Apache Solr (读音: SOLer) 是一个开源.高性能.采用Java开发.基于Luc ...

随机推荐

  1. Flask architecture

    论文The Flask Security Architecture: System Support for Diverse Security Policies 介绍了Flask architectur ...

  2. 【转】ThinkPHP 页面跳转

    ThinkPHP 提供了success 与error 方法用于带提示信息的页面跳转,如添加数据后显示提示信息并跳转等.success 方法用于操作成功后的提示,error 用于操作失败后的提示,二者使 ...

  3. java数据结构之链表的实现

    这个链表的内部是使用双向链表来表示的,但是并未在主函数中进行使用 /** * 链表 * 2016/4/26 **/ class LinkList{ Node head = new Node(); No ...

  4. IE6下绝对定位元素和浮动元素并列绝对定位元素消失

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. FlashDevelop调试Air出错

    按下F5调试命令行窗口出现错误: Starting AIR Debug Launcher... invalid application descriptor: Unknown namespace: h ...

  6. ios 概况了解

    iOS的系统架构分为四个层次:( iOS是基于UNIX内核,android是基于Linux内核) 核心操作系统层(Core OS layer).核心服务层(Core Services layer).媒 ...

  7. 我终于有案例库啦(github 提供的)

    穷逼一个,一直在纠结要不要买个服务器什么的. 后来在慕课网看 git 教程时看到 github 可以帮你展示网页哟,于是我便有了这个案例库. 网址:https://foreverz133.github ...

  8. (二)、Struts第二天

    (二).Struts第二天 回顾: 问题: 1. Struts2的Action类是单例还是多例? Filter? Servlet? Listener? 2. 介绍struts2框架引入的相关jar包及 ...

  9. IE7&IE8不支持rgba的方法

    使用滤镜功能 filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#BF000000,endColorstr=#BF0000 ...

  10. jna

    http://topmanopensource.iteye.com/blog/1752355