vahmed-hamdy commented on code in PR #136: URL: https://github.com/apache/flink-connector-aws/pull/136#discussion_r1581114799
########## flink-connector-aws/flink-connector-dynamodb/src/main/java/org/apache/flink/connector/dynamodb/sink/DynamoDbTypeInformedElementConverter.java: ########## @@ -0,0 +1,380 @@ +/* + * 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.Sink; +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.PojoTypeInfo; +import org.apache.flink.api.java.typeutils.RowTypeInfo; +import org.apache.flink.api.java.typeutils.TupleTypeInfo; +import org.apache.flink.connector.base.sink.writer.ElementConverter; +import org.apache.flink.connector.dynamodb.table.converter.ArrayAttributeConverterProvider; +import org.apache.flink.types.Row; +import org.apache.flink.util.FlinkRuntimeException; +import org.apache.flink.util.Preconditions; + +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter; +import software.amazon.awssdk.enhanced.dynamodb.AttributeConverterProvider; +import software.amazon.awssdk.enhanced.dynamodb.AttributeValueType; +import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; +import software.amazon.awssdk.enhanced.dynamodb.TableSchema; +import software.amazon.awssdk.enhanced.dynamodb.internal.mapper.BeanAttributeGetter; +import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema; +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; + +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Function; + +/** + * 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; + private final boolean ignoreNulls; + private TableSchema<inputT> tableSchema; + + /** + * 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, true); + } + + public DynamoDbTypeInformedElementConverter( + CompositeType<inputT> typeInfo, boolean ignoreNulls) { + this.typeInfo = typeInfo; + this.ignoreNulls = ignoreNulls; + } + + @Override + public void open(Sink.InitContext context) { + try { + tableSchema = createTableSchema(typeInfo); + } catch (IntrospectionException | IllegalStateException | IllegalArgumentException e) { + throw new FlinkRuntimeException("Failed to extract DynamoDb table schema", e); + } + } + + @Override + public DynamoDbWriteRequest apply(inputT input, SinkWriter.Context context) { + Preconditions.checkNotNull(tableSchema, "TableSchema is not initialized"); + try { + return DynamoDbWriteRequest.builder() + .setType(DynamoDbWriteRequestType.PUT) + .setItem(tableSchema.itemToMap(input, ignoreNulls)) + .build(); + } catch (ClassCastException | IllegalArgumentException e) { + throw new FlinkRuntimeException( + String.format( + "Failed to convert %s to DynamoDbWriteRequest using %s", + input, typeInfo), + e); + } + } + + private <attrT> TableSchema<attrT> createTableSchema(CompositeType<attrT> typeInfo) + throws IntrospectionException { + if (typeInfo instanceof RowTypeInfo) { + return (TableSchema<attrT>) createTableSchemaFromRowType((RowTypeInfo) typeInfo); + } else if (typeInfo instanceof PojoTypeInfo<?>) { + return createTableSchemaFromPojo((PojoTypeInfo<attrT>) typeInfo); + } else if (typeInfo instanceof TupleTypeInfo<?>) { + return createTableSchemaFromTuple((TupleTypeInfo<?>) typeInfo); + } else { + throw new IllegalArgumentException(String.format("Unsupported TypeInfo %s", typeInfo)); + } + } + + private TableSchema<Row> createTableSchemaFromRowType(RowTypeInfo typeInfo) { + StaticTableSchema.Builder<Row> tableSchemaBuilder = + StaticTableSchema.builder(typeInfo.getTypeClass()); + + String[] fieldNames = typeInfo.getFieldNames(); + for (int i = 0; i < typeInfo.getArity(); i++) { + TypeInformation<?> fieldType = typeInfo.getTypeAt(i); + int finalI = i; + addAttribute( + tableSchemaBuilder, + fieldNames[finalI], + (attrT) -> attrT.getField(finalI), + (TypeInformation<? super Object>) fieldType); + } + + return tableSchemaBuilder.build(); + } + + private <attT> TableSchema<attT> createTableSchemaFromTuple(TupleTypeInfo<?> typeInfo) { + TypeInformation<?>[] fieldTypes = typeInfo.getFieldTypes(); + String[] fieldNames = typeInfo.getFieldNames(); + + StaticTableSchema.Builder<?> tableSchemaBuilder = + StaticTableSchema.builder(typeInfo.getTypeClass()); + for (int i = 0; i < fieldNames.length; i++) { + int finalI = i; + addAttribute( + tableSchemaBuilder, + fieldNames[finalI], + (attrT) -> ((Tuple) attrT).getField(finalI), + (TypeInformation<?>) fieldTypes[i]); + } + + return (TableSchema<attT>) tableSchemaBuilder.build(); + } + + private <attrT> TableSchema<attrT> createTableSchemaFromPojo(PojoTypeInfo<attrT> typeInfo) + throws IntrospectionException { + StaticTableSchema.Builder<attrT> tableSchemaBuilder = + StaticTableSchema.builder(typeInfo.getTypeClass()); + BeanInfo beanInfo = Introspector.getBeanInfo(typeInfo.getTypeClass()); + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); Review Comment: I am not sure I get what we need to cache, these descriptors are only created once when creating the schema, during element conversion only the Enhanced getters are used. Btw, this is also what the BeanTableSchema does :D -- 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]
