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