forked from DTStack/chunjun
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DTStack#1494 [feat-1494][http] http support array data (DTStack#1495)
- Loading branch information
1 parent
de5b214
commit 58fbb3b
Showing
17 changed files
with
834 additions
and
76 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
84 changes: 84 additions & 0 deletions
84
...nector-http/src/main/java/com/dtstack/chunjun/connector/http/client/CsvResponseParse.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,84 @@ | ||
/* | ||
* 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 com.dtstack.chunjun.connector.http.client; | ||
|
||
import com.dtstack.chunjun.connector.http.common.HttpRestConfig; | ||
import com.dtstack.chunjun.converter.AbstractRowConverter; | ||
|
||
import com.csvreader.CsvReader; | ||
import org.apache.commons.collections.CollectionUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class CsvResponseParse extends ResponseParse { | ||
private CsvReader csvReader; | ||
private Reader reader; | ||
private String responseValue; | ||
private HttpRequestParam requestParam; | ||
|
||
public CsvResponseParse(HttpRestConfig config, AbstractRowConverter converter) { | ||
super(config, converter); | ||
if (CollectionUtils.isEmpty(columns)) { | ||
throw new RuntimeException("please configure column when decode is csv"); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean hasNext() throws IOException { | ||
return csvReader.readRecord(); | ||
} | ||
|
||
@Override | ||
public ResponseValue next() throws Exception { | ||
String[] data = csvReader.getValues(); | ||
HashMap<String, Object> stringObjectHashMap = new HashMap<>(columns.size()); | ||
for (int i = 0; i < columns.size(); i++) { | ||
if (columns.get(i).getValue() != null) { | ||
stringObjectHashMap.put(columns.get(i).getName(), columns.get(i).getValue()); | ||
} else { | ||
stringObjectHashMap.put(columns.get(i).getName(), data[i]); | ||
} | ||
} | ||
|
||
return new ResponseValue( | ||
converter.toInternal(stringObjectHashMap), requestParam, responseValue); | ||
} | ||
|
||
@Override | ||
public void parse(String responseValue, int responseStatus, HttpRequestParam requestParam) { | ||
this.responseValue = responseValue; | ||
this.requestParam = requestParam; | ||
this.reader = new StringReader(responseValue); | ||
this.csvReader = new CsvReader(reader); | ||
csvReader.setDelimiter(config.getCsvDelimiter().charAt(0)); | ||
|
||
Map<String, Object> csvConfig = config.getCsvConfig(); | ||
// 是否跳过空行 | ||
csvReader.setSkipEmptyRecords((Boolean) csvConfig.getOrDefault("skipEmptyRecords", true)); | ||
// 是否使用csv转义字符 | ||
csvReader.setUseTextQualifier((Boolean) csvConfig.getOrDefault("useTextQualifier", true)); | ||
csvReader.setTrimWhitespace((Boolean) csvConfig.getOrDefault("trimWhitespace", false)); | ||
// 单列长度是否限制100000字符 | ||
csvReader.setSafetySwitch((Boolean) csvConfig.getOrDefault("safetySwitch", false)); | ||
} | ||
} |
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
121 changes: 121 additions & 0 deletions
121
...ector-http/src/main/java/com/dtstack/chunjun/connector/http/client/JsonResponseParse.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,121 @@ | ||
/* | ||
* 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 com.dtstack.chunjun.connector.http.client; | ||
|
||
import com.dtstack.chunjun.conf.FieldConf; | ||
import com.dtstack.chunjun.connector.http.common.HttpRestConfig; | ||
import com.dtstack.chunjun.connector.http.util.JsonPathUtil; | ||
import com.dtstack.chunjun.constants.ConstantValue; | ||
import com.dtstack.chunjun.converter.AbstractRowConverter; | ||
import com.dtstack.chunjun.util.GsonUtil; | ||
import com.dtstack.chunjun.util.MapUtil; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.google.gson.Gson; | ||
import org.apache.commons.collections.CollectionUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class JsonResponseParse extends ResponseParse { | ||
private String responseValue; | ||
private HttpRequestParam requestParam; | ||
private final Gson gson; | ||
private final List<FieldConf> fields; | ||
private Iterator<Map<String, Object>> iterator; | ||
|
||
public JsonResponseParse(HttpRestConfig config, AbstractRowConverter converter) { | ||
super(config, converter); | ||
this.gson = GsonUtil.setTypeAdapter(new Gson()); | ||
if (StringUtils.isNotBlank(config.getFields())) { | ||
fields = | ||
Arrays.stream(config.getFields().split(",")) | ||
.map( | ||
i -> { | ||
FieldConf fieldConf = new FieldConf(); | ||
fieldConf.setName(i); | ||
return fieldConf; | ||
}) | ||
.collect(Collectors.toList()); | ||
} else { | ||
fields = null; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean hasNext() throws IOException { | ||
return iterator.hasNext(); | ||
} | ||
|
||
@Override | ||
public ResponseValue next() throws Exception { | ||
Map next = iterator.next(); | ||
|
||
if (CollectionUtils.isEmpty(columns)) { | ||
if (CollectionUtils.isNotEmpty(fields)) { | ||
LinkedHashMap<String, Object> map = | ||
buildResponseByKey(next, fields, ConstantValue.POINT_SYMBOL); | ||
HashMap<String, Object> data = new HashMap<>(); | ||
// 需要拆分key | ||
((Map<String, Object>) map) | ||
.forEach( | ||
(k, v) -> { | ||
MapUtil.buildMap(k, ConstantValue.POINT_SYMBOL, v, data); | ||
}); | ||
return new ResponseValue(converter.toInternal(data), requestParam, responseValue); | ||
} else { | ||
return new ResponseValue(converter.toInternal(next), requestParam, responseValue); | ||
} | ||
|
||
} else { | ||
LinkedHashMap<String, Object> data = | ||
buildResponseByKey(next, columns, ConstantValue.POINT_SYMBOL); | ||
return new ResponseValue(converter.toInternal(data), requestParam, responseValue); | ||
} | ||
} | ||
|
||
@Override | ||
public void parse(String responseValue, int responseStatus, HttpRequestParam requestParam) { | ||
this.responseValue = responseValue; | ||
this.requestParam = requestParam; | ||
|
||
Map<String, Object> map = gson.fromJson(responseValue, GsonUtil.gsonMapTypeToken); | ||
if (StringUtils.isNotBlank(config.getDataSubject())) { | ||
Object valueByKey = | ||
MapUtil.getValueByKey( | ||
map, | ||
JsonPathUtil.parseJsonPath(config.getDataSubject()), | ||
ConstantValue.POINT_SYMBOL); | ||
if (valueByKey instanceof List) { | ||
this.iterator = ((List) valueByKey).iterator(); | ||
} else { | ||
throw new RuntimeException(config.getDataSubject() + " in response is not array"); | ||
} | ||
} else { | ||
this.iterator = Lists.newArrayList(map).iterator(); | ||
} | ||
} | ||
} |
Oops, something went wrong.