|
5 | 5 |
|
6 | 6 | package org.opensearch.sql.spark.response;
|
7 | 7 |
|
8 |
| -import static org.opensearch.sql.datasource.model.DataSourceMetadata.DEFAULT_RESULT_INDEX; |
9 |
| -import static org.opensearch.sql.spark.data.constants.SparkConstants.DATA_FIELD; |
10 |
| -import static org.opensearch.sql.spark.data.constants.SparkConstants.JOB_ID_FIELD; |
11 |
| - |
12 |
| -import org.apache.logging.log4j.LogManager; |
13 |
| -import org.apache.logging.log4j.Logger; |
14 | 8 | import org.json.JSONObject;
|
15 |
| -import org.opensearch.action.search.SearchRequest; |
16 |
| -import org.opensearch.action.search.SearchResponse; |
17 |
| -import org.opensearch.client.Client; |
18 |
| -import org.opensearch.common.action.ActionFuture; |
19 |
| -import org.opensearch.index.IndexNotFoundException; |
20 |
| -import org.opensearch.index.query.QueryBuilder; |
21 |
| -import org.opensearch.index.query.QueryBuilders; |
22 |
| -import org.opensearch.search.SearchHit; |
23 |
| -import org.opensearch.search.builder.SearchSourceBuilder; |
24 |
| - |
25 |
| -public class JobExecutionResponseReader { |
26 |
| - private final Client client; |
27 |
| - private static final Logger LOG = LogManager.getLogger(); |
28 | 9 |
|
| 10 | +/** Interface for reading job execution result */ |
| 11 | +public interface JobExecutionResponseReader { |
29 | 12 | /**
|
30 |
| - * JobExecutionResponseReader for spark query. |
| 13 | + * Retrieves the job execution result based on the job ID. |
31 | 14 | *
|
32 |
| - * @param client Opensearch client |
| 15 | + * @param jobId The job ID. |
| 16 | + * @param resultLocation The location identifier where the result is stored (optional). |
| 17 | + * @return A JSONObject containing the result data. |
33 | 18 | */
|
34 |
| - public JobExecutionResponseReader(Client client) { |
35 |
| - this.client = client; |
36 |
| - } |
37 |
| - |
38 |
| - public JSONObject getResultFromOpensearchIndex(String jobId, String resultIndex) { |
39 |
| - return searchInSparkIndex(QueryBuilders.termQuery(JOB_ID_FIELD, jobId), resultIndex); |
40 |
| - } |
41 |
| - |
42 |
| - public JSONObject getResultWithQueryId(String queryId, String resultIndex) { |
43 |
| - return searchInSparkIndex(QueryBuilders.termQuery("queryId", queryId), resultIndex); |
44 |
| - } |
| 19 | + JSONObject getResultWithJobId(String jobId, String resultLocation); |
45 | 20 |
|
46 |
| - private JSONObject searchInSparkIndex(QueryBuilder query, String resultIndex) { |
47 |
| - SearchRequest searchRequest = new SearchRequest(); |
48 |
| - String searchResultIndex = resultIndex == null ? DEFAULT_RESULT_INDEX : resultIndex; |
49 |
| - searchRequest.indices(searchResultIndex); |
50 |
| - SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
51 |
| - searchSourceBuilder.query(query); |
52 |
| - searchRequest.source(searchSourceBuilder); |
53 |
| - ActionFuture<SearchResponse> searchResponseActionFuture; |
54 |
| - JSONObject data = new JSONObject(); |
55 |
| - try { |
56 |
| - searchResponseActionFuture = client.search(searchRequest); |
57 |
| - } catch (IndexNotFoundException e) { |
58 |
| - // if there is no result index (e.g., EMR-S hasn't created the index yet), we return empty |
59 |
| - // json |
60 |
| - LOG.info(resultIndex + " is not created yet."); |
61 |
| - return data; |
62 |
| - } catch (Exception e) { |
63 |
| - throw new RuntimeException(e); |
64 |
| - } |
65 |
| - SearchResponse searchResponse = searchResponseActionFuture.actionGet(); |
66 |
| - if (searchResponse.status().getStatus() != 200) { |
67 |
| - throw new RuntimeException( |
68 |
| - "Fetching result from " |
69 |
| - + searchResultIndex |
70 |
| - + " index failed with status : " |
71 |
| - + searchResponse.status()); |
72 |
| - } else { |
73 |
| - for (SearchHit searchHit : searchResponse.getHits().getHits()) { |
74 |
| - data.put(DATA_FIELD, searchHit.getSourceAsMap()); |
75 |
| - } |
76 |
| - return data; |
77 |
| - } |
78 |
| - } |
| 21 | + /** |
| 22 | + * Retrieves the job execution result based on the query ID. |
| 23 | + * |
| 24 | + * @param queryId The query ID. |
| 25 | + * @param resultLocation The location identifier where the result is stored (optional). |
| 26 | + * @return A JSONObject containing the result data. |
| 27 | + */ |
| 28 | + JSONObject getResultWithQueryId(String queryId, String resultLocation); |
79 | 29 | }
|
0 commit comments