Hadoop with tool interface
Often Hadoop jobsare executed through a command line. Therefore, each Hadoop job has to
support reading, parsing, and processing command-line arguments. To avoid each developer
having to rewrite this code, Hadoop provides a org.apache.hadoop.util.Toolinterface.
Sample code :
public class WordcountWithTools extends Configured implements Tool {
public int run(String[] args) throws Exception {
if (args.length < 2) {
System.out
.println("chapter3.WordCountWithTools WordCount <inDir> <outDir>");
ToolRunner.printGenericCommandUsage(System.out);
System.out.println("");
return -1;
}
System.out.println(Arrays.toString(args));
// just for test
System.out.println(getConf().get("test"));
Job job = new Job(getConf(), "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
// Uncomment this to
// job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
// delete target if exists
FileSystem.get(getConf()).delete(new Path(args[1]), true);
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
return 0;
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new WordcountWithTools(),
args);
System.exit(res);
}
}
Generic options supported are
-conf<configuration file> specify an application configuration
file
-D <property=value> use value for given property
-fs<local|namenode:port> specify a namenode
-jt<local|jobtracker:port> specify a job tracker
-files<comma separated list of files> specify comma separated
files to be copied to the map reduce cluster
-libjars<comma separated list of jars> specify comma separated
jar files to include in the classpath.
-archives<comma separated list of archives> specify comma
separated archives to be unarchived on the compute machines.
The general command line syntax is
bin/hadoop command [genericOptions] [commandOptions]
这里一定要注意顺序,我曾经用错过顺序,把-input -output放在前面,后面使用-D,-libjars不起作用。
使用示例:
JAR_NAME=/home/hadoop/workspace/myhadoop/target/myhadoop-0.0.1-SNAPSHOT.jar
MAIN_CLASS=chapter3.WordcountWithTools
INPUT_DIR=/data/input/
OUTPUT_DIR=/data/output/
hadoop jar $JAR_NAME $MAIN_CLASS -Dtest=lovejava $INPUT_DIR $OUTPUT_DIR
在代码中测试传递的test属性的值。
JAR_NAME=/home/hadoop/workspace/myhadoop/target/myhadoop-0.0.1-SNAPSHOT.jar
MAIN_CLASS=chapter3.WordcountWithTools
INPUT_DIR=/home/hadoop/data/test1.txt
OUTPUT_DIR=/home/hadoop/data/output/
hadoop jar $JAR_NAME $MAIN_CLASS -Dtest=lovejava -fs=file:/// -files=home/hadoop/data/test2.txt
$INPUT_DIR $OUTPUT_DIR
测试处理本地文件系统的文件。
JAR_NAME=/home/hadoop/workspace/myhadoop/target/myhadoop-0.0.1-SNAPSHOT.jar
MAIN_CLASS=chapter3.WordcountWithTools
INPUT_DIR=/home/hadoop/data/test1.txt
OUTPUT_DIR=/home/hadoop/data/output/
hadoop jar $JAR_NAME $MAIN_CLASS -conf=/home/hadoop/data/democonf.xml -fs=file:/// $INPUT_DIR $OUTPUT_DIR
指定配置文件。
-libjars可以把你写的mapreduce中引用的第三方包放到HDFS上,然后各结点在运行作业的时候复制到本地临时目录,以避免找不到引用类的情况。
Hadoop with tool interface的更多相关文章
- Hadoop 学习笔记3 Develping MapReduce
小笔记: Mavon是一种项目管理工具,通过xml配置来设置项目信息. Mavon POM(project of model). Steps: 1. set up and configure the ...
- hadoop MapReduce 笔记
1. MapReduce程序开发步骤 编写map 和 reduce 程序–> 单元测试 -> 编写驱动程序进行验证-> 本地数据集调试 -> 部署到集群运行 用 ...
- Hadoop MapReduceV2(Yarn) 框架简介[转]
对于业界的大数据存储及分布式处理系统来说,Hadoop 是耳熟能详的卓越开源分布式文件存储及处理框架,对于 Hadoop 框架的介绍在此不再累述,读者可参考 Hadoop 官方简介.使用和学习过老 H ...
- (转)单机上配置hadoop
哈哈,几天连续收到百度两次电话,均是利好消息,于是乎不知不觉的自己的工作效率也提高了,几天折腾了好久终于在单机上配置好了hadoop,然后也成功的运行了一个用例,耶耶耶耶耶耶. 转自:http://w ...
- Hadoop中的辅助类ToolRunner和Configured的用法详解
在开始学习hadoop时,最痛苦的一件事就是难以理解所写程序的执行过程,让我们先来看这个实例,这个测试类ToolRunnerTest继承Configured的基础上实现了Tool接口,下面对其用到的基 ...
- Hadoop伪分布配置与基于Eclipse开发环境搭建
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 远程调试hadoop各组件
远程调试对应用程序开发十分有用.例如,为不能托管开发平台的低端机器开发程序,或在专用的机器上(比如服务不能中断的 Web 服务器)调试程序.其他情况包括:运行在内存小或 CUP 性能低的设备上的 Ja ...
- Hadoop MapReduceV2(Yarn) 框架简介
http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ 对于业界的大数据存储及分布式处理系统来说,Hadoop 是耳熟能详 ...
- hadoop2.2编程:Tool, ToolRunner, GenericOptionsParser, Configuration
继承关系: 1. java.util Interface Map.Entry<K,V> description: public static interface Map.Entry&l ...
随机推荐
- 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能
[源码下载] 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能 作者:webabcd 介绍与众不同 windows ...
- HTML5实现屏幕手势解锁(转载)
来源:https://github.com/lvming6816077/H5lockhttp://threejs.org/examples/http://www.inf.usi.ch/phd/wett ...
- 基于Eclipse的Go语言可视化开发环境
http://jingyan.baidu.com/article/d7130635032e2f13fdf475b8.html 基于Eclipse的Go语言可视化开发环境 | 浏览:2924 | 更新: ...
- 配置了<mvc:resources> 导致以前的controller 无法访问。
解决方案: <mvc:annotation-driven/>
- innerHTML和outerHTML有什么区别
一.区别:1)innerHTML: 从对象的起始位置到终止位置的全部内容,不包括Html标签.2)outerHTML: 除了包含innerHTML的全部内容外, 还包含对象标签本身. 二.例子1: & ...
- XML的介绍
XML是指可扩展标记语言(eXtensible Markup Language),它是一种标记语言,类似于HTML.它被设计的宗旨是传输数据,而非显示数据. XML标签没有被预定义,需要开发者自定 ...
- Microsoft Dynamics CRM 2013 CD-KEY
Microsoft Dynamics CRM Workgroup Server 2013 (5 CAL limit):NX77Y-BTBCV-JP3T3-8W7JH-94QJP Microsoft D ...
- 【转】提高C#编程水平的50个要点
1.总是用属性 (Property) 来代替可访问的数据成员2.在 readonly 和 const 之间,优先使用 readonly3.在 as 和 强制类型转换之间,优先使用 as 操作符4.使用 ...
- 终端环境之tmux
今天继续介绍我的终端环境,tmux. why tmux? 用一个工具的第一问自然还是为什么要用.其实当时使用tmux的原因很简单.工作中经常需要长时间的编译.总想要下班后要关机的情况下,(肯定有人问我 ...
- sql动态insert向varchar(MAX)中写入据的问题
sql动态insert向varchar(MAX)中写入据的问题,在写入时出现列无效.后来发现,varchar要加''两个,号才可以 SET @SQL='INSERT INTO '+@TabName+' ...