Use time variable wildcards in E-MapReduce (EMR) job parameters to dynamically inject date and time values at runtime — without modifying your scripts for each run.
How it works
When a job runs, EMR replaces each variable wildcard in the Configuration Parameters section with the computed time value. The wildcard expression defines the format and the arithmetic offset from the current time.
Syntax:
${dateexpr}
${dateexpr+Nd}
${dateexpr-Nd}
${dateexpr+Nh}
${dateexpr-Nh}Where dateexpr is a time format string built from the specifiers in the table below.
Limitations
Before writing expressions, note these constraints:
Expressions are case-sensitive.
MM(month) andmm(minute) produce different results.Time variables must start with
yyyy. For example,${yyyy-MM}is valid;${MM-dd}is not.Only days (
d) and hours (h) are supported for arithmetic offsets.Nmust be an integer. To work with other time units, useparseDate()andformatDate()instead.
Format specifiers
Build a dateexpr by combining yyyy with one or more of the following specifiers:
| Specifier | Description | Example value |
|---|---|---|
yyyy | 4-digit year | 2016 |
MM | 2-digit month | 04 |
dd | 2-digit day | 27 |
HH | 2-digit hour, 24-hour clock | 12 |
hh | 2-digit hour, 12-hour clock | 12 |
mm | 2-digit minute | 08 |
ss | 2-digit second | 01 |
Arithmetic expressions
The following examples use a reference time of 20160427 12:08:01.
| Expression | Result | Description |
|---|---|---|
${yyyyMMdd} | 20160427 | Current date |
${yyyy-MM-dd} | 2016-04-27 | Current date with separators |
${yyyyMMdd-1d} | 20160426 | Yesterday |
${yyyyMMdd HH:mm:ss-1d} | 20160426 12:08:01 | 24 hours ago, accurate to the second |
${yyyyMMdd-5d} | 20160422 | Five days ago |
Use parseDate and formatDate for other time units
Because arithmetic offsets are limited to days and hours, use parseDate() and formatDate() when you need to extract specific components or work with other time units.
parseDate(<parameter name>, <time format>) — converts the string value of a Configuration Parameters key into a Date object.
<parameter name>: the key name defined in Configuration Parameters (for example,current_time)<time format>: the format string that matches the key's value (for example,yyyyMMddHHmmss)
formatDate(<Date object>, <time format>) — converts a Date object into a formatted string.
Combine the two functions to extract specific time components:
| Expression | Result |
|---|---|
${formatDate(parseDate(current_time, 'yyyyMMddHHmmss'), 'HH')} | Hour value from current_time |
${formatDate(parseDate(current_time, 'yyyyMMddHHmmss'), 'yyyy')} | Year value from current_time |
For example, if current_time is defined as ${yyyyMMddHHmmss-1d}, pass yyyyMMddHHmmss as the format to parseDate().
Configure a variable wildcard
Log on to the EMR console.
In the top navigation bar, select the region where your cluster resides and select a resource group.
Click the Data Platform tab.
In the Projects section, find your project and click Edit Job in the Actions column.
In the Edit Job pane on the left, click the job name, then click Job Settings in the upper-right corner.
On the Basic Settings tab, go to the Configuration Parameters section and click
to add a parameter.Enter a key name and set the value to a variable wildcard expression, such as
${yyyyMMdd-1d}.
Reference the key in your job script using the same key name you defined in step 7.