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

ffdz8vbo  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(439)

我想用hadoop实现一个字符串匹配(boyer-moore)算法。我刚开始使用hadoop,所以我不知道如何用java编写hadoop程序。
到目前为止,我看到的所有示例程序都是单词计数示例,我找不到任何用于字符串匹配的示例程序。
我试着搜索一些教我如何使用java编写hadoop应用程序的教程,但是找不到。你能给我推荐一些教程,让我学习如何使用java编写hadoop应用程序吗。
提前谢谢。

x6h2sr28

x6h2sr281#

我还没有测试下面的代码,但这应该让你开始。我使用了这里提供的boyermoore实现
下面的代码正在执行的操作:
目标是在输入文档中搜索模式。boyermoore类在setup方法中使用配置中设置的模式进行初始化。
Map器一次接收每一行,并使用boyermoore示例来查找模式。如果找到匹配项,我们将使用上下文编写它。
这里不需要减速器。如果在不同的Map器中多次找到该模式,则输出将具有多个偏移(每个Map器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);
      }
}
clj7thdc

clj7thdc2#

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

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(amazonwebservices),所以我可以从控制台中选择要同时运行程序的节点数。因此,我假设我使用的map和reduce方法应该足以并行运行boyer-moore字符串匹配算法。

相关问题