-
Notifications
You must be signed in to change notification settings - Fork 335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: [BitSail][Connector] Support ElasticSearch Source connector #336
Open
liuxiaocs7
wants to merge
9
commits into
bytedance:master
Choose a base branch
from
liuxiaocs7:es-source
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fb5e32e
[BitSail][Connector] Support ElasticSearch Source connector
liuxiaocs7 dc14e30
[BitSail][Connector] Add license header
liuxiaocs7 a420eb6
[BitSail][Connector] Update dependency version
liuxiaocs7 28a6fb3
retrigger ci to test
liuxiaocs7 16f35a6
[BitSail][Connector] Rename to skip ut in windows
liuxiaocs7 1abcba4
fix
liuxiaocs7 4349182
rename util
liuxiaocs7 7bc95d9
update license
liuxiaocs7 297910c
fix format
liuxiaocs7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
53 changes: 53 additions & 0 deletions
53
...main/java/com/bytedance/bitsail/connector/elasticsearch/error/ElasticsearchErrorCode.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,53 @@ | ||
/* | ||
* Copyright 2022-2023 Bytedance Ltd. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.bytedance.bitsail.connector.elasticsearch.error; | ||
|
||
import com.bytedance.bitsail.common.exception.ErrorCode; | ||
|
||
public enum ElasticsearchErrorCode implements ErrorCode { | ||
|
||
REQUIRED_VALUE("Elasticsearch-00", "The configuration file is lack of necessary options"), | ||
VALID_INDEX_FAILED("Elasticsearch-01", "Try to connect index failed."), | ||
NOT_SUPPORT_SPLIT_STRATEGY("Elasticsearch-02", "Split strategy not support yet."), | ||
FETCH_DATA_FAILED("Elasticsearch-03", "Fetch data from elasticsearch cluster failed."), | ||
DESERIALIZE_FAILED("Elasticsearch-04", "Deserialize data from elasticsearch cluster failed."); | ||
|
||
private final String code; | ||
|
||
private final String describe; | ||
|
||
ElasticsearchErrorCode(String code, String describe) { | ||
this.code = code; | ||
this.describe = describe; | ||
} | ||
|
||
@Override | ||
public String getCode() { | ||
return code; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return describe; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("Code:[%s], Describe:[%s]", this.code, | ||
this.describe); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...java/com/bytedance/bitsail/connector/elasticsearch/option/ElasticsearchReaderOptions.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,62 @@ | ||
/* | ||
* Copyright 2022-2023 Bytedance Ltd. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.bytedance.bitsail.connector.elasticsearch.option; | ||
|
||
import com.bytedance.bitsail.common.option.ConfigOption; | ||
import com.bytedance.bitsail.common.option.ReaderOptions; | ||
|
||
import com.alibaba.fastjson.TypeReference; | ||
|
||
import java.util.List; | ||
|
||
import static com.bytedance.bitsail.common.option.ConfigOptions.key; | ||
|
||
public interface ElasticsearchReaderOptions extends ReaderOptions.BaseReaderOptions { | ||
|
||
ConfigOption<List<String>> ES_HOSTS = | ||
key(ReaderOptions.READER_PREFIX + "es_hosts") | ||
.onlyReference(new TypeReference<List<String>>() { | ||
}); | ||
|
||
ConfigOption<String> ES_INDEX = | ||
key(ReaderOptions.READER_PREFIX + "es_index") | ||
.noDefaultValue(String.class); | ||
|
||
ConfigOption<Integer> CONNECTION_REQUEST_TIMEOUT_MS = | ||
key(ReaderOptions.READER_PREFIX + "connection_request_timeout_ms") | ||
.defaultValue(10000); | ||
|
||
ConfigOption<Integer> CONNECTION_TIMEOUT_MS = | ||
key(ReaderOptions.READER_PREFIX + "connection_timeout_ms") | ||
.defaultValue(10000); | ||
|
||
ConfigOption<Integer> SOCKET_TIMEOUT_MS = | ||
key(ReaderOptions.READER_PREFIX + "socket_timeout_ms") | ||
.defaultValue(60000); | ||
|
||
ConfigOption<String> SCROLL_TIME = | ||
key(ReaderOptions.READER_PREFIX + "scroll_time") | ||
.defaultValue("1m"); | ||
|
||
ConfigOption<Integer> SCROLL_SIZE = | ||
key(ReaderOptions.READER_PREFIX + "scroll_size") | ||
.defaultValue(100); | ||
|
||
ConfigOption<String> SPLIT_STRATEGY = | ||
key(ReaderOptions.READER_PREFIX + "split_strategy") | ||
.defaultValue("round_robin"); | ||
} |
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
138 changes: 138 additions & 0 deletions
138
.../main/java/com/bytedance/bitsail/connector/elasticsearch/rest/source/EsSourceRequest.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,138 @@ | ||
/* | ||
* Copyright 2022-2023 Bytedance Ltd. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.bytedance.bitsail.connector.elasticsearch.rest.source; | ||
|
||
import com.bytedance.bitsail.common.BitSailException; | ||
import com.bytedance.bitsail.connector.elasticsearch.error.ElasticsearchErrorCode; | ||
|
||
import org.apache.commons.compress.utils.Lists; | ||
import org.elasticsearch.action.search.ClearScrollRequest; | ||
import org.elasticsearch.action.search.ClearScrollResponse; | ||
import org.elasticsearch.action.search.SearchRequest; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.action.search.SearchScrollRequest; | ||
import org.elasticsearch.client.RequestOptions; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.elasticsearch.client.core.CountRequest; | ||
import org.elasticsearch.client.core.CountResponse; | ||
import org.elasticsearch.index.query.QueryBuilders; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.elasticsearch.search.SearchHit; | ||
import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class EsSourceRequest { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(EsSourceRequest.class); | ||
private final RestHighLevelClient restHighLevelClient; | ||
|
||
public EsSourceRequest(RestHighLevelClient restHighLevelClient) { | ||
this.restHighLevelClient = restHighLevelClient; | ||
} | ||
|
||
public Long validateIndex(String index) { | ||
CountRequest countRequest = new CountRequest(index); | ||
countRequest.query(QueryBuilders.matchAllQuery()); | ||
CountResponse response = null; | ||
try { | ||
response = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT); | ||
if (response == null) { | ||
throw new BitSailException(ElasticsearchErrorCode.VALID_INDEX_FAILED, | ||
"GET " + index + " metadata failed"); | ||
} | ||
if (response.status() != RestStatus.OK) { | ||
throw new BitSailException(ElasticsearchErrorCode.VALID_INDEX_FAILED, | ||
String.format("Get %s response status code = %d", index, response.status().getStatus())); | ||
} | ||
} catch (IOException e) { | ||
throw new BitSailException(ElasticsearchErrorCode.VALID_INDEX_FAILED, e.getMessage()); | ||
} | ||
return response.getCount(); | ||
} | ||
|
||
/** | ||
* Returns a list of all records, Uses scroll API for pagination. | ||
* According to: <a href="https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search-scroll.html">...</a> | ||
* | ||
* @param index index name in Elasticsearch cluster | ||
* @param columnNames source field names | ||
* @param scrollSize scroll size | ||
* @param scrollTime scroll time | ||
* @return list of documents | ||
* @throws IOException throws IOException if Elasticsearch request fails | ||
*/ | ||
public List<Map<String, Object>> getAllDocuments(String index, List<String> columnNames, int scrollSize, String scrollTime) throws IOException { | ||
LOG.info("Start get all records from index: {}, scroll size: {}, scroll time: {}", index, scrollSize, scrollTime); | ||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); | ||
searchSourceBuilder.size(scrollSize); | ||
searchSourceBuilder.query(QueryBuilders.matchAllQuery()); | ||
searchSourceBuilder.fetchSource(columnNames.toArray(new String[0]), null); | ||
searchSourceBuilder.sort("_doc"); | ||
|
||
SearchRequest searchRequest = new SearchRequest(index); | ||
searchRequest.scroll(scrollTime); | ||
searchRequest.source(searchSourceBuilder); | ||
|
||
List<Map<String, Object>> allData = Lists.newArrayList(); | ||
|
||
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); | ||
String scrollId = searchResponse.getScrollId(); | ||
SearchHit[] hits = addSearchHits(searchResponse, allData); | ||
LOG.info("Finish first time search, get {} hits.", Objects.isNull(hits) ? 0 : hits.length); | ||
|
||
while (Objects.nonNull(hits) && hits.length > 0) { | ||
LOG.info("Continue scroll query with scrollId: {}", scrollId); | ||
SearchScrollRequest searchScrollRequest = new SearchScrollRequest(scrollId); | ||
searchScrollRequest.scroll(scrollTime); | ||
searchResponse = restHighLevelClient.scroll(searchScrollRequest, RequestOptions.DEFAULT); | ||
scrollId = searchResponse.getScrollId(); | ||
hits = addSearchHits(searchResponse, allData); | ||
} | ||
|
||
clearScroll(scrollId); | ||
return allData; | ||
} | ||
|
||
private void clearScroll(String scrollId) throws IOException { | ||
ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); | ||
clearScrollRequest.addScrollId(scrollId); | ||
ClearScrollResponse clearScrollResponse = restHighLevelClient.clearScroll(clearScrollRequest, RequestOptions.DEFAULT); | ||
boolean succeeded = clearScrollResponse.isSucceeded(); | ||
if (succeeded) { | ||
LOG.info("Scroll response cleared successfully"); | ||
} else { | ||
LOG.error("Fail to clear scroll response"); | ||
} | ||
} | ||
|
||
private SearchHit[] addSearchHits(SearchResponse searchResponse, List<Map<String, Object>> allData) { | ||
SearchHit[] hits = searchResponse.getHits().getHits(); | ||
if (Objects.isNull(hits)) { | ||
return null; | ||
} | ||
for (SearchHit hit : hits) { | ||
allData.add(hit.getSourceAsMap()); | ||
} | ||
return hits; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...c/main/java/com/bytedance/bitsail/connector/elasticsearch/source/ElasticsearchSource.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,94 @@ | ||
/* | ||
* Copyright 2022-2023 Bytedance Ltd. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.bytedance.bitsail.connector.elasticsearch.source; | ||
|
||
import com.bytedance.bitsail.base.connector.reader.v1.Boundedness; | ||
import com.bytedance.bitsail.base.connector.reader.v1.Source; | ||
import com.bytedance.bitsail.base.connector.reader.v1.SourceReader; | ||
import com.bytedance.bitsail.base.connector.reader.v1.SourceSplitCoordinator; | ||
import com.bytedance.bitsail.base.connector.writer.v1.state.EmptyState; | ||
import com.bytedance.bitsail.base.execution.ExecutionEnviron; | ||
import com.bytedance.bitsail.base.extension.ParallelismComputable; | ||
import com.bytedance.bitsail.base.parallelism.ParallelismAdvice; | ||
import com.bytedance.bitsail.common.configuration.BitSailConfiguration; | ||
import com.bytedance.bitsail.common.row.Row; | ||
import com.bytedance.bitsail.common.type.TypeInfoConverter; | ||
import com.bytedance.bitsail.common.type.filemapping.FileMappingTypeInfoConverter; | ||
import com.bytedance.bitsail.connector.elasticsearch.base.EsConstants; | ||
import com.bytedance.bitsail.connector.elasticsearch.option.ElasticsearchReaderOptions; | ||
import com.bytedance.bitsail.connector.elasticsearch.source.coordinator.ElasticsearchSourceSplitCoordinator; | ||
import com.bytedance.bitsail.connector.elasticsearch.source.reader.ElasticsearchReader; | ||
import com.bytedance.bitsail.connector.elasticsearch.source.split.ElasticsearchSourceSplit; | ||
import com.bytedance.bitsail.connector.elasticsearch.source.split.ElasticsearchSplitByIndexStrategy; | ||
import com.bytedance.bitsail.connector.elasticsearch.source.split.ElasticsearchSplitStrategy; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
|
||
public class ElasticsearchSource implements Source<Row, ElasticsearchSourceSplit, EmptyState>, ParallelismComputable { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ElasticsearchSource.class); | ||
|
||
private BitSailConfiguration jobConf; | ||
|
||
@Override | ||
public void configure(ExecutionEnviron execution, BitSailConfiguration readerConfiguration) throws IOException { | ||
this.jobConf = readerConfiguration; | ||
} | ||
|
||
@Override | ||
public Boundedness getSourceBoundedness() { | ||
return Boundedness.BOUNDEDNESS; | ||
} | ||
|
||
@Override | ||
public SourceReader<Row, ElasticsearchSourceSplit> createReader(SourceReader.Context readerContext) { | ||
return new ElasticsearchReader(jobConf, readerContext); | ||
} | ||
|
||
@Override | ||
public SourceSplitCoordinator<ElasticsearchSourceSplit, EmptyState> createSplitCoordinator( | ||
SourceSplitCoordinator.Context<ElasticsearchSourceSplit, EmptyState> coordinatorContext) { | ||
return new ElasticsearchSourceSplitCoordinator(coordinatorContext, jobConf); | ||
} | ||
|
||
@Override | ||
public String getReaderName() { | ||
return EsConstants.ES_CONNECTOR_NAME; | ||
} | ||
|
||
@Override | ||
public TypeInfoConverter createTypeInfoConverter() { | ||
return new FileMappingTypeInfoConverter(getReaderName()); | ||
} | ||
|
||
@Override | ||
public ParallelismAdvice getParallelismAdvice(BitSailConfiguration commonConf, BitSailConfiguration selfConf, ParallelismAdvice upstreamAdvice) throws Exception { | ||
int parallelism; | ||
if (selfConf.fieldExists(ElasticsearchReaderOptions.READER_PARALLELISM_NUM)) { | ||
parallelism = selfConf.get(ElasticsearchReaderOptions.READER_PARALLELISM_NUM); | ||
LOG.info("Use user-defined reader parallelism: {}", parallelism); | ||
} else { | ||
ElasticsearchSplitStrategy splitStrategy = new ElasticsearchSplitByIndexStrategy(); | ||
parallelism = splitStrategy.estimateSplitNum(jobConf); | ||
LOG.info("Use index number as parallelism: {}", parallelism); | ||
} | ||
return new ParallelismAdvice(false, parallelism); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make a new interface for the class EsRestClientBuilder. i just want to make it more flexible between the difference elasticsearch version.