TyrantLucifer commented on code in PR #2555: URL: https://github.com/apache/incubator-seatunnel/pull/2555#discussion_r956970449
########## 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); + } + } + + @Override + public void finishAndCloseFile() { + beingWrittenOutputStream.forEach((key, value) -> { + try { + value.flush(); + } catch (IOException e) { + log.error("error when flush file {}", key); + throw new RuntimeException(e); + } finally { + try { + value.close(); + } catch (IOException e) { + log.error("error when close output stream {}", key); + } + } + + needMoveFiles.put(key, getTargetLocation(key)); + }); + } + + private FSDataOutputStream getOrCreateOutputStream(@NonNull String filePath) { + FSDataOutputStream fsDataOutputStream = beingWrittenOutputStream.get(filePath); + if (fsDataOutputStream == null) { + try { + fsDataOutputStream = FileSystemUtils.getOutputStream(filePath); + beingWrittenOutputStream.put(filePath, fsDataOutputStream); + } catch (IOException e) { + log.error("can not get output file stream"); + throw new RuntimeException(e); + } Review Comment: Only if the stream is not fetched properly will an exception be thrown here, so there is no need to explicitly close the stream here. ########## 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); + } + } + + @Override + public void finishAndCloseFile() { + beingWrittenOutputStream.forEach((key, value) -> { + try { + value.flush(); + } catch (IOException e) { + log.error("error when flush file {}", key); + throw new RuntimeException(e); + } finally { + try { + value.close(); + } catch (IOException e) { + log.error("error when close output stream {}", key); + } + } + + needMoveFiles.put(key, getTargetLocation(key)); + }); + } + + private FSDataOutputStream getOrCreateOutputStream(@NonNull String filePath) { + FSDataOutputStream fsDataOutputStream = beingWrittenOutputStream.get(filePath); + if (fsDataOutputStream == null) { + try { + fsDataOutputStream = FileSystemUtils.getOutputStream(filePath); + beingWrittenOutputStream.put(filePath, fsDataOutputStream); + } catch (IOException e) { + log.error("can not get output file stream"); + throw new RuntimeException(e); + } Review Comment: The close logic is added in finishAndCloseFile, until prepareCommit we should release resources. -- 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]
