com.hzy.test.WordCount.java Source code

Java tutorial

Introduction

Here is the source code for com.hzy.test.WordCount.java

Source

package com.hzy.test;

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.io.IOException;
import java.util.StringTokenizer;

import com.hzy.util.HdfsDAO;
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;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class WordCount {
    private static final Logger logger = LogManager.getLogger(WordCount.class);

    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
            }
            //            KPI kpi = KPI.filterPVs(value.toString());
            //            if (kpi.isValid()) {
            //                word.set(kpi.getRemote_addr());
            //                context.write(word, one);
            //                //logger.info("KPIPVMapper    " + word + " "+one);
            //            }
        }
    }

    public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();

        public void reduce(Text key, Iterable<IntWritable> values, Context context)
                throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            result.set(sum);
            context.write(key, result);

            logger.info(key);
            logger.info(result);
        }
    }

    public static void main(String[] args) throws Exception {
        //        String input = "hdfs://192.168.1.118:9000/user/hdfs/log_kpi/";
        //        String output = "hdfs://192.168.1.118:9000/user/hdfs/log_kpi/wc/";
        String input = "/tmp/data.txt";
        // String input = args[0];
        String output = "/tmp/t1";

        // String output = args[1];

        Configuration conf = HdfsDAO.config();

        //        conf.set("mapreduce.framework.name", "yarn");
        ////        conf.set("hbase.zookeeper.quorum", "hadoop01:2181");
        //        conf.set("fs.default.name", "hdfs://hadoop01:9000");
        //        conf.set("yarn.resourcemanager.resource-tracker.address", "hadoop01:8031");
        //        conf.set("yarn.resourcemanager.address", "hadoop01:8032");
        //        conf.set("yarn.resourcemanager.scheduler.address", "hadoop01:8030");
        //        conf.set("yarn.resourcemanager.admin.address", "hadoop01:8033");
        //        conf.set("mapreduce.jobhistory.address", "hadoop01:10020");
        //        conf.set("mapreduce.jobhistory.webapp.address", "hadoop01:19888");

        //        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        //        if (otherArgs.length < 2) {
        //            System.err.println("Usage: wordcount <in> [<in>...] <out>");
        //            System.exit(2);
        //        }
        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //        for (int i = 0; i < otherArgs.length - 1; ++i) {
        FileInputFormat.addInputPath(job, new Path(input));
        //        }
        FileOutputFormat.setOutputPath(job, new Path(output));

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