put-job.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. [[java-rest-high-x-pack-ml-put-job]]
  2. === Put Job API
  3. The Put Job API can be used to create a new {ml} job
  4. in the cluster. The API accepts a `PutJobRequest` object
  5. as a request and returns a `PutJobResponse`.
  6. [[java-rest-high-x-pack-ml-put-job-request]]
  7. ==== Put Job Request
  8. A `PutJobRequest` requires the following argument:
  9. ["source","java",subs="attributes,callouts,macros"]
  10. --------------------------------------------------
  11. include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-put-job-request]
  12. --------------------------------------------------
  13. <1> The configuration of the {ml} job to create as a `Job`
  14. [[java-rest-high-x-pack-ml-put-job-config]]
  15. ==== Job Configuration
  16. The `Job` object contains all the details about the {ml} job
  17. configuration.
  18. A `Job` requires the following arguments:
  19. ["source","java",subs="attributes,callouts,macros"]
  20. --------------------------------------------------
  21. include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-put-job-config]
  22. --------------------------------------------------
  23. <1> The job ID
  24. <2> An analysis configuration
  25. <3> A data description
  26. <4> Optionally, a human-readable description
  27. [[java-rest-high-x-pack-ml-put-job-analysis-config]]
  28. ==== Analysis Configuration
  29. The analysis configuration of the {ml} job is defined in the `AnalysisConfig`.
  30. `AnalysisConfig` reflects all the configuration
  31. settings that can be defined using the REST API.
  32. Using the REST API, we could define this analysis configuration:
  33. [source,js]
  34. --------------------------------------------------
  35. "analysis_config" : {
  36. "bucket_span" : "10m",
  37. "detectors" : [
  38. {
  39. "detector_description" : "Sum of total",
  40. "function" : "sum",
  41. "field_name" : "total"
  42. }
  43. ]
  44. }
  45. --------------------------------------------------
  46. // NOTCONSOLE
  47. Using the `AnalysisConfig` object and the high level REST client, the list
  48. of detectors must be built first.
  49. An example of building a `Detector` instance is as follows:
  50. ["source","java",subs="attributes,callouts,macros"]
  51. --------------------------------------------------
  52. include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-put-job-detector]
  53. --------------------------------------------------
  54. <1> The function to use
  55. <2> The field to apply the function to
  56. <3> Optionally, a human-readable description
  57. Then the same configuration would be:
  58. ["source","java",subs="attributes,callouts,macros"]
  59. --------------------------------------------------
  60. include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-put-job-analysis-config]
  61. --------------------------------------------------
  62. <1> Create a list of detectors
  63. <2> Pass the list of detectors to the analysis config builder constructor
  64. <3> The bucket span
  65. [[java-rest-high-x-pack-ml-put-job-data-description]]
  66. ==== Data Description
  67. After defining the analysis config, the next thing to define is the
  68. data description, using a `DataDescription` instance. `DataDescription`
  69. reflects all the configuration settings that can be defined using the
  70. REST API.
  71. Using the REST API, we could define this metrics configuration:
  72. [source,js]
  73. --------------------------------------------------
  74. "data_description" : {
  75. "time_field" : "timestamp"
  76. }
  77. --------------------------------------------------
  78. // NOTCONSOLE
  79. Using the `DataDescription` object and the high level REST client, the same
  80. configuration would be:
  81. ["source","java",subs="attributes,callouts,macros"]
  82. --------------------------------------------------
  83. include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-put-job-data-description]
  84. --------------------------------------------------
  85. <1> The time field
  86. [[java-rest-high-x-pack-ml-put-job-execution]]
  87. ==== Execution
  88. The Put Job API can be executed through a `MachineLearningClient`
  89. instance. Such an instance can be retrieved from a `RestHighLevelClient`
  90. using the `machineLearning()` method:
  91. ["source","java",subs="attributes,callouts,macros"]
  92. --------------------------------------------------
  93. include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-put-job-execute]
  94. --------------------------------------------------
  95. [[java-rest-high-x-pack-ml-put-job-response]]
  96. ==== Response
  97. The returned `PutJobResponse` returns the full representation of
  98. the new {ml} job if it has been successfully created. This will
  99. contain the creation time and other fields initialized using
  100. default values:
  101. ["source","java",subs="attributes,callouts,macros"]
  102. --------------------------------------------------
  103. include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-put-job-response]
  104. --------------------------------------------------
  105. <1> The creation time is a field that was not passed in the `Job` object in the request
  106. [[java-rest-high-x-pack-ml-put-job-async]]
  107. ==== Asynchronous Execution
  108. This request can be executed asynchronously:
  109. ["source","java",subs="attributes,callouts,macros"]
  110. --------------------------------------------------
  111. include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-put-job-execute-async]
  112. --------------------------------------------------
  113. <1> The `PutMlJobRequest` to execute and the `ActionListener` to use when
  114. the execution completes
  115. The asynchronous method does not block and returns immediately. Once it is
  116. completed the `ActionListener` is called back using the `onResponse` method
  117. if the execution successfully completed or using the `onFailure` method if
  118. it failed.
  119. A typical listener for `PutJobResponse` looks like:
  120. ["source","java",subs="attributes,callouts,macros"]
  121. --------------------------------------------------
  122. include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-put-job-execute-listener]
  123. --------------------------------------------------
  124. <1> Called when the execution is successfully completed. The response is
  125. provided as an argument
  126. <2> Called in case of failure. The raised exception is provided as an argument