Skip to content

Commit 3779e9b

Browse files
author
ocean-zhc
committed
connect-http-base Add a new parameter json_field_missed_return_null to let the user control whether to report an error or empty padding.
1 parent 8079535 commit 3779e9b

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

Diff for: docs/en/connector-v2/source/Http.md

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ They can be downloaded via install-plugin.sh or from the Maven central repositor
7171
| common-options | | No | - | Source plugin common parameters, please refer to [Source Common Options](../source-common-options.md) for details |
7272
| keep_params_as_form | Boolean | No | false | Whether the params are submitted according to the form, used for compatibility with legacy behaviors. When true, the value of the params parameter is submitted through the form. |
7373
| keep_page_param_as_http_param | Boolean | No | false | Whether to set the paging parameters to params. For compatibility with legacy behaviors. |
74+
| json_filed_missed_return_null | Boolean | No | false | When the json field is missing, set true return null else error.
75+
7476

7577
## How to Create a Http Data Synchronization Jobs
7678

Diff for: seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/config/HttpParameter.java

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class HttpParameter implements Serializable {
4545
protected boolean enableMultilines;
4646
protected int connectTimeoutMs;
4747
protected int socketTimeoutMs;
48+
protected boolean jsonFiledMissedReturnNull;
4849

4950
public void buildWithConfig(ReadonlyConfig pluginConfig) {
5051
// set url
@@ -90,5 +91,7 @@ public void buildWithConfig(ReadonlyConfig pluginConfig) {
9091
this.setEnableMultilines(pluginConfig.get(HttpSourceOptions.ENABLE_MULTI_LINES));
9192
this.setConnectTimeoutMs(pluginConfig.get(HttpSourceOptions.CONNECT_TIMEOUT_MS));
9293
this.setSocketTimeoutMs(pluginConfig.get(HttpSourceOptions.SOCKET_TIMEOUT_MS));
94+
this.setJsonFiledMissedReturnNull(
95+
pluginConfig.get(HttpSourceOptions.JSON_FILED_MISSED_RETURN_NULL));
9396
}
9497
}

Diff for: seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/config/HttpSourceOptions.java

+6
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,10 @@ public class HttpSourceOptions extends HttpCommonOptions {
117117
.intType()
118118
.defaultValue(DEFAULT_SOCKET_TIMEOUT_MS)
119119
.withDescription("Socket timeout setting, default 60s.");
120+
121+
public static final Option<Boolean> JSON_FILED_MISSED_RETURN_NULL =
122+
Options.key("json_filed_missed_return_null")
123+
.booleanType()
124+
.defaultValue(false)
125+
.withDescription("When the json field is missing, return null");
120126
}

Diff for: seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/source/HttpSourceReader.java

+32-16
Original file line numberDiff line numberDiff line change
@@ -262,25 +262,41 @@ private List<List<String>> decodeJSON(String data) {
262262
List<String> result = jsonReadContext.read(path);
263263
results.add(result);
264264
}
265-
int maxLength = 0;
266-
for (List<?> result : results) {
267-
maxLength = Math.max(maxLength, result.size());
268-
}
269-
for (int i = 0; i < results.size(); i++) {
270-
List<String> result = results.get(i);
271-
if (result.size() < maxLength) {
272-
log.warn(
273-
"Field [{}] with size ({}) is less than max size ({}), will be padded with null values. "
274-
+ "This may happen when JSON paths return different numbers of elements.",
275-
jsonPaths[i].getPath(),
276-
result.size(),
277-
maxLength);
278-
for (int j = result.size(); j < maxLength; j++) {
279-
result.add(null);
265+
if (httpParameter.isJsonFiledMissedReturnNull()) {
266+
int maxLength = 0;
267+
for (List<?> result : results) {
268+
maxLength = Math.max(maxLength, result.size());
269+
}
270+
for (int i = 0; i < results.size(); i++) {
271+
List<String> result = results.get(i);
272+
if (result.size() < maxLength) {
273+
log.warn(
274+
"Field [{}] with size ({}) is less than max size ({}), will be padded with null values. "
275+
+ "This may happen when JSON paths return different numbers of elements.",
276+
jsonPaths[i].getPath(),
277+
result.size(),
278+
maxLength);
279+
for (int j = result.size(); j < maxLength; j++) {
280+
result.add(null);
281+
}
282+
}
283+
}
284+
} else {
285+
for (int i = 1; i < results.size(); i++) {
286+
List<?> result0 = results.get(0);
287+
List<?> result = results.get(i);
288+
if (result0.size() != result.size()) {
289+
throw new HttpConnectorException(
290+
HttpConnectorErrorCode.FIELD_DATA_IS_INCONSISTENT,
291+
String.format(
292+
"[%s](%d) and [%s](%d) the number of parsing records is inconsistent.",
293+
jsonPaths[0].getPath(),
294+
result0.size(),
295+
jsonPaths[i].getPath(),
296+
result.size()));
280297
}
281298
}
282299
}
283-
284300
return dataFlip(results);
285301
}
286302

0 commit comments

Comments
 (0)