TyrantLucifer commented on code in PR #2555: URL: https://github.com/apache/incubator-seatunnel/pull/2555#discussion_r956972347
########## seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/sink/writer/JsonWriteStrategy.java: ########## @@ -0,0 +1,98 @@ +/* + * 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.writer; + +import org.apache.seatunnel.api.serialization.SerializationSchema; +import org.apache.seatunnel.api.table.type.SeaTunnelRow; +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; +import org.apache.seatunnel.connectors.seatunnel.file.sink.config.TextFileSinkConfig; +import org.apache.seatunnel.connectors.seatunnel.file.sink.util.FileSystemUtils; +import org.apache.seatunnel.format.json.JsonSerializationSchema; + +import lombok.NonNull; +import org.apache.hadoop.fs.FSDataOutputStream; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class JsonWriteStrategy extends AbstractWriteStrategy { + private final byte[] rowDelimiter; + private SerializationSchema serializationSchema; + private final Map<String, FSDataOutputStream> beingWrittenOutputStream; + + public JsonWriteStrategy(TextFileSinkConfig textFileSinkConfig) { + super(textFileSinkConfig); + this.beingWrittenOutputStream = new HashMap<>(); + this.rowDelimiter = textFileSinkConfig.getRowDelimiter().getBytes(); + } + + @Override + public void setSeaTunnelRowTypeInfo(SeaTunnelRowType seaTunnelRowType) { + super.setSeaTunnelRowTypeInfo(seaTunnelRowType); + this.serializationSchema = new JsonSerializationSchema(seaTunnelRowType); + } + + @Override + public void write(@NonNull SeaTunnelRow seaTunnelRow) { + String filePath = getOrCreateFilePathBeingWritten(seaTunnelRow); + FSDataOutputStream fsDataOutputStream = getOrCreateOutputStream(filePath); + try { + byte[] rowBytes = serializationSchema.serialize(seaTunnelRow); + fsDataOutputStream.write(rowBytes); + fsDataOutputStream.write(rowDelimiter); + } catch (IOException e) { + log.error("write data to file {} error", filePath); + throw new RuntimeException(e); + } Review Comment: If the program throws an exception here, the stream will automatically close and release resources, and if we continue to close at the catch level, we need to catch IOException again, which will be endless. -- 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]
