MapReduce初探

编译、打包 Hadoop MapReduce 程序

HADOOP_CLASSPATH
/app/lib/hadoop-2.7.3/etc/hadoop:
/app/lib/hadoop-2.7.3/share/hadoop/common/lib/:
/app/lib/hadoop-2.7.3/share/hadoop/common/
:
/app/lib/hadoop-2.7.3/share/hadoop/hdfs:
/app/lib/hadoop-2.7.3/share/hadoop/hdfs/lib/:
/app/lib/hadoop-2.7.3/share/hadoop/hdfs/
:
/app/lib/hadoop-2.7.3/share/hadoop/yarn/lib/:
/app/lib/hadoop-2.7.3/share/hadoop/yarn/
:
/app/lib/hadoop-2.7.3/share/hadoop/mapreduce/lib/:
/app/lib/hadoop-2.7.3/share/hadoop/mapreduce/
:
/app/lib/hbase-1.1.5/lib/*:

$CALSSPATH
-bash: .:/app/lib/jdk1.8.0_144/lib/dt.jar:
/app/lib/jdk1.8.0_144/lib/tools.jar:
/app/lib/hadoop-2.7.3/share/hadoop/common/lib:
/app/lib/hadoop-2.7.3/share/hadoop/hdfs

将 Hadoop 的 classhpath 信息添加到 CLASSPATH 变量中

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$($HADOOP_HOME/bin/hadoop classpath):$CLASSPATH

记得 source /etc/profile

 $CLASSPATH
-bash: .:/app/lib/jdk1.8.0_144/lib/dt.jar:/app/lib/jdk1.8.0_144/lib/tools.jar:/app/lib/hadoop-2.7.3/etc/hadoop:/app/lib/hadoop-2.7.3/share/hadoop/common/lib/*:/app/lib/hadoop-2.7.3/share/hadoop/common/*:/app/lib/hadoop-2.7.3/share/hadoop/hdfs:/app/lib/hadoop-2.7.3/share/hadoop/hdfs/lib/*:/app/lib/hadoop-2.7.3/share/hadoop/hdfs/*:/app/lib/hadoop-2.7.3/share/hadoop/yarn/lib/*:/app/lib/hadoop-2.7.3/share/hadoop/yarn/*:/app/lib/hadoop-2.7.3/share/hadoop/mapreduce/lib/*:/app/lib/hadoop-2.7.3/share/hadoop/mapreduce/*:/app/lib/hbase-1.1.5/lib/*:.:/app/lib/jdk1.8.0_144/lib/dt.jar:/app/lib/jdk1.8.0_144/lib/tools.jar:/app/lib/hadoop-2.7.3/share/hadoop/common/lib:/app/lib/hadoop-2.7.3/share/hadoop/hdfs/lib: No such file or directory

配置好环境后 就可以三步走了

  1. javac xx.java
  2. jar -cvf xx.jar xx*.class
  3. hadoop jar xx.jar xx
[xxxycentos@xxxycentos7 MapReduceExample]$ javac WordCount.java 
[xxxycentos@xxxycentos7 MapReduceExample]$ jar -cvf WordCount.jar WordCount*.class
added manifest
adding: WordCount.class(in = 1911) (out= 1044)(deflated 45%)
adding: WordCount$IntSumReducer.class(in = 1744) (out= 741)(deflated 57%)
adding: WordCount$TokenizerMapper.class(in = 1740) (out= 755)(deflated 56%)

[xxxycentos@xxxycentos7 MapReduceExample]$ hadoop jar WordCount.jar WordCount /input /output_1
(过了很久很久)
[xxxycentos@xxxycentos7 MapReduceExample]$ hadoop fs -cat /output_1/part-r-00000
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/app/lib/hadoop-2.7.3/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/app/lib/hbase-1.1.5/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
19/04/19 11:20:12 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Hadoop	1
Hello	2
World	1

WordCount.java 源码

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
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;
 
public class WordCount {
    public WordCount() {
    }
 
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
        if(otherArgs.length < 2) {
            System.err.println("Usage: wordcount  [...] ");
            System.exit(2);
        }
 
        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCount.TokenizerMapper.class);
        job.setCombinerClass(WordCount.IntSumReducer.class);
        job.setReducerClass(WordCount.IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
 
        for(int i = 0; i < otherArgs.length - 1; ++i) {
            FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }
 
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
        System.exit(job.waitForCompletion(true)?0:1);
    }
 
    public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();
 
        public IntSumReducer() {
        }
 
        public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int sum = 0;
 
            IntWritable val;
            for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
                val = (IntWritable)i$.next();
            }
 
            this.result.set(sum);
            context.write(key, this.result);
        }
    }
 
    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
        private static final IntWritable one = new IntWritable(1);
        private Text word = new Text();
 
        public TokenizerMapper() {
        }
 
        public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
 
            while(itr.hasMoreTokens()) {
                this.word.set(itr.nextToken());
                context.write(this.word, one);
            }
 
        }
    }
}
 

你可能感兴趣的:(大数据实验)