pvary commented on code in PR #1495:
URL: https://github.com/apache/iceberg/pull/1495#discussion_r1582687790


##########
mr/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.mr.hive;
+
+import java.util.Properties;
+import java.util.Set;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.metastore.HiveMetaHook;
+import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants;
+import org.apache.iceberg.BaseMetastoreTableOperations;
+import org.apache.iceberg.CatalogUtil;
+import org.apache.iceberg.PartitionSpecParser;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.SchemaParser;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableMetadataParser;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.mr.Catalogs;
+import org.apache.iceberg.mr.InputFormatConfig;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HiveIcebergMetaHook implements HiveMetaHook {
+  private static final Logger LOG = 
LoggerFactory.getLogger(HiveIcebergMetaHook.class);
+  private static final Set<String> PARAMETERS_TO_REMOVE = ImmutableSet
+      .of(InputFormatConfig.TABLE_SCHEMA, InputFormatConfig.PARTITION_SPEC, 
Catalogs.LOCATION, Catalogs.NAME);
+  private static final Set<String> PROPERTIES_TO_REMOVE = ImmutableSet
+      .of(InputFormatConfig.EXTERNAL_TABLE_PURGE, 
hive_metastoreConstants.META_TABLE_STORAGE, "EXTERNAL",
+          "bucketing_version");
+
+  private final Configuration conf;
+  private Table icebergTable = null;
+  private Properties catalogProperties;
+  private boolean deleteIcebergTable;
+  private FileIO deleteIo;
+  private TableMetadata deleteMetadata;
+
+  public HiveIcebergMetaHook(Configuration conf) {
+    this.conf = conf;
+  }
+
+  @Override
+  public void preCreateTable(org.apache.hadoop.hive.metastore.api.Table 
hmsTable) {
+    this.catalogProperties = getCatalogProperties(hmsTable);
+
+    // Set the table type even for non HiveCatalog based tables
+    hmsTable.getParameters().put(BaseMetastoreTableOperations.TABLE_TYPE_PROP,
+        BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE.toUpperCase());
+
+    if (!Catalogs.hiveCatalog(conf)) {
+      // If not using HiveCatalog check for existing table
+      try {
+        this.icebergTable = Catalogs.loadTable(conf, catalogProperties);
+
+        
Preconditions.checkArgument(catalogProperties.getProperty(InputFormatConfig.TABLE_SCHEMA)
 == null,
+            "Iceberg table already created - can not use provided schema");
+        
Preconditions.checkArgument(catalogProperties.getProperty(InputFormatConfig.PARTITION_SPEC)
 == null,
+            "Iceberg table already created - can not use provided partition 
specification");
+
+        LOG.info("Iceberg table already exists {}", icebergTable);
+
+        return;
+      } catch (NoSuchTableException nte) {
+        // If the table does not exist we will create it below
+      }
+    }
+
+    // If the table does not exist collect data for table creation
+    String schemaString = 
catalogProperties.getProperty(InputFormatConfig.TABLE_SCHEMA);
+    Preconditions.checkNotNull(schemaString, "Please provide a table schema");
+    // Just check if it is parsable, and later use for partition specification 
parsing
+    Schema schema = SchemaParser.fromJson(schemaString);
+
+    String specString = 
catalogProperties.getProperty(InputFormatConfig.PARTITION_SPEC);
+    if (specString != null) {
+      // Just check if it is parsable
+      PartitionSpecParser.fromJson(schema, specString);
+    }
+
+    // Allow purging table data if the table is created now and not set 
otherwise
+    if (hmsTable.getParameters().get(InputFormatConfig.EXTERNAL_TABLE_PURGE) 
== null) {
+      hmsTable.getParameters().put(InputFormatConfig.EXTERNAL_TABLE_PURGE, 
"TRUE");
+    }
+
+    // Remove creation related properties
+    PARAMETERS_TO_REMOVE.forEach(hmsTable.getParameters()::remove);
+  }
+
+  @Override
+  public void rollbackCreateTable(org.apache.hadoop.hive.metastore.api.Table 
hmsTable) {
+    // do nothing
+  }
+
+  @Override
+  public void commitCreateTable(org.apache.hadoop.hive.metastore.api.Table 
hmsTable) {
+    if (icebergTable == null) {
+      if (Catalogs.hiveCatalog(conf)) {
+        catalogProperties.put(TableProperties.ENGINE_HIVE_ENABLED, true);

Review Comment:
   @shivjha30: The code line you have highlighted is for overriding the global 
settings on table level. So you have 2 levels to enable the hive engine:
   - Catalog level (ConfigProperties.ENGINE_HIVE_ENABLED) - you can set the 
config in the hadoop configuration of the HiveCatalog
   - Table level (TableProperties.ENGINE_HIVE_ENABLED) - you can set the config 
in the table properties
   



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

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


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

Reply via email to