You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/01/06 03:35:01 UTC

[GitHub] [iceberg] stevenzwu commented on a change in pull request #2898: Flink: Fix duplicate rows when sync CDC data by FlinkSQL

stevenzwu commented on a change in pull request #2898:
URL: https://github.com/apache/iceberg/pull/2898#discussion_r779278816



##########
File path: flink/v1.14/flink/src/main/java/org/apache/iceberg/flink/sink/EqualityFieldKeySelector.java
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.iceberg.flink.sink;
+
+import java.lang.reflect.Array;
+import java.util.List;
+import org.apache.flink.api.java.functions.KeySelector;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.iceberg.Accessor;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+
+/**
+ * Create a {@link KeySelector} to shuffle by equality fields, to ensure same equality fields record will be emitted to
+ * same writer. That can prevent create duplicate record when insert and delete one row which have same equality field
+ * values on different writer in one transaction, and guarantee pos-delete will take effect.
+ */
+class EqualityFieldKeySelector extends BaseKeySelector<RowData, String> {
+
+  private final Accessor<StructLike>[] accessors;
+
+  @SuppressWarnings("unchecked")
+  EqualityFieldKeySelector(List<Integer> equalityFieldIds, Schema schema, RowType flinkSchema) {
+    super(schema, flinkSchema);
+
+    int size = equalityFieldIds.size();
+    this.accessors = (Accessor<StructLike>[]) Array.newInstance(Accessor.class, size);
+    for (int i = 0; i < size; i++) {
+      Accessor<StructLike> accessor = schema.accessorForField(equalityFieldIds.get(i));
+      Preconditions.checkArgument(accessor != null,
+          "Cannot build accessor for field: " + schema.findField(equalityFieldIds.get(i)));
+      accessors[i] = accessor;
+    }
+  }
+
+  @Override
+  public String getKey(RowData row) throws Exception {
+    StringBuilder builder = new StringBuilder("Key(");
+    for (int i = 0; i < accessors.length; i++) {
+      if (i != 0) {
+        builder.append(",");
+      }
+      builder.append(accessors[i].get(lazyRowDataWrapper().wrap(row)));

Review comment:
       nit: we can `wrap(row)` once outside the for loop




-- 
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-unsubscribe@iceberg.apache.org

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



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org