Hisoka-X commented on code in PR #9064:
URL: https://github.com/apache/seatunnel/pull/9064#discussion_r2017957136
##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/source/reader/CsvReadStrategy.java:
##########
@@ -114,10 +120,18 @@ public void readProcess(
}
}
// read lines
+ List<String> headers = getHeaders(csvParser);
for (CSVRecord csvRecord : csvParser) {
HashMap<Integer, String> fieldIdValueMap = new HashMap<>();
- for (int i = 0; i <
inputCatalogTable.getTableSchema().getColumns().size(); i++) {
- fieldIdValueMap.put(i, csvRecord.get(i));
+ for (int i = 0; i < headers.size(); i++) {
+ // the user input schema may not contain all the columns
in the csv header
+ // and may contain columns in a different order with the
csv header
+ int index =
+
inputCatalogTable.getSeaTunnelRowType().indexOf(headers.get(i), false);
+ if (index == -1) {
+ continue;
+ }
+ fieldIdValueMap.put(index,
csvRecord.get(i).replace("\uFEFF", ""));
Review Comment:
\uFEFF only appear with line start, so we can check with method, then remove
it if existed.
```java
if (line != null && line.startsWith("\uFEFF")) {
line = line.substring(1); // remove BOM
}
```
##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/source/reader/CsvReadStrategy.java:
##########
@@ -152,6 +166,22 @@ public void readProcess(
}
}
+ private List<String> getHeaders(CSVParser csvParser) {
+ List<String> headers;
+ if (firstLineAsHeader) {
+ headers =
+ csvParser.getHeaderNames().stream()
+ .map(header -> header.replace("\"",
"").replace("\uFEFF", "").trim())
Review Comment:
ditto
--
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]