hadoop怎样创建 mapreduce项目简易例子教程
来源:互联网 发布:淘宝如何做详情页 编辑:程序博客网 时间:2024/06/13 00:15
搭建好环境的前提下!!!
1.创建mapReduce项目
2.以上一个mapReduce项目就创建完成了,接下来就是代码示例(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;
import org.apache.hadoop.util.GenericOptionsParser;
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();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.out.println("this is a test !");
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(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(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
3.运行步骤:
4.项目运行成功
- hadoop怎样创建 mapreduce项目简易例子教程
- Hadoop MapReduce 编写例子
- hadoop mapreduce 例子
- hadoop MapReduce例子
- MapReduce教程:Hadoop
- hadoop学习;block数据块;mapreduce实现例子;UnsupportedClassVersionError异常;关联项目源码
- Hadoop-MapReduce-TeraSort-大数据排序例子
- hadoop — MapReduce例子 (求平均值)
- 学习Hadoop MapReduce与WordCount例子分析
- hadoop mapreduce统计的小例子
- Hadoop MapReduce编程创建maven项目时所用到的pom依赖
- Hadoop MapReduce执行过程详解(带hadoop例子)
- Hadoop MapReduce执行过程详解(带hadoop例子)
- Hadoop MapReduce执行过程详解(带hadoop例子)
- Hadoop MapReduce执行过程详解(带hadoop例子)
- Hadoop MapReduce执行过程详解(带hadoop例子)
- Hadoop MapReduce执行过程详解(带hadoop例子)
- Hadoop MapReduce执行过程详解(带hadoop例子)
- SSL 的工作原理
- json
- 简单工厂模式的UML类图与源码实现
- Your Ubuntu 12.04 is running in 2D mode,Many features will not be available.(on Ubuntu 12.04)解决方法
- 记一次解决jenkins持续构建,自动部署的问题
- hadoop怎样创建 mapreduce项目简易例子教程
- 当在写android的xml文件中出现Unexpected text found in layout file: """的解决办法
- 设计一个类,只能实现1个实例或3个实例
- 怎么用VS2005打开VS2008
- hdu,2037,今年暑假不AC
- 获取MS SqlServer 2000中字段的备注信息
- LeetCode 007 Reverse Integer
- Mysql数据库学习(四):常用Mysql C API 介绍和使用、封装一个访问Mysql数据库的类MysqlDB
- ubuntu 13 sogou input method install