-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[Feature][Connector-V2] HTTP supports page increase #5477 #5561
Merged
Hisoka-X
merged 31 commits into
apache:dev
from
xiaofan2022:http_supports_page_increase
Oct 26, 2023
Merged
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
2aed008
http supports page increase #5477
93c3cb4
Update LICENSE
xiaofan2022 49c2f63
spotless
a4b415c
Update LICENSE
xiaofan2022 1f2e6db
add apache header
6dc524a
spotless
51dfd47
spotless
8fef4b7
update license header
9b491a3
Update LICENSE
xiaofan2022 11c7aa5
add test
f5928f7
add test
51167f0
update
c087c71
update
046d62b
update
eb28c3a
update
f12494f
no page num use read size
3a5089b
update
c47ef50
update
bd7e8d8
update
682142f
rollback license
0c65949
Update LICENSE
xiaofan2022 1f71652
update
cf7f7a4
update
ec50835
update
bd59a6a
update
af60117
Merge branch 'dev' into http_supports_page_increase
xiaofan2022 425814a
resolve conflicts
61fd6cb
resolve conflicts
00dd09d
delete comment
8a246af
Update docs/en/connector-v2/source/Http.md
EricJoy2048 301f99e
Update Http.md
EricJoy2048 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -48,6 +48,10 @@ They can be downloaded via install-plugin.sh or from the Maven central repositor | |||||
| schema | Config | No | - | Http and seatunnel data structure mapping | | ||||||
| schema.fields | Config | No | - | The schema fields of upstream data | | ||||||
| json_field | Config | No | - | This parameter helps you configure the schema,so this parameter must be used with schema. | | ||||||
| pageing | Config | No | - | This parameter is used for paging queries | | ||||||
| pageing.page_field | String | No | - | This parameter is used to specify the page field name in the request parameter | | ||||||
| pageing.total_page_size | Int | No | - | This parameter is used to control the total number of pages | | ||||||
| pageing.batch_size | Int | No | - | The batch size returned per request is used to determine whether to continue when the total number of pages is unknown | | ||||||
| content_json | String | No | - | This parameter can get some json data.If you only need the data in the 'book' section, configure `content_field = "$.store.book.*"`. | | ||||||
| format | String | No | json | The format of upstream data, now only support `json` `text`, default `json`. | | ||||||
| method | String | No | get | Http request method, only supports GET, POST method. | | ||||||
|
@@ -310,6 +314,35 @@ source { | |||||
- Test data can be found at this link [mockserver-config.json](../../../../seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/mockserver-config.json) | ||||||
- See this link for task configuration [http_jsonpath_to_assert.conf](../../../../seatunnel-e2e/seatunnel-connector-v2-e2e/connector-http-e2e/src/test/resources/http_jsonpath_to_assert.conf). | ||||||
|
||||||
### pageing | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
EricJoy2048 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
```hocon | ||||||
source { | ||||||
Http { | ||||||
url = "http://localhost:8080/mock/queryData" | ||||||
method = "GET" | ||||||
format = "json" | ||||||
params={ | ||||||
page: "${page}" | ||||||
} | ||||||
content_field = "$.data.*" | ||||||
pageing={ | ||||||
total_page_size=20 | ||||||
page_field=page | ||||||
#when don't know the total_page_size use batch_size if read size<batch_size finish ,otherwise continue | ||||||
#batch_size=10 | ||||||
} | ||||||
schema = { | ||||||
fields { | ||||||
name = string | ||||||
age = string | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
``` | ||||||
|
||||||
## Changelog | ||||||
|
||||||
### 2.2.0-beta 2022-09-26 | ||||||
|
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
35 changes: 35 additions & 0 deletions
35
...tp-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/config/PageInfo.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,35 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.seatunnel.connectors.seatunnel.http.config; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
import java.io.Serializable; | ||
|
||
@Setter | ||
@Getter | ||
@ToString | ||
public class PageInfo implements Serializable { | ||
|
||
private Long totalPageSize; | ||
|
||
private Integer batchSize; | ||
private String pageField; | ||
private Long pageIndex; | ||
} |
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
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -28,6 +28,7 @@ | |||
import org.apache.seatunnel.connectors.seatunnel.http.client.HttpResponse; | ||||
import org.apache.seatunnel.connectors.seatunnel.http.config.HttpParameter; | ||||
import org.apache.seatunnel.connectors.seatunnel.http.config.JsonField; | ||||
import org.apache.seatunnel.connectors.seatunnel.http.config.PageInfo; | ||||
import org.apache.seatunnel.connectors.seatunnel.http.exception.HttpConnectorErrorCode; | ||||
import org.apache.seatunnel.connectors.seatunnel.http.exception.HttpConnectorException; | ||||
|
||||
|
@@ -36,6 +37,7 @@ | |||
import com.jayway.jsonpath.JsonPath; | ||||
import com.jayway.jsonpath.Option; | ||||
import com.jayway.jsonpath.ReadContext; | ||||
import lombok.Setter; | ||||
import lombok.extern.slf4j.Slf4j; | ||||
|
||||
import java.io.BufferedReader; | ||||
|
@@ -46,8 +48,10 @@ | |||
import java.util.List; | ||||
import java.util.Map; | ||||
import java.util.Objects; | ||||
import java.util.Optional; | ||||
|
||||
@Slf4j | ||||
@Setter | ||||
public class HttpSourceReader extends AbstractSingleSplitReader<SeaTunnelRow> { | ||||
protected final SingleSplitReaderContext context; | ||||
protected final HttpParameter httpParameter; | ||||
|
@@ -61,6 +65,8 @@ public class HttpSourceReader extends AbstractSingleSplitReader<SeaTunnelRow> { | |||
private final String contentJson; | ||||
private final Configuration jsonConfiguration = | ||||
Configuration.defaultConfiguration().addOptions(DEFAULT_OPTIONS); | ||||
private boolean noMoreElementFlag = true; | ||||
private Optional<PageInfo> pageInfoOptional = Optional.empty(); | ||||
|
||||
public HttpSourceReader( | ||||
HttpParameter httpParameter, | ||||
|
@@ -75,6 +81,21 @@ public HttpSourceReader( | |||
this.contentJson = contentJson; | ||||
} | ||||
|
||||
public HttpSourceReader( | ||||
HttpParameter httpParameter, | ||||
SingleSplitReaderContext context, | ||||
DeserializationSchema<SeaTunnelRow> deserializationSchema, | ||||
JsonField jsonField, | ||||
String contentJson, | ||||
PageInfo pageInfo) { | ||||
this.context = context; | ||||
this.httpParameter = httpParameter; | ||||
this.deserializationCollector = new DeserializationCollector(deserializationSchema); | ||||
this.jsonField = jsonField; | ||||
this.contentJson = contentJson; | ||||
this.pageInfoOptional = Optional.ofNullable(pageInfo); | ||||
} | ||||
|
||||
@Override | ||||
public void open() { | ||||
httpClient = new HttpClientProvider(httpParameter); | ||||
|
@@ -87,40 +108,73 @@ public void close() throws IOException { | |||
} | ||||
} | ||||
|
||||
@Override | ||||
public void pollNext(Collector<SeaTunnelRow> output) throws Exception { | ||||
try { | ||||
HttpResponse response = | ||||
httpClient.execute( | ||||
this.httpParameter.getUrl(), | ||||
this.httpParameter.getMethod().getMethod(), | ||||
this.httpParameter.getHeaders(), | ||||
this.httpParameter.getParams(), | ||||
this.httpParameter.getBody()); | ||||
if (HttpResponse.STATUS_OK == response.getCode()) { | ||||
String content = response.getContent(); | ||||
if (!Strings.isNullOrEmpty(content)) { | ||||
if (this.httpParameter.isEnableMultilines()) { | ||||
StringReader stringReader = new StringReader(content); | ||||
BufferedReader bufferedReader = new BufferedReader(stringReader); | ||||
String lineStr; | ||||
while ((lineStr = bufferedReader.readLine()) != null) { | ||||
collect(output, lineStr); | ||||
} | ||||
} else { | ||||
collect(output, content); | ||||
public void pollAndCollectData(Collector<SeaTunnelRow> output) throws Exception { | ||||
HttpResponse response = | ||||
httpClient.execute( | ||||
this.httpParameter.getUrl(), | ||||
this.httpParameter.getMethod().getMethod(), | ||||
this.httpParameter.getHeaders(), | ||||
this.httpParameter.getParams(), | ||||
this.httpParameter.getBody()); | ||||
if (HttpResponse.STATUS_OK == response.getCode()) { | ||||
String content = response.getContent(); | ||||
if (!Strings.isNullOrEmpty(content)) { | ||||
if (this.httpParameter.isEnableMultilines()) { | ||||
StringReader stringReader = new StringReader(content); | ||||
BufferedReader bufferedReader = new BufferedReader(stringReader); | ||||
String lineStr; | ||||
while ((lineStr = bufferedReader.readLine()) != null) { | ||||
collect(output, lineStr); | ||||
} | ||||
} else { | ||||
collect(output, content); | ||||
} | ||||
return; | ||||
} | ||||
log.info( | ||||
"http client execute success request param:[{}], http response status code:[{}], content:[{}]", | ||||
httpParameter.getParams(), | ||||
response.getCode(), | ||||
response.getContent()); | ||||
} else { | ||||
log.error( | ||||
"http client execute exception, http response status code:[{}], content:[{}]", | ||||
response.getCode(), | ||||
response.getContent()); | ||||
} | ||||
} | ||||
|
||||
private void updateRequestParam(PageInfo pageInfo) { | ||||
if (this.httpParameter.getParams() == null) { | ||||
httpParameter.setParams(new HashMap<>()); | ||||
} | ||||
this.httpParameter | ||||
.getParams() | ||||
.put(pageInfo.getPageField(), pageInfo.getPageIndex().toString()); | ||||
// | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||
} | ||||
|
||||
@Override | ||||
public void pollNext(Collector<SeaTunnelRow> output) throws Exception { | ||||
try { | ||||
if (pageInfoOptional.isPresent()) { | ||||
noMoreElementFlag = false; | ||||
Long pageIndex = 1L; | ||||
while (!noMoreElementFlag) { | ||||
PageInfo info = pageInfoOptional.get(); | ||||
// increment page | ||||
info.setPageIndex(pageIndex); | ||||
// set request param | ||||
updateRequestParam(info); | ||||
pollAndCollectData(output); | ||||
pageIndex += 1; | ||||
} | ||||
} else { | ||||
pollAndCollectData(output); | ||||
} | ||||
} catch (Exception e) { | ||||
log.error(e.getMessage(), e); | ||||
} finally { | ||||
if (Boundedness.BOUNDED.equals(context.getBoundedness())) { | ||||
if (Boundedness.BOUNDED.equals(context.getBoundedness()) && noMoreElementFlag) { | ||||
// signal to the source that we have reached the end of the data. | ||||
log.info("Closed the bounded http source"); | ||||
context.signalNoMoreElement(); | ||||
|
@@ -140,6 +194,21 @@ private void collect(Collector<SeaTunnelRow> output, String data) throws IOExcep | |||
this.initJsonPath(jsonField); | ||||
data = JsonUtils.toJsonNode(parseToMap(decodeJSON(data), jsonField)).toString(); | ||||
} | ||||
// page increase | ||||
if (pageInfoOptional.isPresent()) { | ||||
// Determine whether the task is completed by specifying the presence of the 'total | ||||
// page' field | ||||
PageInfo pageInfo = pageInfoOptional.get(); | ||||
if (pageInfo.getTotalPageSize() > 0) { | ||||
noMoreElementFlag = pageInfo.getPageIndex() >= pageInfo.getTotalPageSize(); | ||||
} else { | ||||
// no 'total page' configured | ||||
int readSize = JsonUtils.stringToJsonNode(data).size(); | ||||
// if read size < BatchSize : read finish | ||||
// if read size = BatchSize : read next page. | ||||
noMoreElementFlag = readSize < pageInfo.getBatchSize(); | ||||
} | ||||
} | ||||
deserializationCollector.collect(data.getBytes(), output); | ||||
} | ||||
|
||||
|
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
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.
Could you add two demo for different use way just like you added in e2e. Should including the data return by api fisrt. I think this way user can know how to use this feature easiler.