如何用Hadoop实现字符串匹配算法?

时间:2015-11-13 02:41:09

标签: java hadoop string-matching boyer-moore

我想使用Hadoop实现字符串匹配(Boyer-Moore)算法。我刚开始使用Hadoop,所以我不知道如何用Java编写Hadoop程序。

到目前为止,我看到的所有示例程序都是字数统计示例,我找不到任何用于字符串匹配的示例程序。

我尝试搜索一些教程,教授如何使用Java编写Hadoop应用程序但找不到任何内容。你能给我一些教程,我可以学习如何使用Java编写Hadoop应用程序。

提前致谢。

2 个答案:

答案 0 :(得分:2)

我没有测试下面的代码,但这应该让你开始。 我使用了可用的BoyerMoore实现here

以下代码正在做什么:

目标是在输入文档中搜索模式。使用配置中设置的模式在设置方法中初始化BoyerMoore类。

映射器一次接收每一行,并使用BoyerMoore实例查找模式。如果找到匹配,我们使用上下文编写它。

这里不需要减速器。如果在不同的映射器中多次找到模式,则输出将具有多个偏移(每个映射器1个)。

package hadoop.boyermoore;

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.IntWritable;
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.output.FileOutputFormat;

public class BoyerMooreImpl {


      public static class TokenizerMapper
           extends Mapper<Object, Text, Text, IntWritable>{
        private BoyerMoore boyerMoore;
        private static IntWritable offset;
        private Text offsetFound = new Text("offset");

        public void map(Object key, Text value, Context context
                        ) throws IOException, InterruptedException {
          StringTokenizer itr = new StringTokenizer(value.toString());
          while (itr.hasMoreTokens()) {
              String line = itr.nextToken();
              int offset1 = boyerMoore.search(line);
              if (line.length() != offset1) {
                  offset = new IntWritable(offset1);
                  context.write(offsetFound,offset);
              }
          }
        }
        @Override
        public final void setup(Context context) {
            if (boyerMoore == null)
                boyerMoore = new BoyerMoore(context.getConfiguration().get("pattern"));
        }
      }


      public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.set("pattern","your_pattern_here");
        Job job = Job.getInstance(conf, "BoyerMoore");
        job.setJarByClass(BoyerMooreImpl.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
      }
}

答案 1 :(得分:0)

我不知道这是否是并行运行算法的正确实现,但这是我想到的,

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.util.*;

public class StringMatching extends Configured implements Tool {

  public static void main(String args[]) throws Exception {
      long start = System.currentTimeMillis();
      int res = ToolRunner.run(new StringMatching(), args);
      long end = System.currentTimeMillis();
      System.exit((int)(end-start));
  }

  public int run(String[] args) throws Exception {
    Path inputPath = new Path(args[0]);
    Path outputPath = new Path(args[1]);

    Configuration conf = getConf();
    Job job = new Job(conf, this.getClass().toString());

    FileInputFormat.setInputPaths(job, inputPath);
    FileOutputFormat.setOutputPath(job, outputPath);

    job.setJobName("StringMatching");
    job.setJarByClass(StringMatching.class);
    job.setInputFormatClass(TextInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(IntWritable.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    job.setMapperClass(Map.class);
    job.setCombinerClass(Reduce.class);
    job.setReducerClass(Reduce.class);

    return job.waitForCompletion(true) ? 0 : 1;
  }

  public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    @Override
    public void map(LongWritable key, Text value,
                    Mapper.Context context) throws IOException, InterruptedException {
      String line = value.toString();
      StringTokenizer tokenizer = new StringTokenizer(line);
      while (tokenizer.hasMoreTokens()) {
        word.set(tokenizer.nextToken());
        context.write(word, one);
      }
    }
  }

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

    @Override
    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        BoyerMoore bm = new BoyerMoore(); 
        boolean flag = bm.findPattern(key.toString().trim().toLowerCase(), "abc");
        if(flag){
            context.write(key, new IntWritable(1));
        }else{
            context.write(key, new IntWritable(0));
        }
    }
  }

}

我正在使用AWS(亚马逊网络服务),因此我可以从控制台中选择我希望我的程序同时运行的节点数。所以我假设我使用的map和reduce方法应该足以并行运行Boyer-Moore字符串匹配算法。