EricJoy2048 commented on code in PR #2555:
URL: 
https://github.com/apache/incubator-seatunnel/pull/2555#discussion_r956868160


##########
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:
   Should we close the `fsDataOutputStream` in a `finally` code block?



##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/sink/writer/TextWriteStrategy.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.table.type.SeaTunnelRow;
+import 
org.apache.seatunnel.connectors.seatunnel.file.sink.config.TextFileSinkConfig;
+import 
org.apache.seatunnel.connectors.seatunnel.file.sink.util.FileSystemUtils;
+
+import lombok.NonNull;
+import org.apache.hadoop.fs.FSDataOutputStream;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class TextWriteStrategy extends AbstractWriteStrategy {
+    private final Map<String, FSDataOutputStream> beingWrittenOutputStream;
+    private final String fieldDelimiter;
+    private final String rowDelimiter;
+
+    public TextWriteStrategy(TextFileSinkConfig textFileSinkConfig) {
+        super(textFileSinkConfig);
+        this.beingWrittenOutputStream = new HashMap<>();
+        this.fieldDelimiter = textFileSinkConfig.getFieldDelimiter();
+        this.rowDelimiter = textFileSinkConfig.getRowDelimiter();
+    }
+
+    @Override
+    public void write(@NonNull SeaTunnelRow seaTunnelRow) {
+        String filePath = getOrCreateFilePathBeingWritten(seaTunnelRow);
+        FSDataOutputStream fsDataOutputStream = 
getOrCreateOutputStream(filePath);
+        String line = transformRowToLine(seaTunnelRow);
+        try {
+            fsDataOutputStream.write(line.getBytes());
+            fsDataOutputStream.write(rowDelimiter.getBytes());
+        } catch (IOException e) {
+            log.error("write data to file {} error", filePath);
+            throw new RuntimeException(e);

Review Comment:
   Same as above.



##########
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:
   Same as above and please check other places.



##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/sink/commit/FileCommitInfo2.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.commit;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.List;
+import java.util.Map;
+
+@Data
+@AllArgsConstructor
+public class FileCommitInfo2 implements Serializable {

Review Comment:
   Why we add a new file `FileCommitInfo2 ` instead of update the 
`FileCommitInfo` class?



##########
seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/sink/writer/Transaction.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.connectors.seatunnel.file.sink.commit.FileCommitInfo2;
+import 
org.apache.seatunnel.connectors.seatunnel.file.sink.state.FileSinkState2;
+
+import java.io.Serializable;
+import java.util.List;
+import java.util.Optional;
+
+public interface Transaction extends Serializable {

Review Comment:
   Some method be deleted, Can you explain why they were deleted?
   
   ```
   /**
        * A new transaction needs to be started after each checkpoint is 
completed.
        *
        * @param checkpointId A checkpoint indicates that all tasks have a 
status snapshot operation
        * @return transactionId
        */
       String beginTransaction(@NonNull Long checkpointId);
   
       /**
        * Abort current Transaction, called when {@link 
org.apache.seatunnel.connectors.seatunnel.file.sink.TransactionStateFileSinkWriter#prepareCommit()}
 or {@link 
org.apache.seatunnel.connectors.seatunnel.file.sink.TransactionStateFileSinkWriter#snapshotState(long)}
 failed
        */
       void abortTransaction();
   
       /**
        * Get all transactionIds after the @param transactionId
        * This method called when {@link 
AbstractFileSink#restoreWriter(SinkWriter.Context, List)}
        * We get the transactionId of the last successful commit from {@link 
FileSinkState} and
        * then all transactionIds after this transactionId is dirty 
transactions that need to be rollback.
        *
        * @param transactionId The transactionId of the last successful commit 
get from {@link FileSinkState}
        * @return transactionId list
        */
       List<String> getTransactionAfter(@NonNull String transactionId);
   
   
       /**
        * rollback the transaction which is not be commit
        *
        * @param transactionIds transactionIds
        */
       void abortTransactions(List<String> transactionIds);
   
   ```



-- 
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]

Reply via email to