I. 简介
<!--[if !supportLists]-->1. <!--[endif]-->环境需求
Spring for Apache Hadoop 2.1使用JDK 7、Spring Framework 4.1编译测试,对应的Hadoop版本是2.6。
• Spring for Apache Hadoop 2.1支持如下版本的发布版:
• Apache Hadoop 2.4.1
• Apache Hadoop 2.5.2
• Apache Hadoop 2.6.0
• Pivotal HD 2.1
• Cloudera CDH 5(2.5.0-cdh5.3.0)
• Hortonworks Data Platform 2.2
任何与Apache Hadoop 2.2.x或者后续版本兼容的发布版都是可用的。
Spring for Apache Hadoop 2.1 针对很多发布版本进行常规测试。详情参见测试计划页面(https://build.spring.io/browse/SPRINGDATAHADOOP)。
使用各种支持的发布版建立项目的介绍在Spring for Apache Hadoop wiki 提供( https://github.com/spring-projects/spring-hadoop/wiki)。(译者注:维基提供的repository没有包含Spring for CDH的版本,如果发现可以提醒译者谢谢,QQ:289705018)
关于Hadoop相关的项目,如SDHP支持HBase 0.94.11,Hive 0.11.0和Pig 0.11.0以及后续版本。当使用Hadoop相关项目时例如Hive或者Pig,使用某个需要的Hadoop版本作为基础然后寻找这个基础版本支持的相关项目的版本是一个首要原则。
要发挥Spring for Hadoop所有的作用需要运行一个Hadoop集群。如果你还没有集群环境,第一步最好建立一个单节点集群。安装最近的Hadoop稳定版,Apache项目官方网站的“Getting started”(http://hadoop.apache.org/common/docs/stable/#Getting+Started)页面一般来说是个比较好的指导。那里有单节点集群搭建的内容。
还建议下载一个虚拟机供Hadoop安装和运行。Cloudera,Hortonworks和Pivotal的产品页面都提供了虚拟机和镜像的下载。
<!--[if !supportLists]-->2. <!--[endif]-->其他资源
这个文档作为Spring for Hadoop项目的一个参考,还有其他的一些可选资源提供额外的背景知识和示例代码作为这个文档的补充供读者进行实验和体验。
• Spring for Apache Hadoop 示例。官方知识库有很多SHDP示例演示不同项目的特性。
• 《Spring Data Book》。作为Spring Data的使用指南由项目的提交者编写。不但包含了Spring Data Hadoop的介绍,又有与兄弟项目串联的部分。
• 《Spring Data Book》示例。为《Spring Data Book》编写的完全可运行的代码。其中的一些在Spring for Apache Hadoop的示例中也是可用的。
II. Spring 和 Hadoop
本部分介绍Spring for Apache Hadoop提供给基于Spring的应用的核心功能。
<!--[if !supportLists]-->3. <!--[endif]-->Hadoop配置
描述了Spring支持的Hadoop的通用属性。
使用Hadoop时一个主要任务是与runtime集成--不管是本地模式还是远程集群,正确配置和引导Hadoop程序以便提交job。本章的重点在于如何利用Spring for Apache Hadoop(SHDP)与Spring的轻量级IoC容器,简化与Hadoop的集成以及部署、测试,并便于服务的开通和更具管理的便捷性。
3.1. 使用Spring for Hadoop的命名空间
为了简化配置,SHDP为几乎所有的组件提供了预定义的命名空间。当然也可以选择使用<bean>标签直接定义自己的bean。更多基于XML模式配置Spring的信息在Spring Framework的参考文档中可见(http://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/html/xsd-config.html)。
为了只用SHDP的命名空间,只需将其在配置中引入即可。
<?xml version=”1.0” encoding=”UTF-8”?> <beans:beans xmlns=”http://www.springframework.org.schema/beans” Xmlns:xsi=”http://www.23.org/2001/XMLSchema-instance” Xmlns:hdp=”http://www.springframework.org/schema/hadoop” ①② Xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schcema/hadoop/spring-hadoop.xsd”>③ <bean/> <hdp:configruation/>④ </beans> |
① Spring for Hadoop命名空间前缀。任何前缀名都是可以使用的但是在本文档中会一直使用hdp。
②命名空间的URI。
③命名空间URI的位置。即便位置指向外部的地址也是可行的,Spring会在把它当作包含在Spring for Hadoop类库一样本地解析这个schema。
④Hadoop命名空间生命的例子。注意前缀的使用。
命名空间一旦引入,就可以用前面预定义的前缀创建元素。值得注意的是,默认的命名空间可以被修改,例如从<beans>变为<hdp>。当配置主要由Hadoop的组件组成,想要免前缀进行元素声明时很有用。通过交换命名空间前缀定义的位置就可以实现。
<?xml version=”1.0” encoding=”UTF-8”?> <beans xmlns=”http://www.springframework.org/schema/hadoop”① Xmlns:xsi=”http://www.23.org/2001/XMLSchema-instance” xmlns:beans="http://www.springframework.org/schema/beans" ② Xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schcema/hadoop/spring-hadoop.xsd”> <beans:bean id .../>③ <configruation .../>④ </beans:beans> |
①这个XML文件默认的的命名空间声明指向Spring for Apache Hadoop的命名空间。
②beans命名空间前缀声明。
③使用<beans>命名空间声明Bean。注意前缀。
④使用<hdp>命名空间生命Bean。注意前缀没有了。
为了提高可读性,文档剩余的部分的XML示例都会在没有命名空间生命的情况下使用<hdp>命名空间。
3.2. 使用Spring for Hadoop的JavaConfig
注释配置通过SpringHadoopConfigurerAdapter进行工作,注释是松散的,力图使用与xml同样类型的dsl语言。
Import org.springframework.context.annotation.Configuration; Import org.springframework.data.hadoop.config.annotation.EnableHadoop; Import org.springframework.data.hadoop.config.annotation.SrpingHadoopConfigurerAdapter; Improt org.springframework.data.hadoop.config.annotation.builders.HadoopConfigConfigurer;
@Configuration @EnableHadoop Static class Config extends SpringHadoopConfigurerAdapter{ @Override public void configure(HadoopConfigConfigurer config) throws Exception{ config .fileSystemUri(“hdfs://localhost:8021”); } } |
@EnableHadoo注释被用来标注Spring @Configuration类作为Spring Hadoop配置的候补配置。
3.3. 配置Hadoop
为了使用Hadoop首先要设置它也就是创建一个Configuration对象。配置中包含了job tracker、输入、输出格式和map reduce作业所需的其他参数的信息。
最简单的情况下这个配置只有一行:
<hdp:configuration />
上面的声明定义了一个被命名的配置bean(确切地说是ConfigurationFactoryBean类型的工厂bean),默认情况下是hadoopConfiguration。这个生命按照惯例使用了默认命名,不需其他特别设置的情况下像主要组件自动连通一样对其他需要配置的元素进行简明扼要地设置。
当默认设置需要改变时,可以通过添加额外的配置文件来实现。
<hdp:configuration resources=”classpath:/custom-site.xml, classpath:/hq-site.xml” |
这个例子中,把两个Hadoop配置资源添加进来。
注意:这个配置利用Spring的 Resource abstraction定位文件。可以根据运行环境或者值定义的前缀使用多种查询模式,在这个例子中使用了classpath。
除指向配置资源外,也可以通过Java Properties修改Hadoop的配置。这在少数选项需要修改的时候十分有用:
<hdp:configuration> fs.defaultFS=hdfs://localhost:8020 hadoop.tmp.dir=/tmp/hadoop electric=sea </hdp:configuration> |
可以进一步通过非硬编码的方式定制配置项,这样配置项可以在当前环境中的运行时被替换,但又不会碰触到configuration对象。
注意:通常配置参数如fs.defaultFS,mapred.job.tracker和yarn.resourcemanager.address可以被属性标签file-system-uri,job-tracker-uri和rm-manager-uri分别代替。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:hdp="http://www.springframework.org/schema/hadoop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
<hdp:configuration>
fs.defaultFS=${hd.fs}
hadoop.tmp.dir=file://${java.io.tmpdir}
hangar=${number:18}
</hdp:configuration>
<context:property-placeholder location="classpath:hadoop.properties" />
</beans>
通过Spring属性placeholder的支持,SpEL和环境抽象,可以从主代码外部化环境中特定的属性,在多个主机之间方便地部署应用。在上面的例子中,默认的文件系统被hadoop.properties中的值代替,同时临时文件夹是由SpEL动态计算得出的。两种实现都为适配运行环境提供了非常好的弹性空间-实际上我们在Spring for Apache Hadoop测试用例中广泛地使用这个实现以应对不同的开发盒子和CI服务产生的差异。
另外,外部的Porperties文件可以被加载,Properties beans通常由Spring的util命名空间来声明。随着嵌套的属性声明,定制的配置也可以方便地被声明。
<?xml version=”1.0” encoding=”UTF-8”?> <beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:hdp=http://www.springframework.org.schema/hadoop xmlns:context=http://www.springframework.org/schema/context xmlns:util=http://www.springframework.org/schema/util xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org.schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-util.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd”>
<!—merge the local properties, the props bean and the tow properties files--> <hdp:configuration properties-ref=”props” properties-location=”cfg-1.properties,cfg-2.properties”> star=chasing caption=eo </hdp:configuration> <util:properties id=”props” location=”props.properties”/> </beans> |
在上面的例子中配置的属性文件是主要的数据源,
3.4. Boot支持
如果在classpath中发现spring-data-hadoop-boot-2.1.2.RELEASE.jar,Spring 的Boot支持自动生效。目前Boot的自动装配有一些限制,只有支持hadoopConfiguration和fsShell的beans。
@Grab(‘org.springframework.data:spring-data-hadoop-boot:2.1.2.RElEASE’) Import org.springframework.data.hadoop.fs.FsShell Public class App implements CommandLineRunner{ @Autowried FsShell shell Void run(Strng… args){ shell.lsr(“/tmp”).each(){ print “> ${it.path}”} } } } |
上面的例子可以使用Spring Boot CLI运行,展示了自动装配如何简化了Spring Hadoop的使用。在这个例子中,Hadoop配置和FsShell都是自动装配的。
3.4.1. spring.hadoop属性
命名空间spring.hadoop支持如下属性:fsUri,resourceManagerAddress,resourceMnaagerSchedulerAddress,resourceManagerHost,resourceManagerPort,resourceManagerSchedulerPort,resources和config。
spring.hadoo.fsUri
描述: Namenode的Hdfs文件系统uri
是否必须:是
类型:String字符型
默认值:null
spring.hadoop.resourceManagerSchedulerAddress
描述:YARN资源管理调度器的地址
是否必须:否
类型:String
默认值:null
spring.hadoop.resourceManagerHost
描述:YARN资源管理主机名。
是否必须:否
类型:String
默认值:null
spring.hadoop.resourceManagerPort
描述:YARN资源管理器的端口
是否必须:否
类型:Integer
默认值:8032
spring.hadoop.resourceManagerSchedulerPort
描述:YARN资源管理器调度者的端口。只有master application才需要这个属性。
是否必须:否
类型:Integer
默认值:8030
spring.hadoop.resources
描述:在Hadoop中初始化的资源位置的列表。这些资源应该是Hadoop定义的site.xml格式的文件,并且位置可以被Spring支持的任意格式。例如,环境变量中的classpath:/myentry.xml,或者文件系统中的file:/myentry.xml。
是否必须:否
类型:List
默认值:null
spring.hadoop.config
描述:hadoop配置属性
下面的yml例子展示了如何使用config属性替代fsUri设置文件系统uri
application.yml.
spring: hadoop: config: fs.defaultFS: hdfs://localhsot:8020 或者 spring: hadoop: config: fs: defaultFS: hdfs://localhost:8020 |
下面的例子展示了使用属性实现同样的设置
spring.hadoop.config.fs.defaultFS=hdfs://localhost:8020 |
是否必须:否
类型:Map
默认值:null
3.4.2. spring.hadoop.fsshell 属性
命名空间spring.hadoop.fsshell支持如下的属性;enabled
spring.hadoop.fsshell.enabled
描述:定义FsShell是否被自动创建
是否必须:否
类型:Boolean
默认值:true
相关推荐
Spring Data for Apache Hadoop API。 Spring Data for Apache Hadoop 开发文档
SQL for Apache Hadoop, SQL for Apache Hadoop, SQL for Apache Hadoop, SQL for Apache Hadoop
spring-hadoop-入门Spring for Apache Hadoop 入门示例
Spring for Apache Hadoop 提供了 Spring 框架用于创建和运行 Hadoop MapReduce、Hive 和 Pig 作业的功能,包括 HDFS 和 HBase。如果你需要简单的基于 Hadoop 进行作业调度,你可添加 Spring for Apache Hadoop 命名...
Pro apache hadoop 原版书
spring data hadoop 参考文档
带图带说明:Hadoop简介及Apache Hadoop三种搭建方式
Apache Hadoop YARN.pdf完整电子版
Apache Hadoop is a widely used distributed data platform. It enables large datasets to be efficiently processed instead of using one large computer to store and process the data. This book will get ...
Pro Apache Hadoop, 2nd Edition是最新介绍Hadoop2.x的资料
apache hadoop v2.7.0官方最新版
Moving beyond MapReduce and Batch Processing with Apache Hadoop™ 2
藏经阁-A Container-based Sizing Framework for Apache Hadoop_Spark Clusters.pdf
Apache Ambari是一种基于Web的工具,支持Apache Hadoop集群的供应、管理和监控。Ambari已支持大多数Hadoop组件,包括HDFS、MapReduce、Hive、Pig、 Hbase、Zookeeper、Sqoop和Hcatalog等。但是这里的 Hadoop 是广义...
Apache Hadoop YARN: Moving beyond MapReduce and Batch Processing with Apache Hadoop 2 完整版哦,绝对清晰,不是扫描的mobi格式电子书,请使用电子书库calibre (http://calibre-ebook.com/download) 打开。
Apache Hadoop 软件库是一个框架,它允许使用简单的编程模型跨计算机集群分布式处理大型数据集。它旨在从单个服务器扩展到数千台机器,每台机器都提供本地计算和存储。库本身不是依靠硬件来提供高可用性,而是设计...
Apache Hadoop十周岁:展望前方.pdf
Apache Hadoop YARN is the modern distributed operating system for big data applications. It morphed the Hadoop compute layer to be a common resource-management platform that can host a wide variety of...
集成spring3-hadoop0.2.0 eclipse3.7快速开发 mapreduce程序!