edu.umd.cloud9.collection.aquaint2.DemoCountAquaint2Documents.java Source code

Java tutorial

Introduction

Here is the source code for edu.umd.cloud9.collection.aquaint2.DemoCountAquaint2Documents.java

Source

/*
 * Cloud9: A MapReduce Library for Hadoop
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package edu.umd.cloud9.collection.aquaint2;

import java.io.IOException;

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class DemoCountAquaint2Documents extends Configured implements Tool {

    private static enum Count {
        DOCS
    };

    private static class MyMapper extends MapReduceBase
            implements Mapper<LongWritable, Aquaint2Document, LongWritable, Text> {
        public void map(LongWritable key, Aquaint2Document doc, OutputCollector<LongWritable, Text> output,
                Reporter reporter) throws IOException {
            reporter.incrCounter(Count.DOCS, 1);
        }
    }

    /**
     * Creates an instance of this tool.
     */
    public DemoCountAquaint2Documents() {
    }

    private static int printUsage() {
        System.out.println("usage: [input] [output-dir] [mappings-file] [num-mappers]");
        ToolRunner.printGenericCommandUsage(System.out);
        return -1;
    }

    /**
     * Runs this tool.
     */
    public int run(String[] args) throws Exception {
        if (args.length != 4) {
            printUsage();
            return -1;
        }

        String inputPath = args[0];
        String outputPath = args[1];
        String mappingFile = args[2];
        int mapTasks = Integer.parseInt(args[3]);

        System.out.println("input dir: " + inputPath);
        System.out.println("output dir: " + outputPath);
        System.out.println("mapping file: " + mappingFile);
        System.out.println("number of mappers: " + mapTasks);

        JobConf conf = new JobConf(DemoCountAquaint2Documents.class);
        conf.setJobName("DemoCountAquaint2Documents");

        conf.setNumMapTasks(mapTasks);
        conf.setNumReduceTasks(0);

        FileInputFormat.setInputPaths(conf, new Path(inputPath));
        FileOutputFormat.setOutputPath(conf, new Path(outputPath));
        FileOutputFormat.setCompressOutput(conf, false);

        conf.setInputFormat(Aquaint2DocumentInputFormatOld.class);
        conf.setOutputKeyClass(Text.class);
        conf.setOutputValueClass(IntWritable.class);

        conf.setMapperClass(MyMapper.class);

        // delete the output directory if it exists already
        FileSystem.get(conf).delete(new Path(outputPath), true);

        JobClient.runJob(conf);

        // clean up
        FileSystem.get(conf).delete(new Path(outputPath), true);

        return 0;
    }
}