Spring Batch application example

http://www.muxuanli.com/lmx/

Application scenario: Statistics of daily membership registration trends [MongoDB-->MySQL]

Read registered user information from MongoDB, process it, and write it into the MySQL corresponding table for statistical registration The case!!!

MySQL table t_member_aggregation

CREATE TABLE `t_member_aggregation` (

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

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

`day` INT(11) NOT NULL COMMENT 'date',

`amount` BIGINT(20) NULL COMMENT 'Number of followers',

`aggregation_date` DATETIME NOT NULL,

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

ENGINE = InnoDB

DEFAULT CHARSET = utf8;


Field: year month day amount aggregation_date

year month day as the primary key, the amount registered on the same day + 1, aggregation_date records the time of the last statistics, the time of each statistics is from aggregation_date to the current system time.

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 Follow user statistics-->
<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>


Link: https://pan.baidu.com/s/1gfLxV0n

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485775&siteId=291194637