The first hadoop program - WordCount

The first hadoop program - WordCount

  • Create a new maven project and add dependencies as follows
<dependencies>
      <dependency>
          <groupId>org.apache.hadoop</groupId>
          <artifactId>hadoop-common</artifactId>
          <version>2.7.1</version>
      </dependency>

      <dependency>
          <groupId>org.apache.hadoop</groupId>
          <artifactId>hadoop-mapreduce-client-core</artifactId>
          <version>2.7.1</version>
      </dependency>
  </dependencies>
  • Write WordCount source code
import org.apache.hadoop.conf.Configuration;
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.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 java.io.IOException;
import java.util.StringTokenizer;

/**
 * Created by JackManWu on 2018/2/18.
 */
public class WordCount {
    public static class TokenizerMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        @Override
        protected void map(LongWritable 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);
            }
        }
    }

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

        @Override
        protected 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);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        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);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
  • Upload source code to hadoop cluster server
rz
  • Running tests on hadoop cluster
    • Compile the source code and create a jar package
    hadoop com.sun.tools.javac.Main WordCount.jar
    jar cf wc.jar WordCount*.class
    
    • Create input file
    echo "Hello World Bye World" > file01
    echo "Hello Hadoop GoodBye Hadoop" > file02
    
    • Upload to hdfs file system
    hdfs dfs -mkdir -p ~/work/input
    hdfs dfs -put ~/work/file0* ~/work/input
    
    • View uploaded files
    hadoop fs -ls ~/work/input
    
    • run the program
    hadoop jar wc.jar WordCount ~/work/input ~/work/output
    
    • View production results
    hadoop fs -ls ~/work/output
    hadoop fs -cat ~/work/output/part-r-00000
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324943901&siteId=291194637