创建Java文件
[root@hadoop hadoop-2.7.4]# cat WordCount.java
import java.io.IOException;
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;
public class WordCount {
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);
}
}
}
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);
}
}
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);
}
}
添加环境变量
[root@hadoop hadoop-2.7.4]# tail /etc/profile
......省略一堆原有的配置
export PATH=$PATH:/usr/jdk64/jdk1.8.0_144/bin/
export PATH=$PATH:/usr/hadoop/hadoop-2.7.4/bin/
export PATH=$PATH:/usr/hadoop/hadoop-2.7.4/sbin/
export HADOOP_HOME=/usr/hadoop/hadoop-2.7.4/
export JAVA_HOME=/usr/jdk64/jdk1.8.0_144/
export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$CLASSPATH
export CLASSPATH=.:$HADOOP_HOME/share/hadoop/common/hadoop-common-2.7.4.jar:$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.7.4.jar:$HADOOP_HOME/share/hadoop/common/lib/commons-cli-1.2.jar:$CLASSPATH
编译java原文件&create a jar
[root@hadoop hadoop-2.7.4]# javac WordCount.java
[root@hadoop hadoop-2.7.4]# ll WordCount*
-rw-r--r-- 1 root root 1491 Sep 25 03:51 WordCount.class
-rw-r--r-- 1 root root 1739 Sep 25 03:51 WordCount$IntSumReducer.class
-rw-r--r-- 1 root root 2089 Sep 25 03:34 WordCount.java
-rw-r--r-- 1 root root 1736 Sep 25 03:51 WordCount$TokenizerMapper.class
[root@hadoop hadoop-2.7.4]# jar cf wc.jar WordCount*.class
确保启动hdfs和yarn
[root@hadoop hadoop-2.7.4]# start-dfs.sh
Starting namenodes on [localhost]
localhost: starting namenode, logging to /usr/hadoop/hadoop-2.7.4/logs/hadoop-root-namenode-hadoop.out
localhost: starting datanode, logging to /usr/hadoop/hadoop-2.7.4/logs/hadoop-root-datanode-hadoop.out
Starting secondary namenodes [0.0.0.0]
0.0.0.0: starting secondarynamenode, logging to /usr/hadoop/hadoop-2.7.4/logs/hadoop-root-secondarynamenode-hadoop.out
[root@hadoop hadoop-2.7.4]# start-yarn.sh
starting yarn daemons
starting resourcemanager, logging to /usr/hadoop/hadoop-2.7.4/logs/yarn-root-resourcemanager-hadoop.out
localhost: starting nodemanager, logging to /usr/hadoop/hadoop-2.7.4/logs/yarn-root-nodemanager-hadoop.out
创建测试文件
[root@hadoop hadoop-2.7.4]# echo Hello World Bye World > file1
[root@hadoop hadoop-2.7.4]# echo Hello World Bye World > file2
[root@hadoop hadoop-2.7.4]# hadoop fs -mkdir /user/root/input-m/
[root@hadoop hadoop-2.7.4]# hadoop fs -put file* /user/root/input-m/
Run the application
[root@hadoop hadoop-2.7.4]# hadoop jar wc.jar WordCount /user/root/input-m/ /user/root/output-m/
17/09/25 05:49:43 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032
17/09/25 05:49:45 WARN mapreduce.JobResourceUploader: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
17/09/25 05:49:45 INFO input.FileInputFormat: Total input paths to process : 2
17/09/25 05:49:46 INFO mapreduce.JobSubmitter: number of splits:2
......省略一堆输出
验证结果
[root@hadoop hadoop-2.7.4]# hadoop fs -cat /user/root/output-m/*
Bye 2
Hello 2
World 4
常见报错
[root@hadoop hadoop-2.7.4]# javac WordCount.java
WordCount.java:4: error: package org.apache.hadoop.conf does not exist
import org.apache.hadoop.conf.Configuration;
^
WordCount.java:5: error: package org.apache.hadoop.fs does not exist
import org.apache.hadoop.fs.Path;
解决方法:http://blog.csdn.net/xiewendong93/article/details/46754595