the-other-tim-brown commented on code in PR #649:
URL: https://github.com/apache/incubator-xtable/pull/649#discussion_r1954792421


##########
xtable-aws/src/main/java/org/apache/xtable/glue/GlueCatalogPartitionSyncOperations.java:
##########
@@ -0,0 +1,342 @@
+/*
+ * 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.EntityNotFoundException;
+import software.amazon.awssdk.services.glue.model.GetPartitionsRequest;
+import software.amazon.awssdk.services.glue.model.GetPartitionsResponse;
+import software.amazon.awssdk.services.glue.model.GetTableRequest;
+import software.amazon.awssdk.services.glue.model.GetTableResponse;
+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 {

Review Comment:
   Where does this class get used?



##########
xtable-aws/src/main/java/org/apache/xtable/glue/GlueCatalogPartitionSyncOperations.java:
##########
@@ -0,0 +1,342 @@
+/*
+ * 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.EntityNotFoundException;
+import software.amazon.awssdk.services.glue.model.GetPartitionsRequest;
+import software.amazon.awssdk.services.glue.model.GetPartitionsResponse;
+import software.amazon.awssdk.services.glue.model.GetTableRequest;
+import software.amazon.awssdk.services.glue.model.GetTableResponse;
+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);
+    }
+  }
+
+  private Table getTable(CatalogTableIdentifier catalogTableIdentifier) {

Review Comment:
   GlueCatalogSyncClient defines a very similar method. Can we use that instead 
of duplicating the code?



##########
xtable-aws/src/main/java/org/apache/xtable/glue/table/HudiGlueCatalogTableBuilder.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * 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.table;
+
+import static 
org.apache.xtable.catalog.CatalogUtils.toHierarchicalTableIdentifier;
+
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import org.apache.hadoop.conf.Configuration;
+
+import org.apache.hudi.common.model.HoodieFileFormat;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.util.ConfigUtils;
+
+import com.google.common.annotations.VisibleForTesting;
+
+import org.apache.xtable.catalog.CatalogTableBuilder;
+import org.apache.xtable.exception.CatalogSyncException;
+import org.apache.xtable.glue.GlueCatalogConfig;
+import org.apache.xtable.glue.GlueSchemaExtractor;
+import org.apache.xtable.hudi.HudiTableManager;
+import org.apache.xtable.hudi.catalog.HudiCatalogTableUtils;
+import org.apache.xtable.hudi.catalog.HudiInputFormatUtils;
+import org.apache.xtable.model.InternalTable;
+import org.apache.xtable.model.catalog.CatalogTableIdentifier;
+import org.apache.xtable.model.catalog.HierarchicalTableIdentifier;
+import org.apache.xtable.model.schema.InternalPartitionField;
+import org.apache.xtable.model.schema.InternalSchema;
+import org.apache.xtable.model.storage.TableFormat;
+
+import software.amazon.awssdk.services.glue.model.Column;
+import software.amazon.awssdk.services.glue.model.SerDeInfo;
+import software.amazon.awssdk.services.glue.model.StorageDescriptor;
+import software.amazon.awssdk.services.glue.model.Table;
+import software.amazon.awssdk.services.glue.model.TableInput;
+
+public class HudiGlueCatalogTableBuilder implements 
CatalogTableBuilder<TableInput, Table> {
+  protected static final String GLUE_EXTERNAL_TABLE_TYPE = "EXTERNAL_TABLE";
+  protected static final String HUDI_METADATA_CONFIG = 
"hudi.metadata-listing-enabled";
+
+  private final GlueSchemaExtractor schemaExtractor;
+  private final HudiTableManager hudiTableManager;
+  private final GlueCatalogConfig glueCatalogConfig;
+  private HoodieTableMetaClient metaClient;
+
+  public HudiGlueCatalogTableBuilder(
+      GlueCatalogConfig glueCatalogConfig, Configuration configuration) {
+    this.glueCatalogConfig = glueCatalogConfig;
+    this.schemaExtractor = GlueSchemaExtractor.getInstance();
+    this.hudiTableManager = HudiTableManager.of(configuration);
+  }
+
+  @VisibleForTesting
+  HudiGlueCatalogTableBuilder(
+      GlueCatalogConfig glueCatalogConfig,
+      GlueSchemaExtractor schemaExtractor,
+      HudiTableManager hudiTableManager,
+      HoodieTableMetaClient metaClient) {
+    this.glueCatalogConfig = glueCatalogConfig;
+    this.hudiTableManager = hudiTableManager;
+    this.schemaExtractor = schemaExtractor;
+    this.metaClient = metaClient;
+  }
+
+  HoodieTableMetaClient getMetaClient(String basePath) {
+    if (metaClient == null) {
+      Optional<HoodieTableMetaClient> metaClientOpt =
+          hudiTableManager.loadTableMetaClientIfExists(basePath);
+
+      if (!metaClientOpt.isPresent()) {
+        throw new CatalogSyncException(
+            "failed to get meta client since table is not present in the base 
path " + basePath);
+      }
+
+      metaClient = metaClientOpt.get();
+    }
+    return metaClient;
+  }
+
+  @Override
+  public TableInput getCreateTableRequest(
+      InternalTable table, CatalogTableIdentifier catalogTableIdentifier) {
+    HierarchicalTableIdentifier tableIdentifier =
+        toHierarchicalTableIdentifier(catalogTableIdentifier);
+    final Instant now = Instant.now();
+    List<String> partitionFields =
+        table.getPartitioningFields().stream()
+            .map(field -> field.getSourceField().getName())
+            .collect(Collectors.toList());
+    return TableInput.builder()
+        .name(tableIdentifier.getTableName())
+        .tableType(GLUE_EXTERNAL_TABLE_TYPE)
+        .parameters(getTableParameters(partitionFields, table.getReadSchema()))
+        .partitionKeys(getSchemaPartitionKeys(table.getPartitioningFields()))
+        .storageDescriptor(getStorageDescriptor(table))
+        .lastAccessTime(now)
+        .lastAnalyzedTime(now)
+        .build();
+  }
+
+  @Override
+  public TableInput getUpdateTableRequest(
+      InternalTable table, Table glueTable, CatalogTableIdentifier 
catalogTableIdentifier) {
+    HierarchicalTableIdentifier tableIdentifier =
+        toHierarchicalTableIdentifier(catalogTableIdentifier);
+    List<String> partitionFields =
+        table.getPartitioningFields().stream()
+            .map(field -> field.getSourceField().getName())
+            .collect(Collectors.toList());
+    Map<String, String> tableParameters = new 
HashMap<>(glueTable.parameters());
+    tableParameters.putAll(getTableParameters(partitionFields, 
table.getReadSchema()));
+    List<Column> newColumns = getSchemaWithoutPartitionKeys(table);
+    StorageDescriptor sd = glueTable.storageDescriptor();
+    StorageDescriptor partitionSD = sd.copy(copySd -> 
copySd.columns(newColumns));
+
+    final Instant now = Instant.now();
+    return TableInput.builder()
+        .name(tableIdentifier.getTableName())
+        .tableType(glueTable.tableType())
+        .parameters(tableParameters)
+        .partitionKeys(getSchemaPartitionKeys(table.getPartitioningFields()))
+        .storageDescriptor(partitionSD)
+        .lastAccessTime(now)
+        .lastAnalyzedTime(now)
+        .build();
+  }
+
+  @VisibleForTesting
+  Map<String, String> getTableParameters(List<String> partitionFields, 
InternalSchema schema) {
+    Map<String, String> tableProperties = new HashMap<>();
+    tableProperties.put(HUDI_METADATA_CONFIG, "true");
+    Map<String, String> sparkTableProperties =
+        HudiCatalogTableUtils.getSparkTableProperties(
+            partitionFields, "", glueCatalogConfig.getSchemaLengthThreshold(), 
schema);
+    tableProperties.putAll(sparkTableProperties);
+    return tableProperties;
+  }
+
+  @VisibleForTesting
+  StorageDescriptor getStorageDescriptor(InternalTable table) {
+    HoodieFileFormat baseFileFormat =
+        
getMetaClient(table.getBasePath()).getTableConfig().getBaseFileFormat();
+    SerDeInfo serDeInfo =
+        SerDeInfo.builder()
+            
.serializationLibrary(HudiInputFormatUtils.getSerDeClassName(baseFileFormat))
+            .parameters(getSerdeProperties(false, table.getBasePath()))
+            .build();
+    return StorageDescriptor.builder()
+        .serdeInfo(serDeInfo)
+        .location(table.getBasePath())
+        
.inputFormat(HudiInputFormatUtils.getInputFormatClassName(baseFileFormat, 
false))
+        
.outputFormat(HudiInputFormatUtils.getOutputFormatClassName(baseFileFormat))
+        .columns(getSchemaWithoutPartitionKeys(table))
+        .build();
+  }
+
+  List<Column> getSchemaWithoutPartitionKeys(InternalTable table) {
+    List<String> partitionKeys =
+        table.getPartitioningFields().stream()
+            .map(field -> field.getSourceField().getName())
+            .collect(Collectors.toList());
+    return schemaExtractor.toColumns(TableFormat.HUDI, 
table.getReadSchema()).stream()
+        .filter(c -> !partitionKeys.contains(c.name()))
+        .collect(Collectors.toList());
+  }
+
+  List<Column> getSchemaPartitionKeys(List<InternalPartitionField> 
partitioningFields) {

Review Comment:
   Let's make this and the `getSerdeProperties` private



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