ic4y commented on code in PR #3166:
URL: 
https://github.com/apache/incubator-seatunnel/pull/3166#discussion_r1011332545


##########
seatunnel-connectors-v2/connector-amazondynamodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/amazondynamodb/sink/DdynamoDbSinkClient.java:
##########
@@ -0,0 +1,134 @@
+/*
+ * 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.amazondynamodb.sink;
+
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import 
org.apache.seatunnel.connectors.seatunnel.amazondynamodb.config.AmazondynamodbSourceOptions;
+import 
org.apache.seatunnel.connectors.seatunnel.amazondynamodb.serialize.DefaultSeaTunnelRowDeserializer;
+import 
org.apache.seatunnel.connectors.seatunnel.amazondynamodb.serialize.SeaTunnelRowDeserializer;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
+import software.amazon.awssdk.services.dynamodb.model.BatchWriteItemRequest;
+import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
+import software.amazon.awssdk.services.dynamodb.model.PutRequest;
+import software.amazon.awssdk.services.dynamodb.model.WriteRequest;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
+public class DdynamoDbSinkClient {
+    private final AmazondynamodbSourceOptions amazondynamodbSourceOptions;
+    private ScheduledExecutorService scheduler;
+    private ScheduledFuture<?> scheduledFuture;
+    private volatile boolean initialize;
+    private volatile Exception flushException;
+    private DynamoDbClient dynamoDbClient;
+    private final List<WriteRequest> batchList;
+    protected SeaTunnelRowDeserializer seaTunnelRowDeserializer;
+
+    public DdynamoDbSinkClient(AmazondynamodbSourceOptions 
amazondynamodbSourceOptions, SeaTunnelRowType typeInfo) {
+        this.amazondynamodbSourceOptions = amazondynamodbSourceOptions;
+        this.batchList = new ArrayList<>();
+        this.seaTunnelRowDeserializer = new 
DefaultSeaTunnelRowDeserializer(typeInfo);
+    }
+
+    private void tryInit() throws IOException {
+        if (initialize) {
+            return;
+        }
+        dynamoDbClient = DynamoDbClient.builder()
+            .endpointOverride(URI.create(amazondynamodbSourceOptions.getUrl()))
+            // The region is meaningless for local DynamoDb but required for 
client builder validation
+            .region(Region.of(amazondynamodbSourceOptions.getRegion()))
+            .credentialsProvider(StaticCredentialsProvider.create(
+                
AwsBasicCredentials.create(amazondynamodbSourceOptions.getAccessKeyId(), 
amazondynamodbSourceOptions.getSecretAccessKey())))
+            .build();
+
+        scheduler = Executors.newSingleThreadScheduledExecutor(
+            new 
ThreadFactoryBuilder().setNameFormat("DdynamoDb-sink-output-%s").build());
+        scheduledFuture = scheduler.scheduleAtFixedRate(
+            () -> {
+                try {
+                    flush();
+                } catch (IOException e) {
+                    flushException = e;
+                }
+            },
+            amazondynamodbSourceOptions.getBatchIntervalMs(),
+            amazondynamodbSourceOptions.getBatchIntervalMs(),
+            TimeUnit.MILLISECONDS);
+
+        initialize = true;
+    }
+
+    public synchronized void write(PutItemRequest putItemRequest) throws 
IOException {
+        tryInit();
+        checkFlushException();
+        batchList.add(WriteRequest.builder().putRequest(
+            PutRequest.builder().item(putItemRequest.item()).build()).build());
+        if (amazondynamodbSourceOptions.getBatchSize() > 0
+            && batchList.size() >= amazondynamodbSourceOptions.getBatchSize()) 
{
+            flush();
+        }
+    }
+
+    public synchronized void close() throws IOException {
+        if (scheduledFuture != null) {
+            scheduledFuture.cancel(false);
+            scheduler.shutdown();
+        }
+        flush();

Review Comment:
   The close method will also be called when no data is written, and 
dynamoDbClient will have a null pointer exception at this time. This can be 
tested by a task where the source does not output any data.



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