你好,游客 登录 注册 搜索
背景:
阅读新闻

Hadoop倒排索引

[日期:2013-05-14] 来源:Linux社区  作者:limiteewaltwo [字体:大 中 小]

如果文章中没有相应的单词的话,就不用出现。

首先,我们需要一个工具类,把每行的String,分解成一个一个的单词:

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hadoop
 *
 */
public class StringUtil {
 
 /**
  * 获得字符串中单词
  * @param value
  * @return
  */
 public static List<String> getWords(String value)
 {
  List<String> wordList = new ArrayList<String>();
  int len = value.length();
  char[] charArray = value.toCharArray();
  char[] word = new char[40];
  int wordIndex = 0;
  for(int i = 0; i < len; i++)
  {
   char c = charArray[i];
   if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
   {
    word[wordIndex] = c;
    wordIndex++;
   }
   else
   {
    if(wordIndex > 0)
    {
     wordList.add(String.valueOf(word, 0, wordIndex));
     wordIndex = 0;
    }
   }
  }
  return wordList;
 }
}

然后就是我们的mapreduce啦:

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.MapWritable;
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 com.test.util.HdfsFileUtil;
import com.test.util.Prop;
import com.test.util.StringUtil;

/**
 * @author hadoop
 *
 */
public class Seach {

 private static Log log = LogFactory.getLog(Seach.class);

 
 
 public static class Map extends Mapper<Object, Text, Text, MapWritable> {

  private FileSplit split;
 
  private static IntWritable data = new IntWritable(1);
 
  public void map(Object key, Text value, Context context)
    throws IOException, InterruptedException {
   split = (FileSplit)context.getInputSplit();
   MapWritable map = new MapWritable();
   List<String> wordList = StringUtil.getWords(value.toString());
   int len = wordList.size();
   for(int i = 0; i < len; i++)
   {
    map.put(new Text(split.getPath().getName()), data);
    context.write(new Text(wordList.get(i)), map);
   }
  }
 }

 public static class Reduce extends Reducer<Text, MapWritable, Text, Text> {

  public void reduce(Text key, Iterable<MapWritable> values,
    Context context) throws IOException, InterruptedException {
   int count = 0;
   java.util.Map<String, Integer> countMap = new HashMap<String, Integer>();
   Iterator<MapWritable> iterator = values.iterator();
   while(iterator.hasNext())
   {
    MapWritable curMap = iterator.next();
    String fileName = curMap.keySet().iterator().next().toString();
    if(countMap.containsKey(fileName))
    {
     countMap.put(fileName, countMap.get(fileName) + 1);
    }
    else
    {
     countMap.put(fileName, 1);
    }
    count++;
   }
   int fCount = 0;
   String value = "";
   Iterator<String> it = countMap.keySet().iterator();
   while(it.hasNext())
   {
    if(fCount > 0)
    {
     value += "-";
    }
    String fileName = it.next();
    value += fileName + ":" + countMap.get(fileName);
    fCount++;
   }
   context.write(key, new Text(count + "-" + value));
  }
 }

 /**
  * @param args
  */
 public static void main(String[] args) throws Exception {
  String inputPath = "search_in";
     String outPath = "search_out";
     Configuration conf = new Configuration();
        conf.set("mapred.job.tracker", Prop.HADOOP_MAPRED_JOB_TRACKER);
        conf.set("fs.default.name", Prop.HDFS_HOST);
        HdfsFileUtil.checkAndDelete(conf, "/" + Prop.HDFS_DIRECTORY + "/" + inputPath);
        HdfsFileUtil.checkAndDelete(conf, "/" + Prop.HDFS_DIRECTORY + "/" + outPath);
     HdfsFileUtil.upload(conf, Prop.LOCAL_HDFS_DIRECTORY + "/" + inputPath, Prop.HDFS_DIRECTORY + "/" + inputPath);
     
        Job job = new Job(conf, "search");
        job.setJarByClass(STjoin.class);
        // 设置Map和Reduce处理类
        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);
        // 设置输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(MapWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        // 设置输入和输出目录
        FileInputFormat.addInputPath(job, new Path(inputPath));
        FileOutputFormat.setOutputPath(job, new Path(outPath));
       
        boolean exitStatus = job.waitForCompletion(true);
  HdfsFileUtil.download(conf, "/" + Prop.HDFS_DIRECTORY + "/" + outPath, Prop.LOCAL_HDFS_DIRECTORY + "/" + outPath);
  System.exit(exitStatus ? 0 : 1);
 }
}

linux
相关资讯       Hadoop索引 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款