Skip to content

Commit 350b889

Browse files
author
Christoph Büscher
committed
Add start rollup job support to HL REST Client (#34623)
This change adds support for starting a rollup job to High Level REST Client. Relates to #29827
1 parent 682b0d5 commit 350b889

13 files changed

+519
-145
lines changed

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

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
package org.elasticsearch.client;
2121

2222
import org.elasticsearch.action.ActionListener;
23-
import org.elasticsearch.client.rollup.GetRollupJobRequest;
24-
import org.elasticsearch.client.rollup.GetRollupJobResponse;
2523
import org.elasticsearch.client.rollup.GetRollupCapsRequest;
2624
import org.elasticsearch.client.rollup.GetRollupCapsResponse;
25+
import org.elasticsearch.client.rollup.GetRollupJobRequest;
26+
import org.elasticsearch.client.rollup.GetRollupJobResponse;
2727
import org.elasticsearch.client.rollup.PutRollupJobRequest;
2828
import org.elasticsearch.client.rollup.PutRollupJobResponse;
29+
import org.elasticsearch.client.rollup.StartRollupJobRequest;
30+
import org.elasticsearch.client.rollup.StartRollupJobResponse;
2931

3032
import java.io.IOException;
3133
import java.util.Collections;
@@ -56,10 +58,10 @@ public class RollupClient {
5658
*/
5759
public PutRollupJobResponse putRollupJob(PutRollupJobRequest request, RequestOptions options) throws IOException {
5860
return restHighLevelClient.performRequestAndParseEntity(request,
59-
RollupRequestConverters::putJob,
60-
options,
61-
PutRollupJobResponse::fromXContent,
62-
Collections.emptySet());
61+
RollupRequestConverters::putJob,
62+
options,
63+
PutRollupJobResponse::fromXContent,
64+
Collections.emptySet());
6365
}
6466

6567
/**
@@ -72,10 +74,44 @@ public PutRollupJobResponse putRollupJob(PutRollupJobRequest request, RequestOpt
7274
*/
7375
public void putRollupJobAsync(PutRollupJobRequest request, RequestOptions options, ActionListener<PutRollupJobResponse> listener) {
7476
restHighLevelClient.performRequestAsyncAndParseEntity(request,
75-
RollupRequestConverters::putJob,
76-
options,
77-
PutRollupJobResponse::fromXContent,
78-
listener, Collections.emptySet());
77+
RollupRequestConverters::putJob,
78+
options,
79+
PutRollupJobResponse::fromXContent,
80+
listener, Collections.emptySet());
81+
}
82+
83+
/**
84+
* Start a rollup job
85+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-start-job.html">
86+
* the docs</a> for more.
87+
* @param request the request
88+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
89+
* @return the response
90+
* @throws IOException in case there is a problem sending the request or parsing back the response
91+
*/
92+
public StartRollupJobResponse startRollupJob(StartRollupJobRequest request, RequestOptions options) throws IOException {
93+
return restHighLevelClient.performRequestAndParseEntity(request,
94+
RollupRequestConverters::startJob,
95+
options,
96+
StartRollupJobResponse::fromXContent,
97+
Collections.emptySet());
98+
}
99+
100+
/**
101+
* Asynchronously start a rollup job
102+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/rollup-start-job.html">
103+
* the docs</a> for more.
104+
* @param request the request
105+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
106+
* @param listener the listener to be notified upon request completion
107+
*/
108+
public void startRollupJobAsync(StartRollupJobRequest request, RequestOptions options,
109+
ActionListener<StartRollupJobResponse> listener) {
110+
restHighLevelClient.performRequestAsyncAndParseEntity(request,
111+
RollupRequestConverters::startJob,
112+
options,
113+
StartRollupJobResponse::fromXContent,
114+
listener, Collections.emptySet());
79115
}
80116

81117
/**
@@ -89,10 +125,10 @@ public void putRollupJobAsync(PutRollupJobRequest request, RequestOptions option
89125
*/
90126
public GetRollupJobResponse getRollupJob(GetRollupJobRequest request, RequestOptions options) throws IOException {
91127
return restHighLevelClient.performRequestAndParseEntity(request,
92-
RollupRequestConverters::getJob,
93-
options,
94-
GetRollupJobResponse::fromXContent,
95-
Collections.emptySet());
128+
RollupRequestConverters::getJob,
129+
options,
130+
GetRollupJobResponse::fromXContent,
131+
Collections.emptySet());
96132
}
97133

98134
/**
@@ -107,10 +143,10 @@ public GetRollupJobResponse getRollupJob(GetRollupJobRequest request, RequestOpt
107143

108144
public void getRollupJobAsync(GetRollupJobRequest request, RequestOptions options, ActionListener<GetRollupJobResponse> listener) {
109145
restHighLevelClient.performRequestAsyncAndParseEntity(request,
110-
RollupRequestConverters::getJob,
111-
options,
112-
GetRollupJobResponse::fromXContent,
113-
listener, Collections.emptySet());
146+
RollupRequestConverters::getJob,
147+
options,
148+
GetRollupJobResponse::fromXContent,
149+
listener, Collections.emptySet());
114150
}
115151

116152
/**
@@ -124,10 +160,10 @@ public void getRollupJobAsync(GetRollupJobRequest request, RequestOptions option
124160
*/
125161
public GetRollupCapsResponse getRollupCapabilities(GetRollupCapsRequest request, RequestOptions options) throws IOException {
126162
return restHighLevelClient.performRequestAndParseEntity(request,
127-
RollupRequestConverters::getRollupCaps,
128-
options,
129-
GetRollupCapsResponse::fromXContent,
130-
Collections.emptySet());
163+
RollupRequestConverters::getRollupCaps,
164+
options,
165+
GetRollupCapsResponse::fromXContent,
166+
Collections.emptySet());
131167
}
132168

133169
/**
@@ -139,12 +175,12 @@ public GetRollupCapsResponse getRollupCapabilities(GetRollupCapsRequest request,
139175
* @param listener the listener to be notified upon request completion
140176
*/
141177
public void getRollupCapabilitiesAsync(GetRollupCapsRequest request, RequestOptions options,
142-
ActionListener<GetRollupCapsResponse> listener) {
178+
ActionListener<GetRollupCapsResponse> listener) {
143179
restHighLevelClient.performRequestAsyncAndParseEntity(request,
144-
RollupRequestConverters::getRollupCaps,
145-
options,
146-
GetRollupCapsResponse::fromXContent,
147-
listener,
148-
Collections.emptySet());
180+
RollupRequestConverters::getRollupCaps,
181+
options,
182+
GetRollupCapsResponse::fromXContent,
183+
listener,
184+
Collections.emptySet());
149185
}
150186
}

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
package org.elasticsearch.client;
2020

2121
import org.apache.http.client.methods.HttpGet;
22+
import org.apache.http.client.methods.HttpPost;
2223
import org.apache.http.client.methods.HttpPut;
23-
import org.elasticsearch.client.rollup.GetRollupJobRequest;
2424
import org.elasticsearch.client.rollup.GetRollupCapsRequest;
25+
import org.elasticsearch.client.rollup.GetRollupJobRequest;
2526
import org.elasticsearch.client.rollup.PutRollupJobRequest;
27+
import org.elasticsearch.client.rollup.StartRollupJobRequest;
2628

2729
import java.io.IOException;
2830

@@ -36,33 +38,38 @@ private RollupRequestConverters() {
3638

3739
static Request putJob(final PutRollupJobRequest putRollupJobRequest) throws IOException {
3840
String endpoint = new RequestConverters.EndpointBuilder()
39-
.addPathPartAsIs("_xpack")
40-
.addPathPartAsIs("rollup")
41-
.addPathPartAsIs("job")
42-
.addPathPart(putRollupJobRequest.getConfig().getId())
43-
.build();
41+
.addPathPartAsIs("_xpack", "rollup", "job")
42+
.addPathPart(putRollupJobRequest.getConfig().getId())
43+
.build();
4444
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
4545
request.setEntity(createEntity(putRollupJobRequest, REQUEST_BODY_CONTENT_TYPE));
4646
return request;
4747
}
4848

49+
static Request startJob(final StartRollupJobRequest startRollupJobRequest) throws IOException {
50+
String endpoint = new RequestConverters.EndpointBuilder()
51+
.addPathPartAsIs("_xpack", "rollup", "job")
52+
.addPathPart(startRollupJobRequest.getJobId())
53+
.addPathPartAsIs("_start")
54+
.build();
55+
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
56+
return request;
57+
}
58+
4959
static Request getJob(final GetRollupJobRequest getRollupJobRequest) {
5060
String endpoint = new RequestConverters.EndpointBuilder()
51-
.addPathPartAsIs("_xpack")
52-
.addPathPartAsIs("rollup")
53-
.addPathPartAsIs("job")
54-
.addPathPart(getRollupJobRequest.getJobId())
55-
.build();
61+
.addPathPartAsIs("_xpack", "rollup", "job")
62+
.addPathPart(getRollupJobRequest.getJobId())
63+
.build();
5664
return new Request(HttpGet.METHOD_NAME, endpoint);
5765
}
5866

67+
5968
static Request getRollupCaps(final GetRollupCapsRequest getRollupCapsRequest) throws IOException {
6069
String endpoint = new RequestConverters.EndpointBuilder()
61-
.addPathPartAsIs("_xpack")
62-
.addPathPartAsIs("rollup")
63-
.addPathPartAsIs("data")
64-
.addPathPart(getRollupCapsRequest.getIndexPattern())
65-
.build();
70+
.addPathPartAsIs("_xpack", "rollup", "data")
71+
.addPathPart(getRollupCapsRequest.getIndexPattern())
72+
.build();
6673
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
6774
request.setEntity(createEntity(getRollupCapsRequest, REQUEST_BODY_CONTENT_TYPE));
6875
return request;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.rollup;
21+
22+
import org.elasticsearch.common.ParseField;
23+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
24+
import org.elasticsearch.common.xcontent.ToXContent;
25+
import org.elasticsearch.common.xcontent.ToXContentObject;
26+
import org.elasticsearch.common.xcontent.XContentBuilder;
27+
28+
import java.io.IOException;
29+
import java.util.Objects;
30+
import java.util.function.Function;
31+
32+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
33+
34+
public abstract class AcknowledgedResponse implements ToXContentObject {
35+
36+
protected static final String PARSE_FIELD_NAME = "acknowledged";
37+
private final boolean acknowledged;
38+
39+
public AcknowledgedResponse(final boolean acknowledged) {
40+
this.acknowledged = acknowledged;
41+
}
42+
43+
public boolean isAcknowledged() {
44+
return acknowledged;
45+
}
46+
47+
protected static <T> ConstructingObjectParser<T, Void> generateParser(String name, Function<Boolean, T> ctor, String parseField) {
48+
ConstructingObjectParser<T, Void> p = new ConstructingObjectParser<>(name, true, args -> ctor.apply((boolean) args[0]));
49+
p.declareBoolean(constructorArg(), new ParseField(parseField));
50+
return p;
51+
}
52+
53+
@Override
54+
public boolean equals(Object o) {
55+
if (this == o) {
56+
return true;
57+
}
58+
if (o == null || getClass() != o.getClass()) {
59+
return false;
60+
}
61+
final AcknowledgedResponse that = (AcknowledgedResponse) o;
62+
return isAcknowledged() == that.isAcknowledged();
63+
}
64+
65+
@Override
66+
public int hashCode() {
67+
return Objects.hash(acknowledged);
68+
}
69+
70+
@Override
71+
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
72+
builder.startObject();
73+
{
74+
builder.field(getFieldName(), isAcknowledged());
75+
}
76+
builder.endObject();
77+
return builder;
78+
}
79+
80+
/**
81+
* @return the field name this response uses to output the acknowledged flag
82+
*/
83+
protected String getFieldName() {
84+
return PARSE_FIELD_NAME;
85+
}
86+
}

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

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,20 @@
1818
*/
1919
package org.elasticsearch.client.rollup;
2020

21-
import org.elasticsearch.common.ParseField;
2221
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
23-
import org.elasticsearch.common.xcontent.ToXContentObject;
24-
import org.elasticsearch.common.xcontent.XContentBuilder;
2522
import org.elasticsearch.common.xcontent.XContentParser;
2623

2724
import java.io.IOException;
28-
import java.util.Objects;
2925

30-
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
3126

32-
public class PutRollupJobResponse implements ToXContentObject {
27+
public class PutRollupJobResponse extends AcknowledgedResponse {
3328

34-
private final boolean acknowledged;
35-
36-
public PutRollupJobResponse(final boolean acknowledged) {
37-
this.acknowledged = acknowledged;
38-
}
39-
40-
public boolean isAcknowledged() {
41-
return acknowledged;
42-
}
43-
44-
@Override
45-
public boolean equals(Object o) {
46-
if (this == o) {
47-
return true;
48-
}
49-
if (o == null || getClass() != o.getClass()) {
50-
return false;
51-
}
52-
final PutRollupJobResponse that = (PutRollupJobResponse) o;
53-
return isAcknowledged() == that.isAcknowledged();
29+
public PutRollupJobResponse(boolean acknowledged) {
30+
super(acknowledged);
5431
}
5532

56-
@Override
57-
public int hashCode() {
58-
return Objects.hash(acknowledged);
59-
}
60-
61-
@Override
62-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
63-
builder.startObject();
64-
{
65-
builder.field("acknowledged", isAcknowledged());
66-
}
67-
builder.endObject();
68-
return builder;
69-
}
70-
71-
private static final ConstructingObjectParser<PutRollupJobResponse, Void> PARSER
72-
= new ConstructingObjectParser<>("put_rollup_job_response", true, args -> new PutRollupJobResponse((boolean) args[0]));
73-
static {
74-
PARSER.declareBoolean(constructorArg(), new ParseField("acknowledged"));
75-
}
33+
private static final ConstructingObjectParser<PutRollupJobResponse, Void> PARSER = AcknowledgedResponse
34+
.generateParser("delete_rollup_job_response", PutRollupJobResponse::new, AcknowledgedResponse.PARSE_FIELD_NAME);
7635

7736
public static PutRollupJobResponse fromXContent(final XContentParser parser) throws IOException {
7837
return PARSER.parse(parser, null);

0 commit comments

Comments
 (0)