You can use the ${} macro variable definition identifier to define the macros of the time type. currently, dateformat and timestamp are supported.
By using the macro variables of date and time and scheduled job, you can implement incremental synchronization of databases and files.
dateformat supports two types of parameters:
format indicates the date and time format. For details about the format definition, see the definition in java.text.SimpleDateFormat.java.
For example, if the current date is 2017-10-16 09:00:00, yyyy-MM-dd HH:mm:ss indicates 2017-10-16 09:00:00.
Currently, dateType supports SECOND, MINUTE, HOUR, and DAY.
For example, if the current date is 2017-10-16 09:00:00, then:
timestamp supports two types of parameters:
Indicates the returned timestamp of the current time, that is, the number of milliseconds that have elapsed since 00:00:00 on January 1, 1970 (1970-01-01 00:00:00 GMT). For example, 1508078516286.
Indicates the timestamp returned after time offset. dateOffset and dateType indicate the date offset and the offset type, respectively.
For example, if the current date is 2017-10-16 09:00:00, timestamp(-10, MINUTE) indicates that the timestamp generated 10 minutes before the current time point is returned, that is, 1508115000000.
Suppose that the current time is 2017-10-16 09:00:00, then Table 1 describes the macro variable definitions of time and date.
Macro Variable |
Description |
Display Effect |
---|---|---|
${dateformat(yyyy-MM-dd)} |
Returns the current date in yyyy-MM-dd format. |
2017-10-16 |
${dateformat(yyyy/MM/dd)} |
Returns the current date in yyyy/MM/dd format. |
2017/10/16 |
${dateformat(yyyy_MM_dd HH:mm:ss)} |
Returns the current time in yyyy_MM_dd HH:mm:ss format. |
2017_10_16 09:00:00 |
${dateformat(yyyy-MM-dd HH:mm:ss, -1, DAY)} |
Returns the current time in yyyy-MM-dd HH:mm:ss format. The date is one day before the current day. |
2017-10-15 09:00:00 |
${timestamp()} |
Returns the timestamp of the current time, that is, the number of milliseconds that have elapsed since 00:00:00 on January 1, 1970. |
1508115600000 |
${timestamp(-10, MINUTE)} |
Returns the timestamp generated 10 minutes before the current time point. |
1508115000000 |
${timestamp(dateformat(yyyyMMdd))} |
Returns the timestamp of 00:00:00 of the current day. |
1508083200000 |
${timestamp(dateformat(yyyyMMdd,-1,DAY))} |
Returns the timestamp of 00:00:00 of the previous day. |
1507996800000 |
${timestamp(dateformat(yyyyMMddHH))} |
Returns the timestamp of the current hour. |
1508115600000 |
After the macro definition conversion, this job indicates that data in table SQOOP.CDM_20171016 in the Oracle database is migrated to the /opt/ttxx/1508115701746 directory of the HDFS server.
Currently, a table name or path name can contain multiple macro variables. For example, /opt/ttxx/${dateformat(yyyy-MM-dd)}/${timestamp()} is converted to /opt/ttxx/2017-10-16/1508115701746.
Figure 2 uses table SQOOP.CDM_20171016 as an example. The table contains column DS, which indicates the time.
Suppose that the current date is 2017-10-16 and you want to export data generated the day before the current day (DS = 2017-10-15), then you can set the value of Where Clause to DS='${dateformat(yyyy-MM-dd,-1,DAY)}' when creating a job. In this way, you can export all data that complies with the DS = 2017-10-15 condition.
Two simple application scenarios are as follows:
In a scheduled job, the cycle is one day, and the scheduled job is executed at 00:00:00 every day. Set the value of Where Clause to DS='${dateformat(yyyy-MM-dd,-1,DAY)}', and then data generated in the previous day will be exported at 00:00:00 every day.
In a scheduled job, the cycle is one day, and the scheduled job is executed at 00:00:00 every day. Set the value of Where Clause to time between ${timestamp(-1,DAY)} and ${timestamp()}, and then data generated on the previous day will be exported at 00:00:00 every day.
Configuration principles of other application scenarios are the same.