Προγραμματισμός

* Γνώση Υπολογιστών >> Προγραμματισμός >  >> Γλώσσες Προγραμματισμού

Πώς να δημιουργήσετε ανεστραμμένο δείκτη με MapReduce

Οδηγίες

  1. Γράψτε τον ακόλουθο κώδικα για τη συνάρτηση map:
    public static class InvertedIndexerMapper extends MapReduceBase
    implements Mapper
    {
    private final static Text word = new Text ();
    private final static Text location = new Text ();

    public void map(LongWritable key, Text val,
    OutputCollector output, Reporter reporter)
    throws IOException
    {
    FileSplit fileSplit = (FileSplit) reporter.getInputSplit();
    String fileName = fileSplit.getPath().getName();
    location.set(fileName);

    String line = val.toString();
    StringTokenizer itr = new StringTokenizer(line.toLowerCase());
    while (itr.hasMoreTokens()) {
    word.set(itr.nextToken());
    output.collect(word, location);
    }
    }
    }
  2. Γράψτε τον ακόλουθο κώδικα για τη συνάρτηση reduce:
    public static class InvertedIndexerReducer extends MapReduceBase
    implements Reducer
    {
    public void reduce(Text key, Iterator values,
    OutputCollector output,
    Reporter reporter) throws IOException
    {
    boolean first = true;
    StringBuilder toReturn = new StringBuilder();
    while (values.hasNext()) {
    if (!first)
    toReturn.append(", ");
    first = false;
    toReturn.append(values.next().toString());
    }
    output.collect(key, new Text(toReturn.toString()));
    }
    }
  3. Γράψτε τον ακόλουθο κώδικα για να ολοκληρώσετε τον ανεστραμμένο δείκτη:
    public static void main(String[] args) throws IOException
    {
    if (args.length < 2) {
    System.out
    println("Usage: InvertedIndex ");
    system.exit(1);
    }
    JobConf conf = new JobConf(InvertedIndex.class);
    conf.setJobName("InvertedIndex");

    conf.setOutputKeyClass(Text.class);
    conf.setOutputValueClass(Text.class);

    conf.setMapperClass(InvertedIndexerMapper.class);
    conf.setReducerClass(InvertedIndexerReducer.class);

    FileInputFormat.setInputPaths(conf, new Path(args[0]));
    FileOutputFormat.setOutputPath(conf, new Path(args[1]));
    try {
    JobClient.runJob(conf);
    } catch (Exception e) {
    e.pringStackTrace();
    }
    }

Σχετικά άρθρα

Πνευματικά δικαιώματα © Γνώση Υπολογιστών Όλα τα δικαιώματα διατηρούνται.