Built-in Time Variable

Users hope that when writing code, the time format requirements are ever-changing, and the existing Linkis custom variables is currently not enough to support these requirements. In addition, some of the existing time operation -1 means minus one month, and some minus one day, which is easy for users to confuse

  • Other date built-in variables are calculated relative to run_date
  • Support Pattern format time and users can specify at will
  • Support ±y/±M/±d/±H etc.

Pattern format comparison table:

LetterDate or Time ComponentPresentationExamples
GEra designatorTextAD
yYearYear1996; 96
YWeek yearYear2009; 09
MMonth in yearMonthJuly; Jul; 07
wWeek in yearNumber27
WWeek in monthNumber2
DDay in yearNumber189
dDay in monthNumber10
FDay of week in monthNumber2
EDay name in weekTextTuesday; Tue
eDay number of week (1 = Monday, …, 7 = Sunday)Number1
aAm/pm markerTextPM
HHour in day (0-23)Number0
kHour in day (1-24)Number24
KHour in am/pm (0-11)Number0
hHour in am/pm (1-12)Number12
mMinute in hourNumber30
sSecond in minuteNumber55
SMillisecondNumber978
zTime zoneGeneral time zonePacific Standard Time; PST; GMT-08:00
ZTime zoneRFC 822 time zone-0800
XTime zoneISO 8601 time zone-08; -0800; -08:00

The overall design and technical architecture refer to Linkis Custom Variables

  • The variable types supported by Linkis are divided into custom variables (not to be repeated) and system built-in variables. The custom variable date supports +-.
  • Among them, +- is to perform operation on the built-in parameter run_date of linkis, and then replace the pattern field before %. Non-pattern characters do not support operation replacement.

You can define parameters that need to be dynamically rendered according to your own preferences/business actual situation

variableresult
&{yyyy-01-01}2021-01-01
&{yyyy-01-01%-2y}2019-01-01
&{yyyy-MM-01%-2M}2021-02-01
&{yyyy-MM-dd%-2d}2021-03-31
&{yyyy MM ——- HH%-1H}2021 04 ——- 14
&{yyyyMMdd%-1d}20210401
&{yyyyMM01%-1M}20210301
&{HH%-1H}14
  • Example 1: sql
  1. SELECT * FROM hive.tmp.fund_nav_histories
  2. WHERE dt <= DATE_FORMAT(DATE_ADD('day', -1, DATE(Date_parse('&{yyyyMMdd%-1d}', '%Y%m%d'))), '%Y%m%d')

after rendering

  1. SELECT * FROM hive.tmp.fund_nav_histories
  2. WHERE dt <= DATE_FORMAT(DATE_ADD('day', -1, DATE(Date_parse('20220705', '%Y%m%d'))), '%Y%m%d')
  • Example 2: shell
  1. aws s3 ls s3://***/ads/tmp/dws_member_active_detail_d_20210601_20211231/pt=&{yyyyMMdd%-1d}/

after rendering

  1. aws s3 ls s3://***/ads/tmp/dws_member_active_detail_d_20210601_20211231/pt=20220705/
  • Example 3: datax json
  1. {
  2. "job": {
  3. "setting": {
  4. "speed": {
  5. "channel": 1
  6. }
  7. },
  8. "content": [
  9. {
  10. "reader": {
  11. "name": "s3reader",
  12. "parameter": {
  13. "bucket": "****************",
  14. "path": [
  15. "ads/tmp/ccass_tm_announcements/&{yyyyMMdd%-1d}/"
  16. ],
  17. "stored": "parquet",
  18. "compression": "NONE",
  19. "column": [
  20. {
  21. "index": 0,
  22. "type": "int"
  23. },
  24. {
  25. "index": 1,
  26. "type": "string",
  27. "constant": "&{yyyyMMdd%-1d}"
  28. }
  29. ]
  30. }
  31. },
  32. "writer": {
  33. "name": "streamwriter",
  34. "parameter": {
  35. "print": true
  36. }
  37. }
  38. }
  39. ]
  40. }
  41. }

after rendering

  1. {
  2. "job": {
  3. "setting": {
  4. "speed": {
  5. "channel": 1
  6. }
  7. },
  8. "content": [
  9. {
  10. "reader": {
  11. "name": "s3reader",
  12. "parameter": {
  13. "bucket": "****************",
  14. "path": [
  15. "ads/tmp/ccass_tm_announcements/20220705/"
  16. ],
  17. "stored": "parquet",
  18. "compression": "NONE",
  19. "column": [
  20. {
  21. "index": 0,
  22. "type": "int"
  23. },
  24. {
  25. "index": 1,
  26. "type": "string",
  27. "constant": "20220705"
  28. }
  29. ]
  30. }
  31. },
  32. "writer": {
  33. "name": "streamwriter",
  34. "parameter": {
  35. "print": true
  36. }
  37. }
  38. }
  39. ]
  40. }
  41. }
  • Example 4: python
  1. print(&{yyyyMMdd%-1d})

after rendering

  1. 20220705