Hisoka-X commented on code in PR #8064:
URL: https://github.com/apache/seatunnel/pull/8064#discussion_r1865183048


##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/config/BaseSourceConfigOptions.java:
##########
@@ -134,6 +134,12 @@ public class BaseSourceConfigOptions {
                     .noDefaultValue()
                     .withDescription("To be read sheet name,only valid for 
excel files");
 
+    public static final Option<ExcelEngine> EXCEL_ENGINE =
+            Options.key("excel_engine")
+                    .objectType(ExcelEngine.class)

Review Comment:
   ```suggestion
                       .enumType(ExcelEngine.class)
   ```



##########
seatunnel-connectors-v2/connector-file/connector-file-base/pom.xml:
##########
@@ -158,6 +160,19 @@
             <artifactId>jaxen</artifactId>
             <version>${jaxen.version}</version>
         </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>easyexcel</artifactId>
+            <version>${easyexcel.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.dhatim</groupId>
+            <artifactId>fastexcel-reader</artifactId>
+            <version>${fastexcel-reader.version}</version>
+        </dependency>

Review Comment:
   Please remove this dependency because we don't use it at now.



##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/excel/ExcelReaderListener.java:
##########
@@ -0,0 +1,277 @@
+/*
+ * 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.file.excel;
+
+import 
org.apache.seatunnel.shade.com.fasterxml.jackson.core.JsonProcessingException;
+import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import org.apache.seatunnel.api.configuration.ReadonlyConfig;
+import org.apache.seatunnel.api.source.Collector;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.api.table.type.SqlType;
+import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated;
+import org.apache.seatunnel.common.utils.DateTimeUtils;
+import org.apache.seatunnel.common.utils.DateUtils;
+import org.apache.seatunnel.common.utils.TimeUtils;
+import 
org.apache.seatunnel.connectors.seatunnel.file.config.BaseSourceConfigOptions;
+import 
org.apache.seatunnel.connectors.seatunnel.file.exception.FileConnectorException;
+
+import org.apache.poi.ss.usermodel.DateUtil;
+
+import com.alibaba.excel.context.AnalysisContext;
+import com.alibaba.excel.enums.CellDataTypeEnum;
+import com.alibaba.excel.event.AnalysisEventListener;
+import com.alibaba.excel.exception.ExcelDataConvertException;
+import com.alibaba.excel.metadata.Cell;
+import com.alibaba.excel.metadata.data.ReadCellData;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.nio.charset.StandardCharsets;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+@Slf4j
+public class ExcelReaderListener extends AnalysisEventListener<Map<Integer, 
Object>>
+        implements Serializable, Closeable {
+    private final String tableId;
+    private final Collector<SeaTunnelRow> output;
+    private int cellCount;
+
+    private final ObjectMapper objectMapper = new ObjectMapper();
+
+    private DateTimeFormatter dateFormatter;
+    private DateTimeFormatter dateTimeFormatter;
+    private DateTimeFormatter timeFormatter;
+
+    protected Config pluginConfig;
+
+    protected SeaTunnelRowType seaTunnelRowType;
+
+    private SeaTunnelDataType<?>[] fieldTypes;
+
+    Map<Integer, String> customHeaders = new HashMap<>();
+
+    public ExcelReaderListener(
+            String tableId,
+            Collector<SeaTunnelRow> output,
+            Config pluginConfig,
+            SeaTunnelRowType seaTunnelRowType) {
+        this.tableId = tableId;
+        this.output = output;
+        this.pluginConfig = pluginConfig;
+        this.seaTunnelRowType = seaTunnelRowType;
+
+        fieldTypes = seaTunnelRowType.getFieldTypes();
+
+        if (pluginConfig.hasPath(BaseSourceConfigOptions.DATE_FORMAT.key())) {
+            String dateFormatString =
+                    
pluginConfig.getString(BaseSourceConfigOptions.DATE_FORMAT.key());
+            dateFormatter = DateTimeFormatter.ofPattern(dateFormatString);
+        }
+        if 
(pluginConfig.hasPath(BaseSourceConfigOptions.DATETIME_FORMAT.key())) {
+            String datetimeFormatString =
+                    
pluginConfig.getString(BaseSourceConfigOptions.DATETIME_FORMAT.key());
+            dateTimeFormatter = 
DateTimeFormatter.ofPattern(datetimeFormatString);
+        }
+        if (pluginConfig.hasPath(BaseSourceConfigOptions.TIME_FORMAT.key())) {
+            String timeFormatString =
+                    
pluginConfig.getString(BaseSourceConfigOptions.TIME_FORMAT.key());
+            timeFormatter = DateTimeFormatter.ofPattern(timeFormatString);
+        }
+    }
+
+    @Override
+    public void invokeHead(Map<Integer, ReadCellData<?>> headMap, 
AnalysisContext context) {
+        for (int i = 0; i < headMap.size(); i++) {
+            String header = headMap.get(i).getStringValue();
+            if (!"null".equals(header)) {
+                customHeaders.put(i, header);
+            }
+        }
+    }
+
+    @Override
+    public void invoke(Map<Integer, Object> data, AnalysisContext context) {
+        cellCount = data.size();
+        SeaTunnelRow seaTunnelRow = new SeaTunnelRow(fieldTypes.length);
+        Map<Integer, Cell> cellMap = context.readRowHolder().getCellMap();
+        int i = 0;
+        for (; i < fieldTypes.length; i++) {
+            if (cellMap.get(i) == null) {
+                seaTunnelRow.setField(i, null);
+            } else {
+                Object cell = convert(data.get(i), cellMap.get(i), 
fieldTypes[i]);
+                seaTunnelRow.setField(i, cell);
+            }
+        }
+        seaTunnelRow.setTableId(tableId);
+        output.collect(seaTunnelRow);
+    }
+
+    @Override
+    public void doAfterAllAnalysed(AnalysisContext context) {
+        log.info("excel parsing completed");
+    }
+
+    @Override
+    public void onException(Exception exception, AnalysisContext context) {
+        log.error("cell parsing exception :{}", exception.getMessage());
+        if (exception instanceof ExcelDataConvertException) {
+            ExcelDataConvertException excelDataConvertException =
+                    (ExcelDataConvertException) exception;
+            log.error(

Review Comment:
   Reduce big log message.
   ```suggestion
               log.debug(
   ```



##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/excel/ExcelReaderListener.java:
##########
@@ -0,0 +1,277 @@
+/*
+ * 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.file.excel;
+
+import 
org.apache.seatunnel.shade.com.fasterxml.jackson.core.JsonProcessingException;
+import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import org.apache.seatunnel.api.configuration.ReadonlyConfig;
+import org.apache.seatunnel.api.source.Collector;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.api.table.type.SqlType;
+import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated;
+import org.apache.seatunnel.common.utils.DateTimeUtils;
+import org.apache.seatunnel.common.utils.DateUtils;
+import org.apache.seatunnel.common.utils.TimeUtils;
+import 
org.apache.seatunnel.connectors.seatunnel.file.config.BaseSourceConfigOptions;
+import 
org.apache.seatunnel.connectors.seatunnel.file.exception.FileConnectorException;
+
+import org.apache.poi.ss.usermodel.DateUtil;
+
+import com.alibaba.excel.context.AnalysisContext;
+import com.alibaba.excel.enums.CellDataTypeEnum;
+import com.alibaba.excel.event.AnalysisEventListener;
+import com.alibaba.excel.exception.ExcelDataConvertException;
+import com.alibaba.excel.metadata.Cell;
+import com.alibaba.excel.metadata.data.ReadCellData;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.nio.charset.StandardCharsets;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+@Slf4j
+public class ExcelReaderListener extends AnalysisEventListener<Map<Integer, 
Object>>
+        implements Serializable, Closeable {
+    private final String tableId;
+    private final Collector<SeaTunnelRow> output;
+    private int cellCount;
+
+    private final ObjectMapper objectMapper = new ObjectMapper();
+
+    private DateTimeFormatter dateFormatter;
+    private DateTimeFormatter dateTimeFormatter;
+    private DateTimeFormatter timeFormatter;
+
+    protected Config pluginConfig;
+
+    protected SeaTunnelRowType seaTunnelRowType;
+
+    private SeaTunnelDataType<?>[] fieldTypes;
+
+    Map<Integer, String> customHeaders = new HashMap<>();
+
+    public ExcelReaderListener(
+            String tableId,
+            Collector<SeaTunnelRow> output,
+            Config pluginConfig,
+            SeaTunnelRowType seaTunnelRowType) {
+        this.tableId = tableId;
+        this.output = output;
+        this.pluginConfig = pluginConfig;
+        this.seaTunnelRowType = seaTunnelRowType;
+
+        fieldTypes = seaTunnelRowType.getFieldTypes();
+
+        if (pluginConfig.hasPath(BaseSourceConfigOptions.DATE_FORMAT.key())) {
+            String dateFormatString =
+                    
pluginConfig.getString(BaseSourceConfigOptions.DATE_FORMAT.key());
+            dateFormatter = DateTimeFormatter.ofPattern(dateFormatString);
+        }
+        if 
(pluginConfig.hasPath(BaseSourceConfigOptions.DATETIME_FORMAT.key())) {
+            String datetimeFormatString =
+                    
pluginConfig.getString(BaseSourceConfigOptions.DATETIME_FORMAT.key());
+            dateTimeFormatter = 
DateTimeFormatter.ofPattern(datetimeFormatString);
+        }
+        if (pluginConfig.hasPath(BaseSourceConfigOptions.TIME_FORMAT.key())) {
+            String timeFormatString =
+                    
pluginConfig.getString(BaseSourceConfigOptions.TIME_FORMAT.key());
+            timeFormatter = DateTimeFormatter.ofPattern(timeFormatString);
+        }
+    }
+
+    @Override
+    public void invokeHead(Map<Integer, ReadCellData<?>> headMap, 
AnalysisContext context) {
+        for (int i = 0; i < headMap.size(); i++) {
+            String header = headMap.get(i).getStringValue();
+            if (!"null".equals(header)) {
+                customHeaders.put(i, header);
+            }
+        }
+    }
+
+    @Override
+    public void invoke(Map<Integer, Object> data, AnalysisContext context) {
+        cellCount = data.size();
+        SeaTunnelRow seaTunnelRow = new SeaTunnelRow(fieldTypes.length);
+        Map<Integer, Cell> cellMap = context.readRowHolder().getCellMap();
+        int i = 0;
+        for (; i < fieldTypes.length; i++) {
+            if (cellMap.get(i) == null) {
+                seaTunnelRow.setField(i, null);
+            } else {
+                Object cell = convert(data.get(i), cellMap.get(i), 
fieldTypes[i]);
+                seaTunnelRow.setField(i, cell);
+            }
+        }
+        seaTunnelRow.setTableId(tableId);
+        output.collect(seaTunnelRow);
+    }
+
+    @Override
+    public void doAfterAllAnalysed(AnalysisContext context) {
+        log.info("excel parsing completed");
+    }
+
+    @Override
+    public void onException(Exception exception, AnalysisContext context) {
+        log.error("cell parsing exception :{}", exception.getMessage());
+        if (exception instanceof ExcelDataConvertException) {
+            ExcelDataConvertException excelDataConvertException =
+                    (ExcelDataConvertException) exception;
+            log.error(
+                    "row:{},cell:{},data:{}",
+                    excelDataConvertException.getRowIndex(),
+                    excelDataConvertException.getColumnIndex(),
+                    excelDataConvertException.getCellData());
+        }
+    }
+
+    private String getCellValue(ReadCellData cellData) {
+
+        if (cellData.getStringValue() != null) {
+            return cellData.getStringValue();
+        } else if (cellData.getNumberValue() != null) {
+            return cellData.getNumberValue().toString();
+        } else if (cellData.getOriginalNumberValue() != null) {
+            return cellData.getOriginalNumberValue().toString();
+        } else if (cellData.getBooleanValue() != null) {
+            return cellData.getBooleanValue().toString();
+        } else if (cellData.getType() == CellDataTypeEnum.EMPTY) {
+            return "";
+        }
+        return null;
+    }
+
+    @SneakyThrows(JsonProcessingException.class)
+    private Object convert(Object field, Cell cellRaw, SeaTunnelDataType<?> 
fieldType) {
+
+        if (cellRaw == null && field == null) {
+            return null;
+        }
+        String cellValue = (String) field;
+        ReadCellData cellData = (ReadCellData) cellRaw;
+        if (cellRaw != null) {
+            cellValue = getCellValue(cellData);
+        }
+        SqlType sqlType = fieldType.getSqlType();
+
+        if (cellValue == null || (cellValue.equals("") && sqlType != 
SqlType.STRING)) {
+            return null;
+        }
+
+        switch (sqlType) {

Review Comment:
   can we reuse 
https://github.com/apache/seatunnel/pull/8064/files#diff-f7eb796028f745bd57298e931d7712c0105c5e1e09fb6679fd7c07bb2febe211R243
 ?



-- 
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: commits-unsubscr...@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to