-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding ML HLRC wrapper and put_job API call #32726
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.protocol.xpack.ml.PutJobRequest; | ||
import org.elasticsearch.protocol.xpack.ml.PutJobResponse; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
|
||
/** | ||
* Machine Learning API client wrapper for the {@link RestHighLevelClient} | ||
* | ||
* <p> | ||
* See the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-apis.html"> | ||
* X-Pack Machine Learning APIs </a> for additional information. | ||
*/ | ||
public final class MachineLearningClient { | ||
|
||
private final RestHighLevelClient restHighLevelClient; | ||
|
||
MachineLearningClient(RestHighLevelClient restHighLevelClient) { | ||
this.restHighLevelClient = restHighLevelClient; | ||
} | ||
|
||
/** | ||
* Creates a new Machine Learning Job | ||
* <p> | ||
* For additional info | ||
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-put-job.html">ML PUT job documentation</a> | ||
* | ||
* @param request the PutJobRequest containing the {@link org.elasticsearch.protocol.xpack.ml.job.config.Job} settings | ||
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized | ||
* @return PutJobResponse with enclosed {@link org.elasticsearch.protocol.xpack.ml.job.config.Job} object | ||
* @throws IOException when there is a serialization issue sending the request or receiving the response | ||
*/ | ||
public PutJobResponse putJob(PutJobRequest request, RequestOptions options) throws IOException { | ||
return restHighLevelClient.performRequestAndParseEntity(request, | ||
RequestConverters::putMachineLearningJob, | ||
options, | ||
PutJobResponse::fromXContent, | ||
Collections.emptySet()); | ||
} | ||
|
||
/** | ||
* Creates a new Machine Learning Job asynchronously and notifies listener on completion | ||
* <p> | ||
* For additional info | ||
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-put-job.html">ML PUT job documentation</a> | ||
* | ||
* @param request the request containing the {@link org.elasticsearch.protocol.xpack.ml.job.config.Job} settings | ||
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized | ||
* @param listener Listener to be notified upon request completion | ||
*/ | ||
public void putJobAsync(PutJobRequest request, RequestOptions options, ActionListener<PutJobResponse> listener) { | ||
restHighLevelClient.performRequestAsyncAndParseEntity(request, | ||
RequestConverters::putMachineLearningJob, | ||
options, | ||
PutJobResponse::fromXContent, | ||
listener, | ||
Collections.emptySet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client; | ||
|
||
import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.protocol.xpack.ml.PutJobRequest; | ||
import org.elasticsearch.protocol.xpack.ml.PutJobResponse; | ||
import org.elasticsearch.protocol.xpack.ml.job.config.AnalysisConfig; | ||
import org.elasticsearch.protocol.xpack.ml.job.config.DataDescription; | ||
import org.elasticsearch.protocol.xpack.ml.job.config.Detector; | ||
import org.elasticsearch.protocol.xpack.ml.job.config.Job; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
public class MachineLearningIT extends ESRestHighLevelClientTestCase { | ||
|
||
public void testPutJob() throws Exception { | ||
String jobId = randomValidJobId(); | ||
Job job = buildJob(jobId); | ||
MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); | ||
|
||
PutJobResponse putJobResponse = execute(new PutJobRequest(job), machineLearningClient::putJob, machineLearningClient::putJobAsync); | ||
Job createdJob = putJobResponse.getResponse(); | ||
|
||
assertThat(createdJob.getId(), is(jobId)); | ||
assertThat(createdJob.getJobType(), is(Job.ANOMALY_DETECTOR_JOB_TYPE)); | ||
} | ||
|
||
public static String randomValidJobId() { | ||
CodepointSetGenerator generator = new CodepointSetGenerator("abcdefghijklmnopqrstuvwxyz0123456789".toCharArray()); | ||
return generator.ofCodePointsLength(random(), 10, 10); | ||
} | ||
|
||
private static Job buildJob(String jobId) { | ||
Job.Builder builder = new Job.Builder(jobId); | ||
builder.setDescription(randomAlphaOfLength(10)); | ||
|
||
Detector detector = new Detector.Builder() | ||
.setFieldName("total") | ||
.setFunction("sum") | ||
.setDetectorDescription(randomAlphaOfLength(10)) | ||
.build(); | ||
AnalysisConfig.Builder configBuilder = new AnalysisConfig.Builder(Arrays.asList(detector)); | ||
configBuilder.setBucketSpan(new TimeValue(randomIntBetween(1, 10), TimeUnit.SECONDS)); | ||
builder.setAnalysisConfig(configBuilder); | ||
|
||
DataDescription.Builder dataDescription = new DataDescription.Builder(); | ||
dataDescription.setTimeFormat(randomFrom(DataDescription.EPOCH_MS, DataDescription.EPOCH)); | ||
dataDescription.setTimeField(randomAlphaOfLength(10)); | ||
builder.setDataDescription(dataDescription); | ||
|
||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/ml/PutJobRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.protocol.xpack.ml; | ||
|
||
import org.elasticsearch.action.ActionRequest; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.protocol.xpack.ml.job.config.Job; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class PutJobRequest extends ActionRequest implements ToXContentObject { | ||
|
||
private final Job job; | ||
|
||
public PutJobRequest(Job job) { | ||
this.job = job; | ||
} | ||
|
||
public Job getJob() { | ||
return job; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
return job.toXContent(builder, params); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) { | ||
return true; | ||
} | ||
|
||
if (object == null || getClass() != object.getClass()) { | ||
return false; | ||
} | ||
|
||
PutJobRequest request = (PutJobRequest) object; | ||
return Objects.equals(job, request.job); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(job); | ||
} | ||
|
||
@Override | ||
public final String toString() { | ||
return Strings.toString(this); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make jobs built with this method reusable when we come to send data to them in other tests, I think it would be better to fix the time format to
EPOCH_MS
and time field name to a specific value, e.g. "time".However, since it takes hours to get the PR CI build to complete and we'll be changing this same file in future PRs that implement the other endpoints I'm happy to leave this as-is for now.