-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change JobExecutionResponseReader to an interface (#2693)
* Change JobExecutionResponseReader to an interface Signed-off-by: Tomoyuki Morita <[email protected]> * Fix comment Signed-off-by: Tomoyuki Morita <[email protected]> --------- Signed-off-by: Tomoyuki Morita <[email protected]> (cherry picked from commit 3dd1729) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
1768eb6
commit 21d5c6f
Showing
8 changed files
with
121 additions
and
91 deletions.
There are no files selected for viewing
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
77 changes: 77 additions & 0 deletions
77
...src/main/java/org/opensearch/sql/spark/response/OpenSearchJobExecutionResponseReader.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,77 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.spark.response; | ||
|
||
import static org.opensearch.sql.datasource.model.DataSourceMetadata.DEFAULT_RESULT_INDEX; | ||
import static org.opensearch.sql.spark.data.constants.SparkConstants.DATA_FIELD; | ||
import static org.opensearch.sql.spark.data.constants.SparkConstants.JOB_ID_FIELD; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.json.JSONObject; | ||
import org.opensearch.action.search.SearchRequest; | ||
import org.opensearch.action.search.SearchResponse; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.common.action.ActionFuture; | ||
import org.opensearch.index.IndexNotFoundException; | ||
import org.opensearch.index.query.QueryBuilder; | ||
import org.opensearch.index.query.QueryBuilders; | ||
import org.opensearch.search.SearchHit; | ||
import org.opensearch.search.builder.SearchSourceBuilder; | ||
|
||
/** JobExecutionResponseReader implementation for reading response from OpenSearch index. */ | ||
public class OpenSearchJobExecutionResponseReader implements JobExecutionResponseReader { | ||
private final Client client; | ||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
public OpenSearchJobExecutionResponseReader(Client client) { | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
public JSONObject getResultWithJobId(String jobId, String resultLocation) { | ||
return searchInSparkIndex(QueryBuilders.termQuery(JOB_ID_FIELD, jobId), resultLocation); | ||
} | ||
|
||
@Override | ||
public JSONObject getResultWithQueryId(String queryId, String resultLocation) { | ||
return searchInSparkIndex(QueryBuilders.termQuery("queryId", queryId), resultLocation); | ||
} | ||
|
||
private JSONObject searchInSparkIndex(QueryBuilder query, String resultIndex) { | ||
SearchRequest searchRequest = new SearchRequest(); | ||
String searchResultIndex = resultIndex == null ? DEFAULT_RESULT_INDEX : resultIndex; | ||
searchRequest.indices(searchResultIndex); | ||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); | ||
searchSourceBuilder.query(query); | ||
searchRequest.source(searchSourceBuilder); | ||
ActionFuture<SearchResponse> searchResponseActionFuture; | ||
JSONObject data = new JSONObject(); | ||
try { | ||
searchResponseActionFuture = client.search(searchRequest); | ||
} catch (IndexNotFoundException e) { | ||
// if there is no result index (e.g., EMR-S hasn't created the index yet), we return empty | ||
// json | ||
LOG.info(resultIndex + " is not created yet."); | ||
return data; | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
SearchResponse searchResponse = searchResponseActionFuture.actionGet(); | ||
if (searchResponse.status().getStatus() != 200) { | ||
throw new RuntimeException( | ||
"Fetching result from " | ||
+ searchResultIndex | ||
+ " index failed with status : " | ||
+ searchResponse.status()); | ||
} else { | ||
for (SearchHit searchHit : searchResponse.getHits().getHits()) { | ||
data.put(DATA_FIELD, searchHit.getSourceAsMap()); | ||
} | ||
return data; | ||
} | ||
} | ||
} |
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
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
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