Mapreduce《案例之倒排索引》
源数据:
1)file1:
MapReduce is simple
2)file2:
MapReduce is powerful is simple
3)file3:
Hello MapReduce bye MapReduce
要实现的结果:
样例输出如下所示。
MapReduce file1.txt:1;file2.txt:1;file3.txt:2;
is file1.txt:1;file2.txt:2;
simple file1.txt:1;file2.txt:1;
powerful file2.txt:1;
Hello file3.txt:1;
bye file3.txt:1;
=========================JAVA CODE======================
package gq;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/**
*
* Class Description:分词小demo测试类
*
* Author:gaoqi
*
* Date:2015年6月5日 下午2:03:08
*
*/
public class FenCi {
public static class Map extends Mapper<LongWritable, Text, Text, Text>{
private FileSplit fileSplit;
public void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException{
fileSplit = (FileSplit) context.getInputSplit();
StringTokenizer stk = new StringTokenizer(value.toString().trim());
while(stk.hasMoreElements()){
String v = stk.nextToken();
String path = fileSplit.getPath().toString();
String filename = path.substring(path.indexOf("file"));
context.write(new Text(v+":"+filename), new Text("1"));
}
}
}
public static class Combiner extends Reducer<Text, Text, Text, Text>{
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
int sum = 0;
for(Text t :values){
sum += Integer.parseInt(t.toString());
}
String v = key.toString();
context.write(new Text(v.substring(0,v.indexOf(":"))), new Text(v.substring(v.indexOf(":")+1)+":"+sum));
}
}
public static class Reduce extends Reducer<Text, Text, Text, Text>{
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
Iterator<Text> its = values.iterator();
String value = "";
while(its.hasNext()){
value+=its.next()+";";
}
context.write(key, new Text(value));
}
}
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
Job job = new Job(conf,"FenCi");
job.setJarByClass(FenCi.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Combiner.class);
job.setReducerClass(Reduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("hdfs://h0:9000/user/tallqi/in/inputInvertedIndex"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://h0:9000/user/tallqi/in/outputFenci"));
System.exit(job.waitForCompletion(true)?0:1);
}
}
相关推荐
基于MapReduce的简单倒排索引的建立
MapReduce操作实例-倒排索引.pdf 学习资料 复习资料 教学资源
Hadoop mapreduce 实现InvertedIndexer倒排索引,能用。
运行说明:在linux终端输入 $ hadoop jar test-1.0-SNAPSHOT.jar WordCount /input/* /MyOutput1/ 后两个参数是hdfs上面【输入】的文本文件目录和【输出】目录。 记得清空输出目录。
倒排索引源于实际应用中需要根据属性的值来查找记录。这种索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址。由于不是由记录来确定属性值,而是由属性值来确定记录的位置,因而称为倒排索引(inverted ...
文档倒排索引的MapReduce程序设计与实现
人工智能-hadoop
#MapReduce 倒排索引 这是获取倒排索引的示例 MapReduce 代码 输入文件格式 推文ID,主题标签 将输入文件复制到 HDFS hadoop fs -copyFromLocal $HOME/sampleInput.txt /sampleInput.txt 执行 MapReduce 作业 ...
倒排索引(Inverted Index)被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射,是目前几乎所有支持...资源中包含了MapReduce实现的文档倒排索引的源码、jar包、测试数据(包含停词表)和结果。
从财经新闻网页数据开始,进行正文提取、中文分词、倒排索引构建、执行搜索和UI。 要求技术:MapReduce或Spark;执行搜索和UI采用Spark或Java 步骤: (1)新闻正文提取,采用正则表达式提取指定网站栏目新闻的标题...
了解map和reduce工作原理,以及排序,分组,分区设置,有详细的注释,方便查看学习,适合入门初学者练手
针对SSE-1密文检索方案的一些性能缺陷,采用不同的加密策略,在lucene倒排索引的基础上,设计了密文倒排索引Crypt-Lucene,同时结合云计算特点,设计了并行构建Crypt-Lucene方案,理论分析了方案的性能,并通过实验...
1.社交网络综合评分案例 2.微博精准营销案例 3.物品推荐案例 4.QQ好友推荐案例
词频统计+倒排索引+数据去重+TopN
2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx
需求 有如下数据 a.txt hello tom hello jim hello kitty hello rose b.txt hello jerry hello jim hello kitty hello jack c.txt hello jerry hello java hello c++ hello c++ 需要输出如下格式: ...1
大数据mapreduce案例介绍,包括代码解释,详解MRS工作流程
mapreduce案例测试数据
mapreduce案例测试数据
MapReduce程序 完整实验报告 和 jar包 和简单实验数据