MapReduce实现倒排序索引
来源:互联网 发布:淘宝家具安装工挣钱 编辑:程序博客网 时间:2024/05/18 01:55
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;
public class InversedIndex {
/**
* 将输入文件拆分,
* 将关键字和关键字所在的文件名作为map的key输出,
* 该组合的频率作为value输出
* */
public static class InversedIndexMapper extends Mapper<Object, Text, Text, Text> {
private Text outKey = new Text();
private Text outVal = new Text();
@Override
public void map (Object key,Text value,Context context) {
StringTokenizer tokens = new StringTokenizer(value.toString());
FileSplit split = (FileSplit) context.getInputSplit();
while(tokens.hasMoreTokens()) {
String token = tokens.nextToken();
try {
outKey.set(token + ":" + split.getPath());
outVal.set("1");
context.write(outKey, outVal);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* map的输出进入到combiner阶段,此时来自同一个文件的相同关键字进行一次reduce处理,
* 将输入的key拆分成关键字和文件名,然后关键字作为输出key,
* 将文件名与词频拼接,作为输出value,
* 这样就形成了一个关键字,在某一文件中出现的频率的 key--value 对
* */
public static class InversedIndexCombiner extends Reducer<Text, Text, Text, Text> {
private Text outKey = new Text();
private Text outVal = new Text();
@Override
public void reduce(Text key,Iterable<Text> values,Context context) {
String[] keys = key.toString().split(":");
int sum = 0;
for(Text val : values) {
sum += Integer.parseInt(val.toString());
}
try {
outKey.set(keys[0]);
int index = keys[keys.length-1].lastIndexOf('/');
outVal.set(keys[keys.length-1].substring(index+1) + ":" + sum);
context.write(outKey, outVal);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 将combiner后的key value对进行reduce,
* 由于combiner之后,一个关键字可能对应了多个value,故需要将这些value进行合并输出
* */
public static class InversedIndexReducer extends Reducer<Text, Text, Text, Text> {
@Override
public void reduce (Text key,Iterable<Text> values,Context context) {
StringBuffer sb = new StringBuffer();
for(Text text : values) {
sb.append(text.toString() + " ,");
}
try {
context.write(key, new Text(sb.toString()));
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
Job job = new Job(conf,"index inversed");
job.setJarByClass(InversedIndex.class);
job.setMapperClass(InversedIndexMapper.class);
job.setCombinerClass(InversedIndexCombiner.class);
job.setReducerClass(InversedIndexReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("inversed.files"));
FileOutputFormat.setOutputPath(job, new Path("inversed.result"));
System.exit(job.waitForCompletion(true)?0:1);
}
}
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;
public class InversedIndex {
/**
* 将输入文件拆分,
* 将关键字和关键字所在的文件名作为map的key输出,
* 该组合的频率作为value输出
* */
public static class InversedIndexMapper extends Mapper<Object, Text, Text, Text> {
private Text outKey = new Text();
private Text outVal = new Text();
@Override
public void map (Object key,Text value,Context context) {
StringTokenizer tokens = new StringTokenizer(value.toString());
FileSplit split = (FileSplit) context.getInputSplit();
while(tokens.hasMoreTokens()) {
String token = tokens.nextToken();
try {
outKey.set(token + ":" + split.getPath());
outVal.set("1");
context.write(outKey, outVal);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* map的输出进入到combiner阶段,此时来自同一个文件的相同关键字进行一次reduce处理,
* 将输入的key拆分成关键字和文件名,然后关键字作为输出key,
* 将文件名与词频拼接,作为输出value,
* 这样就形成了一个关键字,在某一文件中出现的频率的 key--value 对
* */
public static class InversedIndexCombiner extends Reducer<Text, Text, Text, Text> {
private Text outKey = new Text();
private Text outVal = new Text();
@Override
public void reduce(Text key,Iterable<Text> values,Context context) {
String[] keys = key.toString().split(":");
int sum = 0;
for(Text val : values) {
sum += Integer.parseInt(val.toString());
}
try {
outKey.set(keys[0]);
int index = keys[keys.length-1].lastIndexOf('/');
outVal.set(keys[keys.length-1].substring(index+1) + ":" + sum);
context.write(outKey, outVal);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 将combiner后的key value对进行reduce,
* 由于combiner之后,一个关键字可能对应了多个value,故需要将这些value进行合并输出
* */
public static class InversedIndexReducer extends Reducer<Text, Text, Text, Text> {
@Override
public void reduce (Text key,Iterable<Text> values,Context context) {
StringBuffer sb = new StringBuffer();
for(Text text : values) {
sb.append(text.toString() + " ,");
}
try {
context.write(key, new Text(sb.toString()));
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
Job job = new Job(conf,"index inversed");
job.setJarByClass(InversedIndex.class);
job.setMapperClass(InversedIndexMapper.class);
job.setCombinerClass(InversedIndexCombiner.class);
job.setReducerClass(InversedIndexReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("inversed.files"));
FileOutputFormat.setOutputPath(job, new Path("inversed.result"));
System.exit(job.waitForCompletion(true)?0:1);
}
}
0 0
- MapReduce实现倒排序索引
- MapReduce实现倒排索引全排序
- mapreduce倒排序索引
- mapreduce倒排序索引
- mapreduce实现倒排索引
- MapReduce实现倒排索引
- mapreduce实现倒排索引
- MapReduce倒排索引实现
- MapReduce实现倒排索引
- MapReduce倒排索引简单实现
- MapReduce 倒排索引的实现
- Mapreduce实现倒排索引建立
- MapReduce--倒排索引
- mapreduce--倒排索引
- MapReduce编程之倒排索引的实现
- mapreduce实现搜索引擎简单的倒排索引
- MapReduce之三—搜索引擎-倒排索引实现
- 使用MapReduce实现简单的倒排索引
- 序列化---序列化的含义和意义
- Java并发编程之线程管理(高级线程同步8)
- Linux安装MySQL数据库
- LeetCode之Single Number II
- powerpc的linux驱动writel的疑问
- MapReduce实现倒排序索引
- LeetCode之removeDuplicates
- 一个fork的面试题
- java中的socket编程的入门源码
- 常见笔试题一
- 被包围的棋子 Surrounded Regions
- 如何注册installshield 12
- 用rewind函数使位置指针指向文件头
- Boost.Asio 介绍--之二