Skip to content

Commit c990b30

Browse files
authored
[ML] Data Frame HLRC Get API (#40509)
1 parent b4b17e1 commit c990b30

File tree

13 files changed

+668
-45
lines changed

13 files changed

+668
-45
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/DataFrameClient.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.elasticsearch.action.ActionListener;
2323
import org.elasticsearch.client.core.AcknowledgedResponse;
2424
import org.elasticsearch.client.dataframe.DeleteDataFrameTransformRequest;
25+
import org.elasticsearch.client.dataframe.GetDataFrameTransformRequest;
26+
import org.elasticsearch.client.dataframe.GetDataFrameTransformResponse;
2527
import org.elasticsearch.client.dataframe.GetDataFrameTransformStatsRequest;
2628
import org.elasticsearch.client.dataframe.GetDataFrameTransformStatsResponse;
2729
import org.elasticsearch.client.dataframe.PreviewDataFrameTransformRequest;
@@ -275,12 +277,52 @@ public StopDataFrameTransformResponse stopDataFrameTransform(StopDataFrameTransf
275277
* @param listener Listener to be notified upon request completion
276278
*/
277279
public void stopDataFrameTransformAsync(StopDataFrameTransformRequest request, RequestOptions options,
278-
ActionListener<StopDataFrameTransformResponse> listener) {
280+
ActionListener<StopDataFrameTransformResponse> listener) {
279281
restHighLevelClient.performRequestAsyncAndParseEntity(request,
280282
DataFrameRequestConverters::stopDataFrameTransform,
281283
options,
282284
StopDataFrameTransformResponse::fromXContent,
283285
listener,
284286
Collections.emptySet());
285287
}
288+
289+
/**
290+
* Get one or more data frame transform configurations
291+
* <p>
292+
* For additional info
293+
* see <a href="https://www.TODO.com">Get Data Frame transform documentation</a>
294+
*
295+
* @param request The get data frame transform request
296+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
297+
* @return An GetDataFrameTransformResponse containing the requested transforms
298+
* @throws IOException when there is a serialization issue sending the request or receiving the response
299+
*/
300+
public GetDataFrameTransformResponse getDataFrameTransform(GetDataFrameTransformRequest request, RequestOptions options)
301+
throws IOException {
302+
return restHighLevelClient.performRequestAndParseEntity(request,
303+
DataFrameRequestConverters::getDataFrameTransform,
304+
options,
305+
GetDataFrameTransformResponse::fromXContent,
306+
Collections.emptySet());
307+
}
308+
309+
/**
310+
* Get one or more data frame transform configurations asynchronously and notifies listener on completion
311+
* <p>
312+
* For additional info
313+
* see <a href="https://www.TODO.com">Get Data Frame transform documentation</a>
314+
*
315+
* @param request The get data frame transform request
316+
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
317+
* @param listener Listener to be notified upon request completion
318+
*/
319+
public void getDataFrameTransformAsync(GetDataFrameTransformRequest request, RequestOptions options,
320+
ActionListener<GetDataFrameTransformResponse> listener) {
321+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
322+
DataFrameRequestConverters::getDataFrameTransform,
323+
options,
324+
GetDataFrameTransformResponse::fromXContent,
325+
listener,
326+
Collections.emptySet());
327+
}
286328
}

client/rest-high-level/src/main/java/org/elasticsearch/client/DataFrameRequestConverters.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
import org.apache.http.client.methods.HttpPost;
2525
import org.apache.http.client.methods.HttpPut;
2626
import org.elasticsearch.client.dataframe.DeleteDataFrameTransformRequest;
27+
import org.elasticsearch.client.dataframe.GetDataFrameTransformRequest;
2728
import org.elasticsearch.client.dataframe.GetDataFrameTransformStatsRequest;
2829
import org.elasticsearch.client.dataframe.PreviewDataFrameTransformRequest;
2930
import org.elasticsearch.client.dataframe.PutDataFrameTransformRequest;
3031
import org.elasticsearch.client.dataframe.StartDataFrameTransformRequest;
3132
import org.elasticsearch.client.dataframe.StopDataFrameTransformRequest;
33+
import org.elasticsearch.common.Strings;
3234

3335
import java.io.IOException;
3436

@@ -49,6 +51,21 @@ static Request putDataFrameTransform(PutDataFrameTransformRequest putRequest) th
4951
return request;
5052
}
5153

54+
static Request getDataFrameTransform(GetDataFrameTransformRequest getRequest) {
55+
String endpoint = new RequestConverters.EndpointBuilder()
56+
.addPathPartAsIs("_data_frame", "transforms")
57+
.addPathPart(Strings.collectionToCommaDelimitedString(getRequest.getId()))
58+
.build();
59+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
60+
if (getRequest.getFrom() != null) {
61+
request.addParameter("from", getRequest.getFrom().toString());
62+
}
63+
if (getRequest.getSize() != null) {
64+
request.addParameter("size", getRequest.getSize().toString());
65+
}
66+
return request;
67+
}
68+
5269
static Request deleteDataFrameTransform(DeleteDataFrameTransformRequest request) {
5370
String endpoint = new RequestConverters.EndpointBuilder()
5471
.addPathPartAsIs("_data_frame", "transforms")
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.dataframe;
21+
22+
import org.elasticsearch.client.Validatable;
23+
import org.elasticsearch.client.ValidationException;
24+
25+
import java.util.Arrays;
26+
import java.util.List;
27+
import java.util.Objects;
28+
import java.util.Optional;
29+
30+
public class GetDataFrameTransformRequest implements Validatable {
31+
32+
private final List<String> ids;
33+
private Integer from;
34+
private Integer size;
35+
36+
/**
37+
* Helper method to create a request that will get ALL Data Frame Transforms
38+
* @return new {@link GetDataFrameTransformRequest} object for the id "_all"
39+
*/
40+
public static GetDataFrameTransformRequest getAllDataFrameTransformsRequest() {
41+
return new GetDataFrameTransformRequest("_all");
42+
}
43+
44+
public GetDataFrameTransformRequest(String... ids) {
45+
this.ids = Arrays.asList(ids);
46+
}
47+
48+
public List<String> getId() {
49+
return ids;
50+
}
51+
52+
public Integer getFrom() {
53+
return from;
54+
}
55+
56+
public void setFrom(Integer from) {
57+
this.from = from;
58+
}
59+
60+
public Integer getSize() {
61+
return size;
62+
}
63+
64+
public void setSize(Integer size) {
65+
this.size = size;
66+
}
67+
68+
@Override
69+
public Optional<ValidationException> validate() {
70+
if (ids == null || ids.isEmpty()) {
71+
ValidationException validationException = new ValidationException();
72+
validationException.addValidationError("data frame transform id must not be null");
73+
return Optional.of(validationException);
74+
} else {
75+
return Optional.empty();
76+
}
77+
}
78+
79+
@Override
80+
public int hashCode() {
81+
return Objects.hash(ids);
82+
}
83+
84+
@Override
85+
public boolean equals(Object obj) {
86+
if (this == obj) {
87+
return true;
88+
}
89+
90+
if (obj == null || getClass() != obj.getClass()) {
91+
return false;
92+
}
93+
GetDataFrameTransformRequest other = (GetDataFrameTransformRequest) obj;
94+
return Objects.equals(ids, other.ids);
95+
}
96+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.dataframe;
21+
22+
import org.elasticsearch.client.dataframe.transforms.DataFrameTransformConfig;
23+
import org.elasticsearch.common.Nullable;
24+
import org.elasticsearch.common.ParseField;
25+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
26+
import org.elasticsearch.common.xcontent.XContentParser;
27+
28+
import java.util.List;
29+
import java.util.Objects;
30+
31+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
32+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
33+
34+
public class GetDataFrameTransformResponse {
35+
36+
public static final ParseField TRANSFORMS = new ParseField("transforms");
37+
public static final ParseField INVALID_TRANSFORMS = new ParseField("invalid_transforms");
38+
public static final ParseField COUNT = new ParseField("count");
39+
40+
@SuppressWarnings("unchecked")
41+
static final ConstructingObjectParser<InvalidTransforms, Void> INVALID_TRANSFORMS_PARSER =
42+
new ConstructingObjectParser<>("invalid_transforms", true, args -> new InvalidTransforms((List<String>) args[0]));
43+
44+
@SuppressWarnings("unchecked")
45+
static final ConstructingObjectParser<GetDataFrameTransformResponse, Void> PARSER = new ConstructingObjectParser<>(
46+
"get_data_frame_transform", true, args -> new GetDataFrameTransformResponse(
47+
(List<DataFrameTransformConfig>) args[0], (int) args[1], (InvalidTransforms) args[2]));
48+
static {
49+
// Discard the count field which is the size of the transforms array
50+
INVALID_TRANSFORMS_PARSER.declareInt((a, b) -> {}, COUNT);
51+
INVALID_TRANSFORMS_PARSER.declareStringArray(constructorArg(), TRANSFORMS);
52+
53+
PARSER.declareObjectArray(constructorArg(), DataFrameTransformConfig.PARSER::apply, TRANSFORMS);
54+
PARSER.declareInt(constructorArg(), COUNT);
55+
PARSER.declareObject(optionalConstructorArg(), INVALID_TRANSFORMS_PARSER::apply, INVALID_TRANSFORMS);
56+
}
57+
58+
public static GetDataFrameTransformResponse fromXContent(final XContentParser parser) {
59+
return GetDataFrameTransformResponse.PARSER.apply(parser, null);
60+
}
61+
62+
private List<DataFrameTransformConfig> transformConfigurations;
63+
private int count;
64+
private InvalidTransforms invalidTransforms;
65+
66+
public GetDataFrameTransformResponse(List<DataFrameTransformConfig> transformConfigurations,
67+
int count,
68+
@Nullable InvalidTransforms invalidTransforms) {
69+
this.transformConfigurations = transformConfigurations;
70+
this.count = count;
71+
this.invalidTransforms = invalidTransforms;
72+
}
73+
74+
@Nullable
75+
public InvalidTransforms getInvalidTransforms() {
76+
return invalidTransforms;
77+
}
78+
79+
public int getCount() {
80+
return count;
81+
}
82+
83+
public List<DataFrameTransformConfig> getTransformConfigurations() {
84+
return transformConfigurations;
85+
}
86+
87+
@Override
88+
public int hashCode() {
89+
return Objects.hash(transformConfigurations, count, invalidTransforms);
90+
}
91+
92+
@Override
93+
public boolean equals(Object other) {
94+
if (this == other) {
95+
return true;
96+
}
97+
98+
if (other == null || getClass() != other.getClass()) {
99+
return false;
100+
}
101+
102+
final GetDataFrameTransformResponse that = (GetDataFrameTransformResponse) other;
103+
return Objects.equals(this.transformConfigurations, that.transformConfigurations)
104+
&& Objects.equals(this.count, that.count)
105+
&& Objects.equals(this.invalidTransforms, that.invalidTransforms);
106+
}
107+
108+
static class InvalidTransforms {
109+
private final List<String> transformIds;
110+
111+
InvalidTransforms(List<String> transformIds) {
112+
this.transformIds = transformIds;
113+
}
114+
115+
public int getCount() {
116+
return transformIds.size();
117+
}
118+
119+
public List<String> getTransformIds() {
120+
return transformIds;
121+
}
122+
123+
@Override
124+
public int hashCode() {
125+
return Objects.hash(transformIds);
126+
}
127+
128+
@Override
129+
public boolean equals(Object other) {
130+
if (this == other) {
131+
return true;
132+
}
133+
134+
if (other == null || getClass() != other.getClass()) {
135+
return false;
136+
}
137+
138+
final InvalidTransforms that = (InvalidTransforms) other;
139+
return Objects.equals(this.transformIds, that.transformIds);
140+
}
141+
}
142+
}

client/rest-high-level/src/main/java/org/elasticsearch/client/dataframe/transforms/DataFrameTransformConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class DataFrameTransformConfig implements ToXContentObject {
4646
private final DestConfig dest;
4747
private final PivotConfig pivotConfig;
4848

49-
public static final ConstructingObjectParser<DataFrameTransformConfig, String> PARSER =
49+
public static final ConstructingObjectParser<DataFrameTransformConfig, Void> PARSER =
5050
new ConstructingObjectParser<>("data_frame_transform", true,
5151
(args) -> {
5252
String id = (String) args[0];

client/rest-high-level/src/test/java/org/elasticsearch/client/DataFrameRequestConvertersTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.http.client.methods.HttpPost;
2525
import org.apache.http.client.methods.HttpPut;
2626
import org.elasticsearch.client.dataframe.DeleteDataFrameTransformRequest;
27+
import org.elasticsearch.client.dataframe.GetDataFrameTransformRequest;
2728
import org.elasticsearch.client.dataframe.GetDataFrameTransformStatsRequest;
2829
import org.elasticsearch.client.dataframe.PreviewDataFrameTransformRequest;
2930
import org.elasticsearch.client.dataframe.PutDataFrameTransformRequest;
@@ -147,4 +148,29 @@ public void testGetDataFrameTransformStats() {
147148
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
148149
assertThat(request.getEndpoint(), equalTo("/_data_frame/transforms/foo/_stats"));
149150
}
151+
152+
public void testGetDataFrameTransform() {
153+
GetDataFrameTransformRequest getRequest = new GetDataFrameTransformRequest("bar");
154+
Request request = DataFrameRequestConverters.getDataFrameTransform(getRequest);
155+
156+
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
157+
assertThat(request.getEndpoint(), equalTo("/_data_frame/transforms/bar"));
158+
159+
assertFalse(request.getParameters().containsKey("from"));
160+
assertFalse(request.getParameters().containsKey("size"));
161+
162+
getRequest.setFrom(0);
163+
getRequest.setSize(10);
164+
request = DataFrameRequestConverters.getDataFrameTransform(getRequest);
165+
assertEquals("0", request.getParameters().get("from"));
166+
assertEquals("10", request.getParameters().get("size"));
167+
}
168+
169+
public void testGetDataFrameTransform_givenMulitpleIds() {
170+
GetDataFrameTransformRequest getRequest = new GetDataFrameTransformRequest("foo", "bar", "baz");
171+
Request request = DataFrameRequestConverters.getDataFrameTransform(getRequest);
172+
173+
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
174+
assertThat(request.getEndpoint(), equalTo("/_data_frame/transforms/foo,bar,baz"));
175+
}
150176
}

0 commit comments

Comments
 (0)