migrate_to_java_time.asciidoc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. [[migrate-to-java-time]]
  2. === Java time migration guide
  3. With 7.0, {es} switched from joda time to java time for date-related parsing,
  4. formatting, and calculations. This guide is designed to help you determine
  5. if your cluster is impacted and, if so, prepare for the upgrade.
  6. [discrete]
  7. [[java-time-convert-date-formats]]
  8. ==== Convert date formats
  9. To upgrade to {es} 8, you'll need to convert any joda-time date formats
  10. to their java-time equivalents.
  11. [discrete]
  12. [[java-time-migration-impacted-features]]
  13. === Impacted features
  14. The switch to java time only impacts custom <<date,`date`>> and
  15. <<date_nanos,`date_nanos`>> formats.
  16. These formats are commonly used in:
  17. * <<mapping,Index mappings>>
  18. * <<index-templates,Index templates>>
  19. * <<ingest,Ingest pipelines>>
  20. If you don't use custom date formats, you can skip the rest of this guide.
  21. Most custom date formats are compatible. However, several require
  22. an update.
  23. To see if your date format is impacted, use the <<migration-api-deprecation,deprecation info API>>
  24. or the {kibana-ref-all}/{prev-major-last}/upgrade-assistant.html[Kibana Upgrade Assistant].
  25. [discrete]
  26. [[java-time-migration-incompatible-date-formats]]
  27. === Incompatible date formats
  28. Custom date formats containing the following joda-time literals should be
  29. migrated.
  30. `Y` (Year of era)::
  31. +
  32. --
  33. Replace with `y`.
  34. *Example:*
  35. `YYYY-MM-dd` should become `yyyy-MM-dd`.
  36. In java time, `Y` is used for
  37. https://docs.oracle.com/javase/8/docs/api/java/time/temporal/WeekFields.html[week-based year].
  38. Using `Y` in place of `y` could result in off-by-one errors in year calculation.
  39. For pattern `YYYY-ww` and date `2019-01-01T00:00:00.000Z` will give `2019-01`
  40. For pattern `YYYY-ww` and date `2018-12-31T00:00:00.000Z` will give `2019-01` (counter-intuitive) because there is >4 days of that week in 2019
  41. --
  42. `y` (Year)::
  43. +
  44. --
  45. Replace with `u`.
  46. *Example:*
  47. `yyyy-MM-dd` should become `uuuu-MM-dd`.
  48. In java time, `y` is used for year of era. `u` can contain non-positive
  49. values while `y` cannot. `y` can also be associated with an era field.
  50. --
  51. `C` (Century of era)::
  52. +
  53. --
  54. Century of era is not supported in java time.
  55. There is no replacement. Instead, we recommend you preprocess your input.
  56. --
  57. `x` (Week year)::
  58. +
  59. --
  60. Replace with `Y`.
  61. In java time, `x` means https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html[zone-offset].
  62. [WARNING]
  63. ====
  64. Failure to properly convert `x` (Week year) to `Y` could result in data loss.
  65. ====
  66. --
  67. `Z` (Zone offset/id)::
  68. +
  69. --
  70. Replace with multiple `X`'s.
  71. `Z` has a similar meaning in java time. However, java time expects different
  72. numbers of literals to parse different forms.
  73. Consider migrating to `X`, which gives you more control over how time is parsed.
  74. For example, the joda-time format `YYYY-MM-dd'T'hh:mm:ssZZ` accepts the following dates:
  75. ```
  76. 2010-01-01T01:02:03Z
  77. 2010-01-01T01:02:03+01
  78. 2010-01-01T01:02:03+01:02
  79. 2010-01-01T01:02:03+01:02:03
  80. ```
  81. In java time, you cannot parse all these dates using a single format
  82. Instead, you must specify 3 separate formats:
  83. ```
  84. 2010-01-01T01:02:03Z
  85. 2010-01-01T01:02:03+01
  86. both parsed with yyyy-MM-dd'T'hh:mm:ssX
  87. 2010-01-01T01:02:03+01:02
  88. yyyy-MM-dd'T'hh:mm:ssXXX
  89. 2010-01-01T01:02:03+01:02:03
  90. yyyy-MM-dd'T'hh:mm:ssXXXXX
  91. ```
  92. The formats must then be delimited using `||`:
  93. [source,txt]
  94. --------------------------------------------------
  95. yyyy-MM-dd'T'hh:mm:ssX||yyyy-MM-dd'T'hh:mm:ssXXX||yyyy-MM-dd'T'hh:mm:ssXXXXX
  96. --------------------------------------------------
  97. The same applies if you expect your pattern to occur without a colon (`:`):
  98. For example, the `YYYY-MM-dd'T'hh:mm:ssZ` format accepts the following date forms:
  99. ```
  100. 2010-01-01T01:02:03Z
  101. 2010-01-01T01:02:03+01
  102. 2010-01-01T01:02:03+0102
  103. 2010-01-01T01:02:03+010203
  104. ```
  105. To accept all these forms in java time, you must use the `||` delimiter:
  106. [source,txt]
  107. --------------------------------------------------
  108. yyyy-MM-dd'T'hh:mm:ssX||yyyy-MM-dd'T'hh:mm:ssXX||yyyy-MM-dd'T'hh:mm:ssXXXX
  109. --------------------------------------------------
  110. --
  111. `d` (Day)::
  112. +
  113. --
  114. In java time, `d` is still interpreted as "day" but is less flexible.
  115. For example, the joda-time date format `YYYY-MM-dd` accepts `2010-01-01` or
  116. `2010-01-1`.
  117. In java time, you must use the `||` delimiter to provide specify each format:
  118. [source,txt]
  119. --------------------------------------------------
  120. yyyy-MM-dd||yyyy-MM-d
  121. --------------------------------------------------
  122. In java time, `d` also does not accept more than 2 digits. To accept days with more
  123. than two digits, you must include a text literal in your java-time date format.
  124. For example, to parse `2010-01-00001`, you must use the following java-time date format:
  125. [source,txt]
  126. --------------------------------------------------
  127. yyyy-MM-'000'dd
  128. --------------------------------------------------
  129. --
  130. `e` (Name of day)::
  131. +
  132. --
  133. In java time, `e` is still interpreted as "name of day" but does not parse
  134. short- or full-text forms.
  135. For example, the joda-time date format `EEE YYYY-MM` accepts both
  136. `Wed 2020-01` and `Wednesday 2020-01`.
  137. To accept both of these dates in java time, you must specify each format using
  138. the `||` delimiter:
  139. [source,txt]
  140. --------------------------------------------------
  141. cccc yyyy-MM||ccc yyyy-MM
  142. --------------------------------------------------
  143. The joda-time literal `E` is interpreted as "day of week."
  144. The java-time literal `c` is interpreted as "localized day of week."
  145. `E` does not accept full-text day formats, such as `Wednesday`.
  146. --
  147. `EEEE` and similar text forms::
  148. +
  149. --
  150. Support for full-text forms depends on the locale data provided with your Java
  151. Development Kit (JDK) and other implementation details. We recommend you
  152. test formats containing these patterns carefully before upgrading.
  153. --
  154. `z` (Time zone text)::
  155. +
  156. --
  157. In java time, `z` outputs 'Z' for Zulu when given a UTC timezone.
  158. --
  159. [discrete]
  160. [[java-time-migration-test]]
  161. === Test with your data
  162. We strongly recommend you test any date format changes using real data before
  163. deploying in production.
  164. [discrete]
  165. [[java-time-migrate-update-mappings]]
  166. === Update index mappings
  167. To update joda-time date formats in index mappings, you must create a new index
  168. with an updated mapping and reindex your data to it.
  169. The following `my-index-000001` index contains a mapping for the `datetime` field, a
  170. `date` field with a custom joda-time date format.
  171. ////
  172. [source,console]
  173. --------------------------------------------------
  174. PUT my-index-000001
  175. {
  176. "mappings": {
  177. "properties": {
  178. "datetime": {
  179. "type": "date",
  180. "format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
  181. }
  182. }
  183. }
  184. }
  185. --------------------------------------------------
  186. ////
  187. [source,console]
  188. --------------------------------------------------
  189. GET my-index-000001/_mapping
  190. --------------------------------------------------
  191. // TEST[continued]
  192. [source,console-result]
  193. --------------------------------------------------
  194. {
  195. "my-index-000001" : {
  196. "mappings" : {
  197. "properties" : {
  198. "datetime": {
  199. "type": "date",
  200. "format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
  201. }
  202. }
  203. }
  204. }
  205. }
  206. --------------------------------------------------
  207. To change the date format for the `datetime` field, create a separate index
  208. containing an updated mapping and date format.
  209. For example, the following `my-index-000002` index changes the `datetime` field's
  210. date format to `uuuu/MM/dd HH:mm:ss||uuuu/MM/dd||epoch_millis`.
  211. [source,console]
  212. --------------------------------------------------
  213. PUT my-index-000002
  214. {
  215. "mappings": {
  216. "properties": {
  217. "datetime": {
  218. "type": "date",
  219. "format": "uuuu/MM/dd HH:mm:ss||uuuu/MM/dd||epoch_millis"
  220. }
  221. }
  222. }
  223. }
  224. --------------------------------------------------
  225. // TEST[continued]
  226. Next, reindex data from the old index to the new index.
  227. The following <<docs-reindex,reindex>> API request reindexes data from
  228. `my-index-000001` to `my-index-000002`.
  229. [source,console]
  230. --------------------------------------------------
  231. POST _reindex
  232. {
  233. "source": {
  234. "index": "my-index-000001"
  235. },
  236. "dest": {
  237. "index": "my-index-000002"
  238. }
  239. }
  240. --------------------------------------------------
  241. // TEST[continued]
  242. If you use index aliases, update them to point to the new index.
  243. [source,console]
  244. --------------------------------------------------
  245. POST /_aliases
  246. {
  247. "actions" : [
  248. { "remove" : { "index" : "my-index-000001", "alias" : "my-index" } },
  249. { "add" : { "index" : "my-index-000002", "alias" : "my-index" } }
  250. ]
  251. }
  252. --------------------------------------------------
  253. // TEST[continued]