CalvinKirs commented on code in PR #2273: URL: https://github.com/apache/incubator-seatunnel/pull/2273#discussion_r930642928
########## seatunnel-connectors-v2/connector-file/connector-file-hadoop/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/sink/hdfs/HdfsParquetTransactionStateFileWriter.java: ########## @@ -0,0 +1,175 @@ +/* + * 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.sink.hdfs; + +import org.apache.seatunnel.api.table.type.BasicType; +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.connectors.seatunnel.file.sink.spi.FileSystem; +import org.apache.seatunnel.connectors.seatunnel.file.sink.transaction.TransactionFileNameGenerator; +import org.apache.seatunnel.connectors.seatunnel.file.sink.writer.AbstractTransactionStateFileWriter; +import org.apache.seatunnel.connectors.seatunnel.file.sink.writer.PartitionDirNameGenerator; + +import lombok.NonNull; +import org.apache.avro.Schema; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericRecord; +import org.apache.avro.generic.GenericRecordBuilder; +import org.apache.hadoop.fs.Path; +import org.apache.parquet.avro.AvroParquetWriter; +import org.apache.parquet.column.ParquetProperties; +import org.apache.parquet.hadoop.ParquetFileWriter; +import org.apache.parquet.hadoop.ParquetWriter; +import org.apache.parquet.hadoop.metadata.CompressionCodecName; +import org.apache.parquet.hadoop.util.HadoopOutputFile; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class HdfsParquetTransactionStateFileWriter extends AbstractTransactionStateFileWriter { + private static final Logger LOGGER = LoggerFactory.getLogger(HdfsParquetTransactionStateFileWriter.class); + private Map<String, ParquetWriter<GenericRecord>> beingWrittenWriter; + + public HdfsParquetTransactionStateFileWriter(@NonNull SeaTunnelRowType seaTunnelRowTypeInfo, + @NonNull TransactionFileNameGenerator transactionFileNameGenerator, + @NonNull PartitionDirNameGenerator partitionDirNameGenerator, + @NonNull List<Integer> sinkColumnsIndexInRow, @NonNull String tmpPath, + @NonNull String targetPath, + @NonNull String jobId, + int subTaskIndex, + @NonNull FileSystem fileSystem) { + super(seaTunnelRowTypeInfo, transactionFileNameGenerator, partitionDirNameGenerator, sinkColumnsIndexInRow, tmpPath, targetPath, jobId, subTaskIndex, fileSystem); + beingWrittenWriter = new HashMap<>(); + } + + @Override + public void write(@NonNull SeaTunnelRow seaTunnelRow) { + String filePath = getOrCreateFilePathBeingWritten(seaTunnelRow); + ParquetWriter<GenericRecord> writer = getOrCreateWriter(filePath); + Schema schema = buildSchemaWithRowType(); + GenericRecordBuilder recordBuilder = new GenericRecordBuilder(schema); + sinkColumnsIndexInRow.forEach(index -> { + if (seaTunnelRowTypeInfo.getFieldType(index).equals(BasicType.STRING_TYPE)) { + recordBuilder.set(seaTunnelRowTypeInfo.getFieldName(index), seaTunnelRow.getField(index).toString()); + } else { + recordBuilder.set(seaTunnelRowTypeInfo.getFieldName(index), seaTunnelRow.getField(index)); + } + }); + GenericData.Record record = recordBuilder.build(); + try { + writer.write(record); + } catch (IOException e) { + LOGGER.error("Write data to file [{}] error", filePath); + throw new RuntimeException(e); Review Comment: If an exception needs to be thrown, then there is no need to use Log.error -- 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]
