IDEA 运行 Hadoop WordCount示例

1、在本地解压hadoop安装包,然后修改系统变量,增加HADOOP_HOME及HADOOP_USER_NAME,HADOOP_USER_NAME为实际集群运行用户

IDEA 运行 Hadoop WordCount示例_第1张图片

2、修改项目的Pom文件


        
            org.apache.hadoop
            hadoop-common
            2.9.0
        
        
            org.apache.hadoop
            hadoop-client
            2.9.0
        
        
            org.apache.hadoop
            hadoop-hdfs
            2.9.0
        
        
            org.apache.hadoop
            hadoop-mapreduce-client-core
            2.9.0
        
        
            org.apache.hadoop
            hadoop-mapreduce-client-jobclient
            2.9.0
        
        
            org.apache.hadoop
            hadoop-mapreduce-client-common
            2.9.0
        
        
            commons-cli
            commons-cli
            1.2
        
    

3、将core-site.xml、hdfs-site.xml、mapred-site.xml、yarn-site.xml、log4j.properties拷贝至resources目录

在mapred-site.xml设置

    
        mapreduce.app-submission.cross-platform
        true
    
    
        mapred.jar
        E:\Projects\hadoop\HadoopExercise\target\HadoopExercise-1.0-SNAPSHOT.jar
    

4、示例程序

Mapper

package org.zheng.demo;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

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

public class TokenizerMapper
        extends Mapper {

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

    private Text word = new Text();

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

Reducer

package org.zheng.demo;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class IntSumReducer
        extends Reducer {

    private IntWritable result = new IntWritable();

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

Main

package org.zheng.demo;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount {

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        //设置RM 访问位置
        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);
    }
}

5、编辑运行选项,设置参数

IDEA 运行 Hadoop WordCount示例_第2张图片

6、运行

你可能感兴趣的:(Hadoop)