第一个hadoop程序-WordCount

第一个hadoop程序-WordCount

  • 新建maven项目,添加依赖如下
<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>
  • 编写WordCount源码
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);
    }
}
  • 上传源代码到hadoop集群服务器
rz
  • hadoop集群上运行测试
    • 编译源码并创建jar包
    hadoop com.sun.tools.javac.Main WordCount.jar
    jar cf wc.jar WordCount*.class
    
    • 创建输入文件
    echo "Hello World Bye World" > file01
    echo "Hello Hadoop GoodBye Hadoop" > file02
    
    • 上传到hdfs文件系统
    hdfs dfs -mkdir -p ~/work/input
    hdfs dfs -put ~/work/file0* ~/work/input
    
    • 查看上传的文件
    hadoop fs -ls ~/work/input
    
    • 运行程序
    hadoop jar wc.jar WordCount ~/work/input ~/work/output
    
    • 查看生产的结果
    hadoop fs -ls ~/work/output
    hadoop fs -cat ~/work/output/part-r-00000
    

猜你喜欢

转载自my.oschina.net/u/3163032/blog/1622366