|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.connectors.seatunnel.file.sink.local; |
| 19 | + |
| 20 | +import org.apache.seatunnel.api.serialization.SerializationSchema; |
| 21 | +import org.apache.seatunnel.api.table.type.SeaTunnelRow; |
| 22 | +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; |
| 23 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.spi.FileSystem; |
| 24 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.transaction.TransactionFileNameGenerator; |
| 25 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.writer.AbstractTransactionStateFileWriter; |
| 26 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.writer.PartitionDirNameGenerator; |
| 27 | +import org.apache.seatunnel.format.json.JsonSerializationSchema; |
| 28 | + |
| 29 | +import lombok.NonNull; |
| 30 | +import lombok.extern.slf4j.Slf4j; |
| 31 | + |
| 32 | +import java.io.FileOutputStream; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +@Slf4j |
| 39 | +public class LocalJsonTransactionStateFileWriter extends AbstractTransactionStateFileWriter { |
| 40 | + |
| 41 | + private static final long serialVersionUID = -3834472539886339383L; |
| 42 | + |
| 43 | + private final byte[] rowDelimiter; |
| 44 | + private final SerializationSchema serializationSchema; |
| 45 | + private Map<String, FileOutputStream> beingWrittenOutputStream; |
| 46 | + |
| 47 | + public LocalJsonTransactionStateFileWriter(@NonNull SeaTunnelRowType seaTunnelRowTypeInfo, |
| 48 | + @NonNull TransactionFileNameGenerator transactionFileNameGenerator, |
| 49 | + @NonNull PartitionDirNameGenerator partitionDirNameGenerator, |
| 50 | + @NonNull List<Integer> sinkColumnsIndexInRow, |
| 51 | + @NonNull String tmpPath, |
| 52 | + @NonNull String targetPath, |
| 53 | + @NonNull String jobId, |
| 54 | + int subTaskIndex, |
| 55 | + @NonNull String rowDelimiter, |
| 56 | + @NonNull FileSystem fileSystem) { |
| 57 | + super(seaTunnelRowTypeInfo, transactionFileNameGenerator, partitionDirNameGenerator, sinkColumnsIndexInRow, tmpPath, targetPath, jobId, subTaskIndex, fileSystem); |
| 58 | + |
| 59 | + this.rowDelimiter = rowDelimiter.getBytes(); |
| 60 | + this.serializationSchema = new JsonSerializationSchema(seaTunnelRowTypeInfo); |
| 61 | + this.beingWrittenOutputStream = new HashMap<>(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void beginTransaction(String transactionId) { |
| 66 | + this.beingWrittenOutputStream = new HashMap<>(); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void abortTransaction(String transactionId) { |
| 71 | + this.beingWrittenOutputStream = new HashMap<>(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void write(@NonNull SeaTunnelRow seaTunnelRow) { |
| 76 | + String filePath = getOrCreateFilePathBeingWritten(seaTunnelRow); |
| 77 | + FileOutputStream fileOutputStream = getOrCreateOutputStream(filePath); |
| 78 | + try { |
| 79 | + byte[] rowBytes = serializationSchema.serialize(seaTunnelRow); |
| 80 | + fileOutputStream.write(rowBytes); |
| 81 | + fileOutputStream.write(rowDelimiter); |
| 82 | + } catch (IOException e) { |
| 83 | + log.error("write data to file {} error", filePath); |
| 84 | + throw new RuntimeException(e); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void finishAndCloseWriteFile() { |
| 90 | + beingWrittenOutputStream.entrySet().forEach(entry -> { |
| 91 | + try { |
| 92 | + entry.getValue().flush(); |
| 93 | + } catch (IOException e) { |
| 94 | + log.error("error when flush file {}", entry.getKey()); |
| 95 | + throw new RuntimeException(e); |
| 96 | + } finally { |
| 97 | + try { |
| 98 | + entry.getValue().close(); |
| 99 | + } catch (IOException e) { |
| 100 | + log.error("error when close output stream {}", entry.getKey()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + needMoveFiles.put(entry.getKey(), getTargetLocation(entry.getKey())); |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + private FileOutputStream getOrCreateOutputStream(@NonNull String filePath) { |
| 109 | + FileOutputStream fileOutputStream = beingWrittenOutputStream.get(filePath); |
| 110 | + if (fileOutputStream == null) { |
| 111 | + try { |
| 112 | + FileUtils.createFile(filePath); |
| 113 | + fileOutputStream = new FileOutputStream(filePath); |
| 114 | + beingWrittenOutputStream.put(filePath, fileOutputStream); |
| 115 | + } catch (IOException e) { |
| 116 | + log.error("can not get output file stream"); |
| 117 | + throw new RuntimeException(e); |
| 118 | + } |
| 119 | + } |
| 120 | + return fileOutputStream; |
| 121 | + } |
| 122 | +} |
0 commit comments