设为首页 收藏本站
查看: 2391|回复: 0

[经验分享] Spring for Apache Hadoop 的 Reference 一

[复制链接]

尚未签到

发表于 2016-12-6 11:28:10 | 显示全部楼层 |阅读模式
I. 简介
<!--[if !supportLists]-->1. <!--[endif]-->环境需求
Spring for Apache Hadoop 2.1使用JDK 7Spring 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 52.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的版本,如果发现可以提醒译者谢谢,QQ289705018
关于Hadoop相关的项目,如SDHP支持HBase 0.94.11Hive 0.11.0Pig 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安装和运行。ClouderaHortonworksPivotal的产品页面都提供了虚拟机和镜像的下载。
<!--[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 HadoopSHDP)与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 HadoopJavaConfig
注释配置通过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.trackeryarn.resourcemanager.address可以被属性标签file-system-uri,job-tracker-urirm-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通常由Springutil命名空间来声明。随着嵌套的属性声明,定制的配置也可以方便地被声明。
<?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.jarSpring Boot支持自动生效。目前Boot的自动装配有一些限制,只有支持hadoopConfigurationfsShellbeans
@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,resourcesconfig
spring.hadoo.fsUri
    描述: NamenodeHdfs文件系统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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-310514-1-1.html 上篇帖子: eclipse安装Hadoop 1.2.1 插件 下篇帖子: HADOOP的学习笔记
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表