vamsikarnika commented on code in PR #649:
URL: https://github.com/apache/incubator-xtable/pull/649#discussion_r1963616801


##########
xtable-aws/src/main/java/org/apache/xtable/glue/GlueCatalogPartitionSyncOperations.java:
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.xtable.glue;
+
+import static org.apache.hudi.common.util.CollectionUtils.isNullOrEmpty;
+import static 
org.apache.xtable.catalog.CatalogUtils.toHierarchicalTableIdentifier;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import lombok.extern.log4j.Log4j2;
+
+import org.apache.hudi.common.util.CollectionUtils;
+
+import org.apache.xtable.catalog.CatalogPartition;
+import org.apache.xtable.catalog.CatalogPartitionSyncOperations;
+import org.apache.xtable.exception.CatalogSyncException;
+import org.apache.xtable.model.catalog.CatalogTableIdentifier;
+import org.apache.xtable.model.catalog.HierarchicalTableIdentifier;
+
+import software.amazon.awssdk.services.glue.GlueClient;
+import software.amazon.awssdk.services.glue.model.BatchCreatePartitionRequest;
+import software.amazon.awssdk.services.glue.model.BatchCreatePartitionResponse;
+import software.amazon.awssdk.services.glue.model.BatchDeletePartitionRequest;
+import software.amazon.awssdk.services.glue.model.BatchDeletePartitionResponse;
+import software.amazon.awssdk.services.glue.model.BatchUpdatePartitionRequest;
+import 
software.amazon.awssdk.services.glue.model.BatchUpdatePartitionRequestEntry;
+import software.amazon.awssdk.services.glue.model.BatchUpdatePartitionResponse;
+import software.amazon.awssdk.services.glue.model.GetPartitionsRequest;
+import software.amazon.awssdk.services.glue.model.GetPartitionsResponse;
+import software.amazon.awssdk.services.glue.model.PartitionInput;
+import software.amazon.awssdk.services.glue.model.PartitionValueList;
+import software.amazon.awssdk.services.glue.model.StorageDescriptor;
+import software.amazon.awssdk.services.glue.model.Table;
+import software.amazon.awssdk.services.glue.model.TableInput;
+import software.amazon.awssdk.services.glue.model.UpdateTableRequest;
+
+@Log4j2
+public class GlueCatalogPartitionSyncOperations implements 
CatalogPartitionSyncOperations {
+
+  private final GlueClient glueClient;
+  private final GlueCatalogConfig glueCatalogConfig;
+
+  public GlueCatalogPartitionSyncOperations(
+      GlueClient glueClient, GlueCatalogConfig glueCatalogConfig) {
+    this.glueClient = glueClient;
+    this.glueCatalogConfig = glueCatalogConfig;
+  }
+
+  @Override
+  public List<CatalogPartition> getAllPartitions(CatalogTableIdentifier 
catalogTableIdentifier) {
+    HierarchicalTableIdentifier tableIdentifier =
+        toHierarchicalTableIdentifier(catalogTableIdentifier);
+    try {
+      List<CatalogPartition> partitions = new ArrayList<>();
+      String nextToken = null;
+      do {
+        GetPartitionsResponse result =
+            glueClient.getPartitions(
+                GetPartitionsRequest.builder()
+                    .databaseName(tableIdentifier.getDatabaseName())
+                    .tableName(tableIdentifier.getTableName())
+                    .nextToken(nextToken)
+                    .build());
+        partitions.addAll(
+            result.partitions().stream()
+                .map(p -> new CatalogPartition(p.values(), 
p.storageDescriptor().location()))
+                .collect(Collectors.toList()));
+        nextToken = result.nextToken();
+      } while (nextToken != null);
+      return partitions;
+    } catch (Exception e) {
+      throw new CatalogSyncException(
+          "Failed to get all partitions for table " + tableIdentifier, e);
+    }
+  }
+
+  @Override
+  public void addPartitionsToTable(
+      CatalogTableIdentifier catalogTableIdentifier, List<CatalogPartition> 
partitionsToAdd) {
+    HierarchicalTableIdentifier tableIdentifier =
+        toHierarchicalTableIdentifier(catalogTableIdentifier);
+    if (partitionsToAdd.isEmpty()) {
+      log.info("No partitions to add for {}", tableIdentifier);
+      return;
+    }
+    log.info("Adding {} CatalogPartition(s) in table {}", 
partitionsToAdd.size(), tableIdentifier);
+    try {
+      Table table =
+          GlueCatalogTableUtils.getTable(
+              glueClient, glueCatalogConfig.getCatalogId(), 
catalogTableIdentifier);
+      StorageDescriptor sd = table.storageDescriptor();
+      List<PartitionInput> partitionInputs =
+          partitionsToAdd.stream()
+              .map(partition -> createPartitionInput(table, partition))
+              .collect(Collectors.toList());
+
+      List<BatchCreatePartitionResponse> responses = new ArrayList<>();
+
+      CollectionUtils.batches(partitionInputs, 
glueCatalogConfig.getMaxPartitionsPerRequest())
+          .forEach(
+              batch -> {
+                BatchCreatePartitionRequest request =
+                    BatchCreatePartitionRequest.builder()
+                        .databaseName(tableIdentifier.getDatabaseName())
+                        .tableName(tableIdentifier.getTableName())
+                        .partitionInputList(batch)
+                        .build();
+                responses.add(glueClient.batchCreatePartition(request));
+              });
+
+      responses.forEach(
+          response -> {
+            if (CollectionUtils.nonEmpty(response.errors())) {
+              if (response.errors().stream()
+                  .allMatch(
+                      (error) ->
+                          
"AlreadyExistsException".equals(error.errorDetail().errorCode()))) {

Review Comment:
   No, I couldn't find any error code for this exceptions. Glue SDK is the name 
itself as the error code.



##########
xtable-aws/src/main/java/org/apache/xtable/glue/GlueCatalogPartitionSyncOperations.java:
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.xtable.glue;
+
+import static org.apache.hudi.common.util.CollectionUtils.isNullOrEmpty;
+import static 
org.apache.xtable.catalog.CatalogUtils.toHierarchicalTableIdentifier;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import lombok.extern.log4j.Log4j2;
+
+import org.apache.hudi.common.util.CollectionUtils;
+
+import org.apache.xtable.catalog.CatalogPartition;
+import org.apache.xtable.catalog.CatalogPartitionSyncOperations;
+import org.apache.xtable.exception.CatalogSyncException;
+import org.apache.xtable.model.catalog.CatalogTableIdentifier;
+import org.apache.xtable.model.catalog.HierarchicalTableIdentifier;
+
+import software.amazon.awssdk.services.glue.GlueClient;
+import software.amazon.awssdk.services.glue.model.BatchCreatePartitionRequest;
+import software.amazon.awssdk.services.glue.model.BatchCreatePartitionResponse;
+import software.amazon.awssdk.services.glue.model.BatchDeletePartitionRequest;
+import software.amazon.awssdk.services.glue.model.BatchDeletePartitionResponse;
+import software.amazon.awssdk.services.glue.model.BatchUpdatePartitionRequest;
+import 
software.amazon.awssdk.services.glue.model.BatchUpdatePartitionRequestEntry;
+import software.amazon.awssdk.services.glue.model.BatchUpdatePartitionResponse;
+import software.amazon.awssdk.services.glue.model.GetPartitionsRequest;
+import software.amazon.awssdk.services.glue.model.GetPartitionsResponse;
+import software.amazon.awssdk.services.glue.model.PartitionInput;
+import software.amazon.awssdk.services.glue.model.PartitionValueList;
+import software.amazon.awssdk.services.glue.model.StorageDescriptor;
+import software.amazon.awssdk.services.glue.model.Table;
+import software.amazon.awssdk.services.glue.model.TableInput;
+import software.amazon.awssdk.services.glue.model.UpdateTableRequest;
+
+@Log4j2
+public class GlueCatalogPartitionSyncOperations implements 
CatalogPartitionSyncOperations {
+
+  private final GlueClient glueClient;
+  private final GlueCatalogConfig glueCatalogConfig;
+
+  public GlueCatalogPartitionSyncOperations(
+      GlueClient glueClient, GlueCatalogConfig glueCatalogConfig) {
+    this.glueClient = glueClient;
+    this.glueCatalogConfig = glueCatalogConfig;
+  }
+
+  @Override
+  public List<CatalogPartition> getAllPartitions(CatalogTableIdentifier 
catalogTableIdentifier) {
+    HierarchicalTableIdentifier tableIdentifier =
+        toHierarchicalTableIdentifier(catalogTableIdentifier);
+    try {
+      List<CatalogPartition> partitions = new ArrayList<>();
+      String nextToken = null;
+      do {
+        GetPartitionsResponse result =
+            glueClient.getPartitions(
+                GetPartitionsRequest.builder()
+                    .databaseName(tableIdentifier.getDatabaseName())
+                    .tableName(tableIdentifier.getTableName())
+                    .nextToken(nextToken)
+                    .build());
+        partitions.addAll(
+            result.partitions().stream()
+                .map(p -> new CatalogPartition(p.values(), 
p.storageDescriptor().location()))
+                .collect(Collectors.toList()));
+        nextToken = result.nextToken();
+      } while (nextToken != null);
+      return partitions;
+    } catch (Exception e) {
+      throw new CatalogSyncException(
+          "Failed to get all partitions for table " + tableIdentifier, e);
+    }
+  }
+
+  @Override
+  public void addPartitionsToTable(
+      CatalogTableIdentifier catalogTableIdentifier, List<CatalogPartition> 
partitionsToAdd) {
+    HierarchicalTableIdentifier tableIdentifier =
+        toHierarchicalTableIdentifier(catalogTableIdentifier);
+    if (partitionsToAdd.isEmpty()) {
+      log.info("No partitions to add for {}", tableIdentifier);
+      return;
+    }
+    log.info("Adding {} CatalogPartition(s) in table {}", 
partitionsToAdd.size(), tableIdentifier);
+    try {
+      Table table =
+          GlueCatalogTableUtils.getTable(
+              glueClient, glueCatalogConfig.getCatalogId(), 
catalogTableIdentifier);
+      StorageDescriptor sd = table.storageDescriptor();
+      List<PartitionInput> partitionInputs =
+          partitionsToAdd.stream()
+              .map(partition -> createPartitionInput(table, partition))
+              .collect(Collectors.toList());
+
+      List<BatchCreatePartitionResponse> responses = new ArrayList<>();
+
+      CollectionUtils.batches(partitionInputs, 
glueCatalogConfig.getMaxPartitionsPerRequest())
+          .forEach(
+              batch -> {
+                BatchCreatePartitionRequest request =
+                    BatchCreatePartitionRequest.builder()
+                        .databaseName(tableIdentifier.getDatabaseName())
+                        .tableName(tableIdentifier.getTableName())
+                        .partitionInputList(batch)
+                        .build();
+                responses.add(glueClient.batchCreatePartition(request));
+              });
+
+      responses.forEach(
+          response -> {
+            if (CollectionUtils.nonEmpty(response.errors())) {
+              if (response.errors().stream()
+                  .allMatch(
+                      (error) ->
+                          
"AlreadyExistsException".equals(error.errorDetail().errorCode()))) {

Review Comment:
   No, I couldn't find any error code for this exceptions. Glue SDK is using 
the name itself as the error code.



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