一、说明
创建好的jar 将上传至hadoop集群执行,不在windows下搭建hadoop环境,但是需要用到一些hadoop的jar包。
二、概括
Windows下编译MapReduce程序,可以选择eclipse,也可以选择Intellij IDEA。
选择Intellij IDEA有:通过maven方式、手动添加jar两张方式。
三、创建/编译MapReduce项目的具体步骤
1、使用eclipse编译MapReduce程序,并创建jar包
2、使用Intellij IDEA编译MapReduce程序,并创建jar包
1)第一种:通过手动添加jar包的方式。这种方式与eclipse基本一样。
步骤一:创建项目
步骤二:添加jar包。(补充批量添加jar包的方法)
步骤三:打jar包
**注:Intellij IDEA 添加jar包的三种方式
2)第二种:通过maven的方式。
通过maven的方式创建项目时,主要注意在pom.xml文件中添加依赖。
四、上传jar包至hadoop集群运行
命令行执行MapReduce程序
hadoop jar first t.Count ./new_input/* ./n_o8
hadoop jar w.jar ./new_input/* ./n_o10查看输出结果
hdfs dfs -text ./n_o10
五、附例程
package t;
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 Count {
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 {
// 权限问题 设置hadoop用户
// System.setProperty("HADOOP_USER_NAME", "root");
// 创建配置对象
Configuration conf = new Configuration();
// 创建job对象
Job job = Job.getInstance(conf, "word count");
// 设置运行job的类
job.setJarByClass(Count.class);
// 指定reduce数量
job.setNumReduceTasks(1);
// 设置mapper类
job.setMapperClass(TokenizerMapper.class);
// 设置reduce类
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
// 设置map输出的key value
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// 设置reduce输出的key value值
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);
// 提交job
boolean b = job.waitForCompletion(true);
if(!b){
System.out.println("wordcount task fail!");
}
}
}
网友评论