TyrantLucifer commented on code in PR #3510:
URL:
https://github.com/apache/incubator-seatunnel/pull/3510#discussion_r1035614518
##########
seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/source/HttpSourceReader.java:
##########
@@ -84,4 +110,84 @@ public void pollNext(Collector<SeaTunnelRow> output) throws
Exception {
}
}
}
+
+ private List<Map<String, String>> parseToMap(List<List<String>> datas,
JsonField jsonField) {
+ List<Map<String, String>> decodeDatas = new
ArrayList<>(datas.get(0).size());
+ String[] keys = jsonField.getFields().keySet().toArray(new String[]{});
+ for (int index = 0; index < jsonField.getFields().size(); index++) {
+ int finalIndex = index;
+ datas.get(index).forEach(field -> {
+ if (decodeDatas.isEmpty() || decodeDatas.size() !=
datas.get(0).size()) {
+ Map<String, String> decodeData = new
HashMap<>(jsonField.getFields().size());
+ decodeData.put(keys[finalIndex], field);
+ decodeDatas.add(decodeData);
+ } else {
+ decodeDatas.forEach(decodeData -> {
+ decodeData.put(keys[finalIndex], field);
+ });
+ }
+ });
+ }
+
+ return decodeDatas;
+ }
+
+ private List<List<String>> decodeJSON(String data) {
+ ReadContext jsonReadContext =
JsonPath.using(jsonConfiguration).parse(data);
+ List<List<String>> results = new ArrayList<>(jsonPaths.length);
+ for (JsonPath path : jsonPaths) {
+ List<String> result = jsonReadContext.read(path);
+ results.add(result);
+ }
+ for (int i = 1; i < results.size(); i++) {
+ List<?> result0 = results.get(0);
+ List<?> result = results.get(i);
+ if (result0.size() != result.size()) {
+ throw new SeaTunnelException(
Review Comment:
Use HttpConnectorException
##########
seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/source/HttpSourceReader.java:
##########
@@ -84,4 +110,84 @@ public void pollNext(Collector<SeaTunnelRow> output) throws
Exception {
}
}
}
+
+ private List<Map<String, String>> parseToMap(List<List<String>> datas,
JsonField jsonField) {
+ List<Map<String, String>> decodeDatas = new
ArrayList<>(datas.get(0).size());
+ String[] keys = jsonField.getFields().keySet().toArray(new String[]{});
+ for (int index = 0; index < jsonField.getFields().size(); index++) {
+ int finalIndex = index;
+ datas.get(index).forEach(field -> {
+ if (decodeDatas.isEmpty() || decodeDatas.size() !=
datas.get(0).size()) {
+ Map<String, String> decodeData = new
HashMap<>(jsonField.getFields().size());
+ decodeData.put(keys[finalIndex], field);
+ decodeDatas.add(decodeData);
+ } else {
+ decodeDatas.forEach(decodeData -> {
+ decodeData.put(keys[finalIndex], field);
+ });
+ }
+ });
+ }
+
+ return decodeDatas;
+ }
+
+ private List<List<String>> decodeJSON(String data) {
+ ReadContext jsonReadContext =
JsonPath.using(jsonConfiguration).parse(data);
+ List<List<String>> results = new ArrayList<>(jsonPaths.length);
+ for (JsonPath path : jsonPaths) {
+ List<String> result = jsonReadContext.read(path);
+ results.add(result);
+ }
+ for (int i = 1; i < results.size(); i++) {
+ List<?> result0 = results.get(0);
+ List<?> result = results.get(i);
+ if (result0.size() != result.size()) {
+ throw new SeaTunnelException(
+ String.format(
+ "[%s](%d) and [%s](%d) the number of parsing records
is inconsistent.",
+ jsonPaths[0].getPath(), result0.size(),
jsonPaths[i].getPath(), result.size()));
+ }
+ }
+
+ return dataFlip(results);
+ }
+
+ private String getPartOfJson(String data) {
+ ReadContext jsonReadContext =
JsonPath.using(jsonConfiguration).parse(data);
+ return
JsonUtils.toJsonString(jsonReadContext.read(JsonPath.compile(contentJson)));
+ }
+
+ private List<List<String>> dataFlip(List<List<String>> results) {
+
+ List<List<String>> datas = new ArrayList<>();
+ for (int i = 0; i < results.size(); i++) {
+ List<String> result = results.get(i);
+ if (i == 0) {
+ for (Object o : result) {
+ String val = o == null ? null : o.toString();
+ List<String> row = new ArrayList<>(jsonPaths.length);
+ row.add(val);
+ datas.add(row);
+ }
+ } else {
+ for (int j = 0; j < result.size(); j++) {
+ Object o = result.get(j);
+ String val = o == null ? null : o.toString();
+ List<String> row = datas.get(j);
+ row.add(val);
+ }
+ }
+ }
+ return datas;
+ }
+
+ private void initJsonPath(JsonField jsonField) {
+ jsonPaths = new JsonPath[jsonField.getFields().size()];
+ final int[] index = {0};
+ jsonField.getFields().forEach((key, value) -> {
Review Comment:
Use `entrySet` is better, `final int[] index = {0};` make the code not clear
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]