编译hadoop版的hello,world
cd ~/src
mkdir classes
javac -classpath ~/hadoop-0.20./hadoop-0.20.-core.jar WordCount.java -d classes
jar -cvf WordCount.jar -C classes/ .
hadoop jar WordCount.jar com.codestyle.hadoop.WordCount input output
hadoop fs -ls output
hadoop fs -cat output/part-
要点:
编译WordCount.java时必须通过classpath指定hadoop的库文件。指定源码输出到classes目录
打包class文件成为jar文件
通过hadoop调用jar文件执行MapReduce, 内容输出到output目录 (如果该目录存在,则要先删掉这个目录)在命令参数中必须指定包名+类名
WordCount.java
package com.codestyle.hadoop; import java.io.IOException;
import java.util.*; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*; public class WordCount { public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text(); public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
} public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
} public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("wordcount"); conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class); conf.setMapperClass(Map.class);
conf.setReducerClass(Reduce.class); conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class); FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1])); JobClient.runJob(conf);
}
}
查看执行结果
lishujun@lishujun-virtual-machine:~/src$ hadoop fs -cat output/part-
Hadoop
Hello
World
参考资料:
http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html
http://blog.csdn.net/xw13106209/article/details/6862480
http://blog.csdn.net/turkeyzhou/article/details/8121601
编译hadoop版的hello,world的更多相关文章
- 在Ubuntu X64上编译Hadoop
在之前的文章中介绍了如何直接在Ubuntu中安装Hadoop.但是对于64位的Ubuntu来说,官方给出的Hadoop包是32位的,运行时会得到警告: WARN util.NativeCodeLoad ...
- mac OS X Yosemite 上编译hadoop 2.6.0/2.7.0及TEZ 0.5.2/0.7.0 注意事项
1.jdk 1.7问题 hadoop 2.7.0必须要求jdk 1.7.0,而oracle官网已经声明,jdk 1.7 以后不准备再提供更新了,所以趁现在还能下载,赶紧去down一个mac版吧 htt ...
- 64位centos 下编译 hadoop 2.6.0 源码
64位os下为啥要编译hadoop就不解释了,百度一下就能知道原因,下面是步骤: 前提:编译源码所在的机器,必须能上网,否则建议不要尝试了 一. 下载必要的组件 a) 下载hadoop源码 (当前最新 ...
- Windows 10 x64 下编译 Hadoop 源码
Windows 10 x64 下编译 Hadoop 源码 环境准备 Hadoop并没有提供官方的 Windows 10 下的安装包,所以需要自己手动来编译,官方文档中 BUILDING.txt 文件中 ...
- [转]编译hadoop
安装maven hadoop源码是使用maven组织管理的,必须下载maven.从maven官网下载,下载地址是http://maven.apache.org/download.cgi,选择 apac ...
- [b0013] Hadoop 版hello word mapreduce wordcount 运行(三)
目的: 不用任何IDE,直接在linux 下输入代码.调试执行 环境: Linux Ubuntu Hadoop 2.6.4 相关: [b0012] Hadoop 版hello word mapred ...
- 在docker容器中编译hadoop 3.1.0
在docker容器中编译hadoop 3.1.0 优点:docker安装好之后可以一键部署编译环境,不用担心各种库不兼容等问题,编译失败率低. Hadoop 3.1.0 的源代码目录下有一个 `sta ...
- 编译Hadoop
Apache Hadoop 生态圈软件下载地址:http://archive.apache.org/dist/hadoop/hadoop下载地址 http://archive.apache.org/d ...
- 编译hadoop遇到maven timeout
在编译hadoop的过程中,使用ant jar进行编译时,提示maven版本库连接超时的问题,通过搜索发现,在如下文件的位置中有repo2的版本库地址,这个地址在国内,目前不能正常的访问: 将 ...
随机推荐
- C# 自定义事件
C#自定义事件和java有所不同,涉及到委托.下面代码包括自定义事件从事件定义到事件触发和执行的全过程. using System; using System.Collections.Generic; ...
- 01 if
if - else if语句是一种控制语句,执行一代码块,如果一个表达式计算为true if (expression) statement1 else statement2 如 ...
- Cocos2d-x3.0TestCpp文件夹笔记(二)
3.Actions-Basic:此demo中体现ccp由Point取代 ①ActionManual:直接设置精灵的属性demo. const Color3B Color3B::RED (255, ...
- [Javascript] MetaProgramming: new.target
new.target is a new “magical” value available in all functions, thoughin normal functions it will al ...
- 传输层:TCP UDP SCTP
总图 虽然协议族被称为“TCP/IP”,但除了TCP和IP这两个主要协议外,还有许多其他成员.图2-1展示了这些协议的概况. 图2-1中同时展示了IPV4和IPV6.从右向左看该图,最右边的5个网络应 ...
- Exploring Message Brokers: RabbitMQ, Kafka, ActiveMQ, and Kestrel--reference
[This article was originally written by Yves Trudeau.] http://java.dzone.com/articles/exploring-mess ...
- 一次向svn中增加所有新增文件 svn add all new files
svn st | grep '^\?' | tr '^\?' ' ' | sed 's/[ ]*//' | sed 's/[ ]/\\ /g' | xargs svn add
- CocoaPods导入第三方库头文件自动补齐
使用了一段时间CocoaPods来管理Objective-c的类库,方便了不少.但是有一个小问题,当我在xcode输入import关键字的时候,没有自动联想补齐代码的功能,需要手工敲全了文件名,难以适 ...
- MySQL查询
DQL 操作 DQL 数据查询语言(重要) 数据库执行DQL语句不会对数据做出任何改变,而是让数据库发送结果集给客户端. 查询返回的结果是一张虚拟表. 查询关键字:SELECT ...
- jquery easyUI 日期格式化,DateBox只显示年
jquery easyUI 日期格式化,DateBox只显示年 >>>>>>>>>>>>>>>>> ...