Skip to content

Commit

Permalink
[HLRC][ML] Add ML find file structure API
Browse files Browse the repository at this point in the history
Relates to elastic#29827
  • Loading branch information
droberts195 committed Nov 22, 2018
1 parent 6f46584 commit b43d3a5
Show file tree
Hide file tree
Showing 14 changed files with 1,786 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.elasticsearch.client.ml.DeleteForecastRequest;
import org.elasticsearch.client.ml.DeleteJobRequest;
import org.elasticsearch.client.ml.DeleteModelSnapshotRequest;
import org.elasticsearch.client.ml.FindFileStructureRequest;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.ForecastJobRequest;
import org.elasticsearch.client.ml.GetBucketsRequest;
Expand Down Expand Up @@ -70,6 +71,7 @@
import org.elasticsearch.client.ml.job.util.PageParams;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

Expand Down Expand Up @@ -648,4 +650,65 @@ static Request deleteFilter(DeleteFilterRequest deleteFilterRequest) {
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
return request;
}

static Request findFileStructure(FindFileStructureRequest findFileStructureRequest) {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_xpack")
.addPathPartAsIs("ml")
.addPathPartAsIs("find_file_structure")
.build();
Request request = new Request(HttpPost.METHOD_NAME, endpoint);

RequestConverters.Params params = new RequestConverters.Params(request);
if (findFileStructureRequest.getLinesToSample() != null) {
params.putParam(FindFileStructureRequest.LINES_TO_SAMPLE.getPreferredName(),
findFileStructureRequest.getLinesToSample().toString());
}
if (findFileStructureRequest.getTimeout() != null) {
params.putParam(FindFileStructureRequest.TIMEOUT.getPreferredName(), findFileStructureRequest.getTimeout().toString());
}
if (findFileStructureRequest.getCharset() != null) {
params.putParam(FindFileStructureRequest.CHARSET.getPreferredName(), findFileStructureRequest.getCharset());
}
if (findFileStructureRequest.getFormat() != null) {
params.putParam(FindFileStructureRequest.FORMAT.getPreferredName(), findFileStructureRequest.getFormat().toString());
}
if (findFileStructureRequest.getColumnNames() != null) {
params.putParam(FindFileStructureRequest.COLUMN_NAMES.getPreferredName(),
Strings.collectionToCommaDelimitedString(findFileStructureRequest.getColumnNames()));
}
if (findFileStructureRequest.getHasHeaderRow() != null) {
params.putParam(FindFileStructureRequest.HAS_HEADER_ROW.getPreferredName(),
findFileStructureRequest.getHasHeaderRow().toString());
}
if (findFileStructureRequest.getDelimiter() != null) {
params.putParam(FindFileStructureRequest.DELIMITER.getPreferredName(),
findFileStructureRequest.getDelimiter().toString());
}
if (findFileStructureRequest.getQuote() != null) {
params.putParam(FindFileStructureRequest.QUOTE.getPreferredName(), findFileStructureRequest.getQuote().toString());
}
if (findFileStructureRequest.getShouldTrimFields() != null) {
params.putParam(FindFileStructureRequest.SHOULD_TRIM_FIELDS.getPreferredName(),
findFileStructureRequest.getShouldTrimFields().toString());
}
if (findFileStructureRequest.getGrokPattern() != null) {
params.putParam(FindFileStructureRequest.GROK_PATTERN.getPreferredName(), findFileStructureRequest.getGrokPattern());
}
if (findFileStructureRequest.getTimestampFormat() != null) {
params.putParam(FindFileStructureRequest.TIMESTAMP_FORMAT.getPreferredName(), findFileStructureRequest.getTimestampFormat());
}
if (findFileStructureRequest.getTimestampField() != null) {
params.putParam(FindFileStructureRequest.TIMESTAMP_FIELD.getPreferredName(), findFileStructureRequest.getTimestampField());
}
if (findFileStructureRequest.getExplain() != null) {
params.putParam(FindFileStructureRequest.EXPLAIN.getPreferredName(), findFileStructureRequest.getExplain().toString());
}

BytesReference sample = findFileStructureRequest.getSample();
BytesRef source = sample.toBytesRef();
HttpEntity byteEntity = new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(XContentType.JSON));
request.setEntity(byteEntity);
return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.elasticsearch.client.ml.DeleteJobRequest;
import org.elasticsearch.client.ml.DeleteJobResponse;
import org.elasticsearch.client.ml.DeleteModelSnapshotRequest;
import org.elasticsearch.client.ml.FindFileStructureRequest;
import org.elasticsearch.client.ml.FindFileStructureResponse;
import org.elasticsearch.client.ml.FlushJobRequest;
import org.elasticsearch.client.ml.FlushJobResponse;
import org.elasticsearch.client.ml.ForecastJobRequest;
Expand Down Expand Up @@ -1711,4 +1713,45 @@ public void deleteFilterAsync(DeleteFilterRequest request, RequestOptions option
listener,
Collections.emptySet());
}

/**
* Finds the structure of a file
* <p>
* For additional info
* see <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-find-file-structure.html">
* ML Find File Structure documentation</a>
*
* @param request The find file structure request
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response containing details of the file structure
* @throws IOException when there is a serialization issue sending the request or receiving the response
*/
public FindFileStructureResponse findFileStructure(FindFileStructureRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request,
MLRequestConverters::findFileStructure,
options,
FindFileStructureResponse::fromXContent,
Collections.emptySet());
}

/**
* Finds the structure of a file asynchronously and notifies the listener on completion
* <p>
* For additional info
* see <a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-find-file-structure.html">
* ML Find File Structure documentation</a>
*
* @param request The find file structure request
* @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 findFileStructureAsync(FindFileStructureRequest request, RequestOptions options,
ActionListener<FindFileStructureResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request,
MLRequestConverters::findFileStructure,
options,
FindFileStructureResponse::fromXContent,
listener,
Collections.emptySet());
}
}
Loading

0 comments on commit b43d3a5

Please sign in to comment.