|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.dtstack.flink.sql.sink.aws; |
| 20 | + |
| 21 | +import com.amazonaws.services.s3.AmazonS3Client; |
| 22 | +import com.amazonaws.services.s3.model.AppendObjectRequest; |
| 23 | +import com.amazonaws.services.s3.model.ObjectMetadata; |
| 24 | +import com.dtstack.flink.sql.outputformat.AbstractDtRichOutputFormat; |
| 25 | +import com.dtstack.flink.sql.sink.aws.util.AwsManager; |
| 26 | +import org.apache.flink.api.java.tuple.Tuple2; |
| 27 | +import org.apache.flink.configuration.Configuration; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +import java.io.ByteArrayInputStream; |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.InputStream; |
| 34 | +import java.util.Objects; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author tiezhu |
| 38 | + * date 2020/12/1 |
| 39 | + * company dtstack |
| 40 | + */ |
| 41 | +public class AwsOutputFormat extends AbstractDtRichOutputFormat<Tuple2> { |
| 42 | + |
| 43 | + private static final Logger LOG = LoggerFactory.getLogger(AwsOutputFormat.class); |
| 44 | + |
| 45 | + private static final String LINE_BREAK = "\n"; |
| 46 | + |
| 47 | + private transient AmazonS3Client client; |
| 48 | + |
| 49 | + private String accessKey; |
| 50 | + private String secretKey; |
| 51 | + private String bucket; |
| 52 | + private String bucketAcl; |
| 53 | + private String objectName; |
| 54 | + private String hostname; |
| 55 | + private InputStream inputStream; |
| 56 | + private Long position = 0L; |
| 57 | + |
| 58 | + private AwsOutputFormat() { |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void configure(Configuration parameters) { |
| 63 | + LOG.warn("--- configure client ---"); |
| 64 | + client = AwsManager.initClient(accessKey, secretKey, hostname); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void open(int taskNumber, int numTasks) throws IOException { |
| 69 | + LOG.warn("--- open ---"); |
| 70 | + initMetric(); |
| 71 | + position = AwsManager.getObjectPosition(bucket, objectName, client); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void writeRecord(Tuple2 record) throws IOException { |
| 76 | + String recordStr = record.f1.toString() + LINE_BREAK; |
| 77 | + int length = recordStr.getBytes().length; |
| 78 | + inputStream = new ByteArrayInputStream(recordStr.getBytes()); |
| 79 | + ObjectMetadata metadata = new ObjectMetadata(); |
| 80 | + metadata.setContentLength(length); |
| 81 | + // 追加流式写入,但是这种情况下,可能会出现oom【因为数据都是缓存在内存中】 |
| 82 | + AppendObjectRequest appendObjectRequest = new AppendObjectRequest( |
| 83 | + bucket, objectName, inputStream, metadata) |
| 84 | + .withPosition(position); |
| 85 | + |
| 86 | + client.appendObject(appendObjectRequest); |
| 87 | + position += length; |
| 88 | + outRecords.inc(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void close() throws IOException { |
| 93 | + if (Objects.nonNull(inputStream)) { |
| 94 | + inputStream.close(); |
| 95 | + } |
| 96 | + |
| 97 | + if (Objects.nonNull(client)) { |
| 98 | + client.shutdown(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public static AwsOutputFormatBuilder buildS3OutputFormat() { |
| 103 | + return new AwsOutputFormatBuilder(); |
| 104 | + } |
| 105 | + |
| 106 | + public static class AwsOutputFormatBuilder { |
| 107 | + private final AwsOutputFormat awsOutputFormat; |
| 108 | + |
| 109 | + private AwsOutputFormatBuilder() { |
| 110 | + awsOutputFormat = new AwsOutputFormat(); |
| 111 | + } |
| 112 | + |
| 113 | + public AwsOutputFormatBuilder setAccessKey(String accessKey) { |
| 114 | + awsOutputFormat.accessKey = accessKey; |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + public AwsOutputFormatBuilder setSecretKey(String secretKey) { |
| 119 | + awsOutputFormat.secretKey = secretKey; |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + public AwsOutputFormatBuilder setBucket(String bucket) { |
| 124 | + awsOutputFormat.bucket = bucket; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + public AwsOutputFormatBuilder setBucketAcl(String bucketAcl) { |
| 129 | + awsOutputFormat.bucketAcl = bucketAcl; |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + public AwsOutputFormatBuilder setHostname(String hostname) { |
| 134 | + awsOutputFormat.hostname = hostname; |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + public AwsOutputFormatBuilder setObjectName(String objectName) { |
| 139 | + awsOutputFormat.objectName = objectName; |
| 140 | + return this; |
| 141 | + } |
| 142 | + |
| 143 | + public AwsOutputFormat finish() { |
| 144 | + return awsOutputFormat; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments