|
| 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; |
| 19 | + |
| 20 | +import org.apache.seatunnel.api.common.PrepareFailException; |
| 21 | +import org.apache.seatunnel.api.common.SeaTunnelContext; |
| 22 | +import org.apache.seatunnel.api.serialization.DefaultSerializer; |
| 23 | +import org.apache.seatunnel.api.serialization.Serializer; |
| 24 | +import org.apache.seatunnel.api.sink.SeaTunnelSink; |
| 25 | +import org.apache.seatunnel.api.sink.SinkAggregatedCommitter; |
| 26 | +import org.apache.seatunnel.api.sink.SinkCommitter; |
| 27 | +import org.apache.seatunnel.api.sink.SinkWriter; |
| 28 | +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; |
| 29 | +import org.apache.seatunnel.api.table.type.SeaTunnelRow; |
| 30 | +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; |
| 31 | +import org.apache.seatunnel.connectors.seatunnel.file.config.HadoopConf; |
| 32 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.commit.FileAggregatedCommitInfo2; |
| 33 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.commit.FileCommitInfo2; |
| 34 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.commit.FileSinkAggregatedCommitter2; |
| 35 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.commit.FileSinkCommitter2; |
| 36 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.config.TextFileSinkConfig; |
| 37 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.state.FileSinkState2; |
| 38 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.writer.WriteStrategy; |
| 39 | +import org.apache.seatunnel.connectors.seatunnel.file.sink.writer.WriteStrategyFactory; |
| 40 | + |
| 41 | +import org.apache.seatunnel.shade.com.typesafe.config.Config; |
| 42 | + |
| 43 | +import java.io.IOException; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Optional; |
| 46 | + |
| 47 | +public abstract class BaseFileSink implements SeaTunnelSink<SeaTunnelRow, FileSinkState2, FileCommitInfo2, FileAggregatedCommitInfo2> { |
| 48 | + protected SeaTunnelRowType seaTunnelRowType; |
| 49 | + protected Config pluginConfig; |
| 50 | + protected HadoopConf hadoopConf; |
| 51 | + protected TextFileSinkConfig textFileSinkConfig; |
| 52 | + protected WriteStrategy writeStrategy; |
| 53 | + protected SeaTunnelContext seaTunnelContext; |
| 54 | + protected String jobId; |
| 55 | + |
| 56 | + @Override |
| 57 | + public void setSeaTunnelContext(SeaTunnelContext seaTunnelContext) { |
| 58 | + this.seaTunnelContext = seaTunnelContext; |
| 59 | + this.jobId = seaTunnelContext.getJobId(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void setTypeInfo(SeaTunnelRowType seaTunnelRowType) { |
| 64 | + this.seaTunnelRowType = seaTunnelRowType; |
| 65 | + this.textFileSinkConfig = new TextFileSinkConfig(pluginConfig, seaTunnelRowType); |
| 66 | + this.writeStrategy = WriteStrategyFactory.of(textFileSinkConfig.getFileFormat(), textFileSinkConfig); |
| 67 | + this.writeStrategy.setSeaTunnelRowTypeInfo(seaTunnelRowType); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public SeaTunnelDataType<SeaTunnelRow> getConsumedType() { |
| 72 | + return seaTunnelRowType; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public SinkWriter<SeaTunnelRow, FileCommitInfo2, FileSinkState2> restoreWriter(SinkWriter.Context context, List<FileSinkState2> states) throws IOException { |
| 77 | + return new BaseFileSinkWriter(writeStrategy, hadoopConf, context, jobId, states); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Optional<SinkCommitter<FileCommitInfo2>> createCommitter() throws IOException { |
| 82 | + return Optional.of(new FileSinkCommitter2()); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public Optional<SinkAggregatedCommitter<FileCommitInfo2, FileAggregatedCommitInfo2>> createAggregatedCommitter() throws IOException { |
| 87 | + return Optional.of(new FileSinkAggregatedCommitter2()); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public SinkWriter<SeaTunnelRow, FileCommitInfo2, FileSinkState2> createWriter(SinkWriter.Context context) throws IOException { |
| 92 | + return new BaseFileSinkWriter(writeStrategy, hadoopConf, context, jobId); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Optional<Serializer<FileCommitInfo2>> getCommitInfoSerializer() { |
| 97 | + return Optional.of(new DefaultSerializer<>()); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public Optional<Serializer<FileAggregatedCommitInfo2>> getAggregatedCommitInfoSerializer() { |
| 102 | + return Optional.of(new DefaultSerializer<>()); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public Optional<Serializer<FileSinkState2>> getWriterStateSerializer() { |
| 107 | + return Optional.of(new DefaultSerializer<>()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Use the pluginConfig to do some initialize operation. |
| 112 | + * |
| 113 | + * @param pluginConfig plugin config. |
| 114 | + * @throws PrepareFailException if plugin prepare failed, the {@link PrepareFailException} will throw. |
| 115 | + */ |
| 116 | + @Override |
| 117 | + public void prepare(Config pluginConfig) throws PrepareFailException { |
| 118 | + this.pluginConfig = pluginConfig; |
| 119 | + } |
| 120 | +} |
0 commit comments