Spring Batch 应用实例

[url]http://www.muxuanli.com/lmx/[/url]

应用场景:统计每日会员注册走势【MongoDB-->MySQL】

从MongoDB读取注册用户的信息,处理,写入MySQL对应表中,用于统计注册的情况!!!

MySQL表 t_member_aggregation


CREATE TABLE `t_member_aggregation` (

`year` INT(11) NOT NULL COMMENT '年份',

`month` INT(11) NOT NULL COMMENT '月份',

`day` INT(11) NOT NULL COMMENT '日期',

`amount` BIGINT(20) NULL COMMENT '关注人数',

`aggregation_date` DATETIME NOT NULL,

PRIMARY KEY (`year`,`month`,`day`))

ENGINE = InnoDB

DEFAULT CHARSET = utf8;


字段: year month day amount aggregation_date

year month day 做主键, 同一天注册的 amount+1, aggregation_date纪录上一次统计的时间,每次统计的时间为 aggregation_date 到 系统当前时间。


INSERT INTO `t_member_aggregation` (`year`, `month`, `day`, `amount`, `aggregation_date`) VALUES (?,?,?,?,now()) ON DUPLICATE KEY UPDATE `amount` = `amount` + 1, `aggregation_date` = now();



<!-- Member Data : from Mongo to MySQL 关注用户统计 -->
<bean id="memberQuery" class="com.newjetsoft.genen.batch.util.QueryFactory">
<property name="tableName" value="t_member_aggregation"></property>
<property name="jdbcTemplateAggregation" ref="jdbcTemplateAggregation"></property>
</bean>
<bean id="memberInfoResultItemReader"
class="org.springframework.batch.item.data.MongoItemReader"
scope="step">
<property name="template" ref="mongoUicTemplate"/>
<property name="collection" value="Member"/>
<property name="targetType" value="com.newjetsoft.genen.batch.aggregation.member.MemberInfoResult" />
<property name="query" ref="memberQuery"/>
<property name="sort">
<util:map id="sort">
<entry key="id" value="ASC"/>
</util:map>
</property>
</bean>

<bean id="memberInfoResultItemProcessor"
class="com.newjetsoft.genen.batch.aggregation.member.MemberInfoResultItemProcessor" />

<bean id="memberInfoResultItemWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter"
scope="step">
<property name="dataSource" ref="dataSourceAggregation"/>
<property name="sql">
<value>
<![CDATA[
INSERT INTO `t_member_aggregation` (`year`, `month`, `day`, `amount`, `aggregation_date`)
VALUES (?,?,?,?,now()) ON DUPLICATE KEY UPDATE `amount` = `amount` + 1, `aggregation_date` = now();
]]>
</value>
</property>

<property name="ItemPreparedStatementSetter">
<bean class="com.newjetsoft.genen.batch.aggregation.member.UpdateMemberInfoPreparedStatementSetter"/>
</property>
</bean>


链接:[url]https://pan.baidu.com/s/1gfLxV0n[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值