Hadoop: Add third-party libraries to MapReduce job
来自:http://hadoopi.wordpress.com/2014/06/05/hadoop-add-third-party-libraries-to-mapreduce-job/
Anybody working with Hadoop should have already faced a same common issue: How to add third-party libraries to your MapReduce job.
Add libjars option
The first solution, maybe the most common one, consists on adding libraries using -libjars parameter on CLI. To make it work, your class MyClass must useGenericOptionsParser class. Easiest way is to implement the Hadoop Tool interface as described in post Hadoop: Implementing the Tool interface for MapReduce driver.
$ export LIBJARS=/path/jar1,/path/jar2
$ hadoop jar /path/to/my.jar com.wordpress.hadoopi.MyClass -libjars ${LIBJARS} value
This will obviously work only when playing with CLI, so how the heck can we add such external jar files when not using CLI ?
Add jar files to Hadoop classpath
You could certainly upload external jar files to each tasktracker and update HADOOOP_CLASSPATH accordingly, but are you really willing to bother Ops team each time you need to add a new jar ? Works well on a single server node, but are you going to upload such jar across all of the 10, 100 or even more Hadoop nodes ? This approach does not scale at all !
Create a fat jar
Another approach is to create a fat jar, which is a JAR that contains your classes as well as your third-party classes (see this Cloudera blog post for more details). Be aware this Jar will not only contain your classes, but might also include all your project dependencies (such as Hadoop libraries) unless you explicitly exclude them (using provided tag).
Here is an example of maven plugin you will need to set up
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Following a “mvn clean package” command, your fat JAR will be located in maven project’s target directory as follows
drwxr-xr-x 2 antoine staff 68 Jun 10 09:30 archive-tmp
drwxr-xr-x 3 antoine staff 102 Jun 10 09:29 classes
drwxr-xr-x 3 antoine staff 102 Jun 10 09:29 generated-sources
drwxr-xr-x 3 antoine staff 102 Jun 10 09:29 generated-test-sources
drwxr-xr-x 3 antoine staff 102 Jun 10 09:29 maven-archiver
drwxr-xr-x 4 antoine staff 136 Jun 10 09:29 myproject-1.0-SNAPSHOT
-rw-r--r-- 1 antoine staff 63880020 Jun 10 09:30 myproject-1.0-SNAPSHOT-jar-with-dependencies.jar
drwxr-xr-x 4 antoine staff 136 Jun 10 09:29 surefire-reports
drwxr-xr-x 4 antoine staff 136 Jun 10 09:29 test-classes
In above example, note the actual size of your JAR file (61MB). Quite fat, isn’t it ?
You can ensure all dependencies have been added by firing up below command
$ jar -tf myproject-1.0-SNAPSHOT-jar-with-dependencies.jar META-INF/
META-INF/MANIFEST.MF
com/aamend/hadoop/allMyClasses.class
...
com/others/allMyDependencies.class
...
Use Distributed cache
I am always following such approach when using third-party libraries in my MapReduce jobs. One would say such approach is not elegant, but I can work without annoying anyone from Ops team :). I first create a directory “lib” in my HDFS home directory (“/user/hadoopi/”). You could even use “/tmp”, it does not matter. I then create a static method that
- Locate the jar file that includes the class I need
- Upload this jar to Hadoop HDFS
- Add the uploaded jar file to Hadoop distributed cache
Simply add the following lines to some Utils class.
private static void addJarToDistributedCache(
Class classToAdd, Configuration conf)
throws IOException { // Retrieve jar file for class2Add
String jar = classToAdd.getProtectionDomain().
getCodeSource().getLocation().
getPath();
File jarFile = new File(jar); // Declare new HDFS location
Path hdfsJar = new Path("/user/hadoopi/lib/"
+ jarFile.getName()); // Mount HDFS
FileSystem hdfs = FileSystem.get(conf); // Copy (override) jar file to HDFS
hdfs.copyFromLocalFile(false, true,
new Path(jar), hdfsJar); // Add jar to distributed classPath
DistributedCache.addFileToClassPath(hdfsJar, conf);
}
The only thing you need to remember is to add this class prior to Job submission…
public static void main(String[] args) throws Exception { // Create Hadoop configuration
Configuration conf = new Configuration(); // Add 3rd-party libraries
addJarToDistributedCache(MyFirstClass.class, conf);
addJarToDistributedCache(MySecondClass.class, conf); // Create my job
Job job = new Job(conf, "Hadoop-classpath");
.../...
}
Here you are, your MapReduce is now able to use any external JAR file.
Hadoop: Add third-party libraries to MapReduce job的更多相关文章
- Hadoop:使用Mrjob框架编写MapReduce
Mrjob简介 Mrjob是一个编写MapReduce任务的开源Python框架,它实际上对Hadoop Streaming的命令行进行了封装,因此接粗不到Hadoop的数据流命令行,使我们可以更轻松 ...
- 【Cloud Computing】Hadoop环境安装、基本命令及MapReduce字数统计程序
[Cloud Computing]Hadoop环境安装.基本命令及MapReduce字数统计程序 1.虚拟机准备 1.1 模板机器配置 1.1.1 主机配置 IP地址:在学校校园网Wifi下连接下 V ...
- 十九、Hadoop学记笔记————Hbase和MapReduce
概要: hadoop和hbase导入环境变量: 要运行Hbase中自带的MapReduce程序,需要运行如下指令,可在官网中找到: 如果遇到如下问题,则说明Hadoop的MapReduce没有权限访问 ...
- hadoop源码分析(2):Map-Reduce的过程解析
一.客户端 Map-Reduce的过程首先是由客户端提交一个任务开始的. 提交任务主要是通过JobClient.runJob(JobConf)静态函数实现的: public static Runnin ...
- Hadoop学习之旅三:MapReduce
MapReduce编程模型 在Google的一篇重要的论文MapReduce: Simplified Data Processing on Large Clusters中提到,Google公司有大量的 ...
- Hadoop:使用原生python编写MapReduce
功能实现 功能:统计文本文件中所有单词出现的频率功能. 下面是要统计的文本文件 [/root/hadooptest/input.txt] foo foo quux labs foo bar quux ...
- Hadoop学习记录(4)|MapReduce原理|API操作使用
MapReduce概念 MapReduce是一种分布式计算模型,由谷歌提出,主要用于搜索领域,解决海量数据计算问题. MR由两个阶段组成:Map和Reduce,用户只需要实现map()和reduce( ...
- Hadoop 学习笔记 (十一) MapReduce 求平均成绩
china:张三 78李四 89王五 96赵六 67english张三 80李四 82王五 84赵六 86math张三 88李四 99王五 66赵六 77 import java.io.IOEx ...
- Hadoop 学习笔记 (十) MapReduce实现排序 全局变量
一些疑问:1 全排序的话,最后的应该sortJob.setNumReduceTasks(1);2 如果多个reduce task都去修改 一个静态的 IntWritable ,IntWritable会 ...
随机推荐
- Kettle优化就这么多
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/ClamReason/article/details/49930479 Kettle正常转换速度 场景 ...
- JavaScript:动态代理之远程代理
背景 2008 第一次使用 AJAX 的时候好像使用的是 AJAX.NET,该组件支持为服务器 C# 类型提供 JS 代理,当时不是十分的明白.设计模式中有代理模式,不过真正需要我们手工写代理的次数却 ...
- BaseControl按钮合集
BaseControl按钮合集 效果 源码 https://github.com/YouXianMing/Animations // // POPBaseControl.h // Animations ...
- PL/SQL 存储过程
PL/SQL复习九 存储过程 无参数的存储过程: create or replace procedure out_time is begin dbms_output.put_line(to_char( ...
- 暗影猎人第一二季/全集Shadowhunters迅雷下载
<暗影猎人>该剧根据小说<致命契约>(The Mortal Instruments)改编,是电影<圣杯神器:骸骨之城>的续篇.故事描述少女Clary Fray(Ka ...
- git clone报错:“server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none”
I can push by clone project using ssh, but it doesn't work when I clone project with https. it shows ...
- Iocomp控件教程之Analog Display—模拟显示控件(优于EDIT控件)
Analog Display是简洁的显示控件.用于显示指定准确度和单位的模拟值(实数),能够将准确度设置为0.使显示结果为整数. 第一步:建立MFC对话框 第二步:插入AnalogDisplay控件 ...
- IIS7.5中调试.Net 4.0网站出现无厘头500错误的解决办法 (转)
刚刚 部署了ii7的dll的有x86写的,就会出现以下这样的问题 iis 7 x86,Could not load file or assembly 'Name' or one of its depe ...
- 从 SVM 到多核学习 MKL
SVM是机器学习里面最强大最好用的工具之一,它试图在特征空间里寻找一个超平面,以最小的错分率把正负样本分开.它的强大之处还在于,当样本在原特征空间中线性不可分,即找不到一个足够好的超平面时,可以利用核 ...
- CSS-设置Footer始终在页面底部
Footer顾名思义页脚,如果内容多的时候在底部时感官很好,但是当内容变少(无法撑开一屏的时候)footer不固定在底部,影响美观,对于已经从事前端工作的工作的来说应该是比价工作中入门级别的问题了,由 ...