vahmed-hamdy commented on code in PR #136:
URL: 
https://github.com/apache/flink-connector-aws/pull/136#discussion_r1565513965


##########
flink-connector-aws/flink-connector-dynamodb/src/main/java/org/apache/flink/connector/dynamodb/sink/DynamoDbTypeInformedElementConverter.java:
##########
@@ -0,0 +1,245 @@
+/*
+ * 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.flink.connector.dynamodb.sink;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo;
+import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
+import org.apache.flink.api.common.typeinfo.NumericTypeInfo;
+import org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeutils.CompositeType;
+import org.apache.flink.api.connector.sink2.SinkWriter;
+import org.apache.flink.api.java.tuple.Tuple;
+import org.apache.flink.api.java.typeutils.ObjectArrayTypeInfo;
+import org.apache.flink.api.java.typeutils.TupleTypeInfo;
+import org.apache.flink.connector.base.sink.writer.ElementConverter;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import software.amazon.awssdk.core.SdkBytes;
+import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * A {@link ElementConverter} that converts an element to a {@link 
DynamoDbWriteRequest} using
+ * TypeInformation provided.
+ */
+@PublicEvolving
+public class DynamoDbTypeInformedElementConverter<inputT>
+        implements ElementConverter<inputT, DynamoDbWriteRequest> {
+    private final CompositeType<inputT> typeInfo;
+
+    /**
+     * Creates a {@link DynamoDbTypeInformedElementConverter} that converts an 
element to a {@link
+     * DynamoDbWriteRequest} using the provided {@link CompositeType}. Usage: 
{@code new
+     * 
DynamoDbTypeInformedElementConverter<>(TypeInformation.of(MyPojoClass.class))}
+     *
+     * @param typeInfo The {@link CompositeType} that provides the type 
information for the element.
+     */
+    public DynamoDbTypeInformedElementConverter(CompositeType<inputT> 
typeInfo) {
+        this.typeInfo = typeInfo;
+    }
+
+    @Override
+    public DynamoDbWriteRequest apply(inputT input, SinkWriter.Context 
context) {
+        try {
+            return DynamoDbWriteRequest.builder()
+                    .setType(DynamoDbWriteRequestType.PUT)
+                    .setItem(convertElementUsingTypeInfo(input, typeInfo))
+                    .build();
+        } catch (IllegalArgumentException e) {
+            throw new FlinkRuntimeException("Couldn't convert Element to 
AttributeVal", e);
+        }
+    }
+
+    private <attT> Map<String, AttributeValue> convertElementUsingTypeInfo(
+            attT t, CompositeType<attT> typeInfo) {
+        Map<String, AttributeValue> map = new HashMap<>();
+        for (String fieldKey : typeInfo.getFieldNames()) {
+            TypeInformation<attT> fieldType = typeInfo.getTypeAt(fieldKey);
+            try {
+                Field field = t.getClass().getDeclaredField(fieldKey);
+                field.setAccessible(true);
+                Object fieldVal = field.get(t);
+                checkTypeCompatibility(fieldVal, fieldType);
+                attT fieldValCaster = (attT) fieldVal;
+                map.put(fieldKey, convertValue(fieldValCaster, fieldType));
+            } catch (NoSuchFieldException | IllegalAccessException e) {
+                throw new IllegalArgumentException(
+                        String.format(
+                                "Failed to extract field %s declared in 
TypeInfo "
+                                        + "from Object %s",
+                                fieldKey, t),
+                        e);
+            }
+        }
+
+        return map;
+    }
+
+    private <attrT> AttributeValue convertValue(
+            attrT attribute, TypeInformation<attrT> objectTypeInformation) {
+        if (attribute == null) {
+            return AttributeValue.builder().nul(true).build();

Review Comment:
   @dannycranmer , yeah just checked 
`testConvertOrderToDynamoDbWriteRequestWithIgnoresNull`.



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to