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

[经验分享] spring hadoop之batch处理(二)

[复制链接]

尚未签到

发表于 2016-12-10 10:39:53 | 显示全部楼层 |阅读模式
一、测试
public class MrBatchApp {
    // Log
    private static final Log log = LogFactory.getLog(MrBatchApp.class);
    //
    public static void main(String[] args) throws      JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
    System.out.println("TEST");
    // 加载对应的xml配置文件
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/*-context.xml");
        log.info("Batch Tweet Hashtag MR Job Running");
        // 关闭"钩子" 为了方便在适当的时候关闭 spring ioc
        // (在非web环境下,关闭spring ioc需要手动完成)
        context.registerShutdownHook();
        // job 发射器
        // JobLaucher是一个简化的job的控制接口;基于运行时不同的标识
        // 该接口并不能确保执行job是同步还是异步
        JobLauncher jobLauncher = context.getBean(JobLauncher.class);
        // job
        Job job = context.getBean(Job.class);
        // 运行job
        jobLauncher.run(job, new JobParameters());

    }
}
二、xml配置文件
(1)、common 配置
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
// job 仓库
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/>
// 事务
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
// job launcher
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher" p:jobRepository-ref="jobRepository"/>
</beans>
(2)、特殊配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
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/batchhttp://www.springframework.org/schema/batch/spring-batch.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">
    // 引入common 配置
    <import resource="batch-common-context.xml"/>
    // hdfs uri/分析目录/统计目录/分析源文件
    <context:property-placeholder location="hadoop.properties"/>

<context:component-scan base-package="org.springframework.samples.hadoop.mapreduce" />
// 指定hdfs
<hdp:configuration>
        fs.defaultFS=${hd.fs}
</hdp:configuration>
    // 设定hdp 脚本 用于创建localSourceFile、inputDir、outputDir
    <hdp:script id="setupScript" location="file-prep.groovy" run-at-startup="true">
        <hdp:property name="localSourceFile" value="${localSourceFile}"/>
        <hdp:property name="inputDir" value="${tweets.input.path}"/>
        <hdp:property name="outputDir" value="${tweets.output.path}"/>
    </hdp:script>
    // 指定mapreduce step to step执行
    <!-- required since Hadoop Job is a class not an interface and we need to use a Job with step scope to access #{jobParameters['...']} -->
    <bean class="org.springframework.batch.core.scope.StepScope">
        <property name="proxyTargetClass" value="true"/>
    </bean>
    // 设置job steps
    <job id="job" xmlns="http://www.springframework.org/schema/batch">
        <step id="hashtagcount" next="result-step">
            <tasklet ref="hashtagcount-tasklet" />
        </step>
        <step id="result-step">
            <tasklet ref="results"/>
        </step>
    </job>
   
    <hdp:job-tasklet id="hashtagcount-tasklet" job-ref="hashtagcountJob" scope="step"/>

    <hdp:job id="hashtagcountJob"
        input-path="${tweets.input.path}"
        output-path="${tweets.output.path}"
        mapper="org.springframework.samples.hadoop.mapreduce.HashtagCount$TokenizerMapper"
        reducer="org.springframework.samples.hadoop.mapreduce.HashtagCount$LongSumReducer"
        scope="step" />
    // 指定统计结果 输出
    <hdp:script-tasklet id="results" scope="step">
        <hdp:script location="classpath:results.groovy">
            <hdp:property name="outputDir" value="${tweets.output.path}"/>
        </hdp:script>
    </hdp:script-tasklet>

</beans>
三、groovy脚本
// 判断分析源文件所在的目录是否存在 不存在创建 并将源文件复制到指定目录下
// 同时修改该文件夹的权限
if (!fsh.test(inputDir)) {
   fsh.mkdir(inputDir);
   fsh.copyFromLocal(localSourceFile, inputDir);
   fsh.chmod(700, inputDir)
}
// 判断统计结果目录是否存在 存在则删除
if (fsh.test(outputDir)) {
   fsh.rmr(outputDir)
}
-----------------------------------------------------------------------
// 输出分析统计结果的内容
println "RESULTS from " + outputDir
old = new File('results.txt')
if( old.exists() ) {
    old.delete()
}
fsh.get(outputDir + '/part-r-*', 'results.txt');
String fileContents = new File('results.txt').text
println fileContents
以上即可完全通过xml完成mapreduce的batch处理

运维网声明 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.yunweiku.com/thread-312287-1-1.html 上篇帖子: Hadoop学习—HDFS中的Snapshot和Checkpoint 下篇帖子: hadoop分析之四:关于hadoop namenode的双机热备份方案
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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