the-other-tim-brown commented on code in PR #599: URL: https://github.com/apache/incubator-xtable/pull/599#discussion_r1901295672
########## xtable-core/src/main/java/org/apache/xtable/catalog/glue/table/IcebergGlueCatalogTableBuilder.java: ########## @@ -0,0 +1,121 @@ +/* + * 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.catalog.glue.table; + +import static org.apache.iceberg.BaseMetastoreTableOperations.METADATA_LOCATION_PROP; +import static org.apache.iceberg.BaseMetastoreTableOperations.PREVIOUS_METADATA_LOCATION_PROP; +import static org.apache.iceberg.BaseMetastoreTableOperations.TABLE_TYPE_PROP; +import static org.apache.xtable.catalog.CatalogUtils.castToHierarchicalTableIdentifier; +import static org.apache.xtable.catalog.glue.GlueCatalogSyncClient.GLUE_EXTERNAL_TABLE_TYPE; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.hadoop.conf.Configuration; + +import org.apache.iceberg.BaseTable; +import org.apache.iceberg.hadoop.HadoopTables; + +import com.google.common.annotations.VisibleForTesting; + +import org.apache.xtable.catalog.CatalogTableBuilder; +import org.apache.xtable.catalog.glue.GlueSchemaExtractor; +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.storage.TableFormat; + +import software.amazon.awssdk.services.glue.model.StorageDescriptor; +import software.amazon.awssdk.services.glue.model.Table; +import software.amazon.awssdk.services.glue.model.TableInput; + +/** Iceberg specific table operations for Glue catalog sync */ +public class IcebergGlueCatalogTableBuilder implements CatalogTableBuilder<TableInput, Table> { + + private final GlueSchemaExtractor schemaExtractor; + private final HadoopTables hadoopTables; + private static final String tableFormat = TableFormat.ICEBERG; + + public IcebergGlueCatalogTableBuilder(Configuration configuration) { + this.schemaExtractor = GlueSchemaExtractor.getInstance(); + this.hadoopTables = new HadoopTables(configuration); + } + + @VisibleForTesting + IcebergGlueCatalogTableBuilder(GlueSchemaExtractor schemaExtractor, HadoopTables hadoopTables) { + this.schemaExtractor = schemaExtractor; + this.hadoopTables = hadoopTables; + } + + @Override + public TableInput getCreateTableRequest( + InternalTable table, CatalogTableIdentifier tableIdentifier) { + HierarchicalTableIdentifier tblIdentifier = castToHierarchicalTableIdentifier(tableIdentifier); + BaseTable fsTable = loadTableFromFs(table.getBasePath()); + return TableInput.builder() + .name(tblIdentifier.getTableName()) + .tableType(GLUE_EXTERNAL_TABLE_TYPE) + .parameters(getTableParameters(fsTable)) + .storageDescriptor( + StorageDescriptor.builder() + .location(table.getBasePath()) + .columns(schemaExtractor.toColumns(tableFormat, table.getReadSchema())) + .build()) + .build(); + } + + @Override + public TableInput getUpdateTableRequest( + InternalTable table, Table catalogTable, CatalogTableIdentifier tableIdentifier) { + HierarchicalTableIdentifier tblIdentifier = castToHierarchicalTableIdentifier(tableIdentifier); + BaseTable icebergTable = loadTableFromFs(table.getBasePath()); + Map<String, String> parameters = new HashMap<>(catalogTable.parameters()); + parameters.put(PREVIOUS_METADATA_LOCATION_PROP, parameters.get(METADATA_LOCATION_PROP)); + parameters.put(METADATA_LOCATION_PROP, getMetadataFileLocation(icebergTable)); + parameters.putAll(icebergTable.properties()); + + return TableInput.builder() + .name(tblIdentifier.getTableName()) + .tableType(GLUE_EXTERNAL_TABLE_TYPE) + .parameters(parameters) + .storageDescriptor( + StorageDescriptor.builder() + .location(table.getBasePath()) + .columns( + schemaExtractor.toColumns(tableFormat, table.getReadSchema(), catalogTable)) + .build()) + .build(); + } + + @VisibleForTesting + Map<String, String> getTableParameters(BaseTable icebergTable) { + Map<String, String> parameters = new HashMap<>(icebergTable.properties()); + parameters.put(TABLE_TYPE_PROP, tableFormat); + parameters.put(METADATA_LOCATION_PROP, getMetadataFileLocation(icebergTable)); + return parameters; + } + + private BaseTable loadTableFromFs(String tableBasePath) { Review Comment: yes, specifically an iceberg table managed with the glue catalog https://iceberg.apache.org/docs/1.6.0/aws/?h=glue#glue-catalog -- 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: commits-unsubscr...@xtable.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org