flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [flink] bowenli86 commented on a change in pull request #8329: [FLINK-12239][hive] Support table related operations in GenericHiveMetastoreCatalog
Date Thu, 02 May 2019 05:59:54 GMT
bowenli86 commented on a change in pull request #8329: [FLINK-12239][hive] Support table related
operations in GenericHiveMetastoreCatalog
URL: https://github.com/apache/flink/pull/8329#discussion_r280257423
 
 

 ##########
 File path: flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/GenericHiveMetastoreCatalogUtil.java
 ##########
 @@ -18,32 +18,179 @@
 
 package org.apache.flink.table.catalog.hive;
 
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.table.catalog.CatalogBaseTable;
 import org.apache.flink.table.catalog.CatalogDatabase;
+import org.apache.flink.table.catalog.CatalogTable;
+import org.apache.flink.table.catalog.GenericCatalogTable;
+import org.apache.flink.table.catalog.ObjectPath;
+import org.apache.flink.table.plan.stats.TableStats;
 
 import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.metastore.api.SerDeInfo;
+import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
+import org.apache.hadoop.hive.metastore.api.Table;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
-
+import java.util.stream.Collectors;
 
 /**
  * Utils to convert meta objects between Flink and Hive for GenericHiveMetastoreCatalog.
  */
 public class GenericHiveMetastoreCatalogUtil {
 
+	// Prefix used to distinguish properties created by Hive and Flink,
+	// as Hive metastore has its own properties created upon table creation and migration between
different versions of metastore.
+	private static final String FLINK_PROPERTY_PREFIX = "flink.";
+
+	// Flink tables should be stored as 'external' tables in Hive metastore
+	private static final Map<String, String> EXTERNAL_TABLE_PROPERTY = new HashMap<String,
String>() {{
+		put("EXTERNAL", "TRUE");
+	}};
+
 	private GenericHiveMetastoreCatalogUtil() {
 	}
 
 	// ------ Utils ------
 
 	/**
-	 * Creates a Hive database from CatalogDatabase.
+	 * Creates a Hive database from a CatalogDatabase.
+	 *
+	 * @param databaseName name of the database
+	 * @param catalogDatabase the CatalogDatabase instance
+	 * @return a Hive database
 	 */
-	public static Database createHiveDatabase(String dbName, CatalogDatabase db) {
-		Map<String, String> props = db.getProperties();
+	public static Database createHiveDatabase(String databaseName, CatalogDatabase catalogDatabase)
{
 		return new Database(
-			dbName,
-			db.getDescription().isPresent() ? db.getDescription().get() : null,
+			databaseName,
+			catalogDatabase.getDescription().isPresent() ? catalogDatabase.getDescription().get()
: null,
 			null,
-			props);
+			catalogDatabase.getProperties());
+	}
+
+	/**
+	 * Creates a Hive table from a CatalogBaseTable.
+	 *
+	 * @param tablePath path of the table
+	 * @param table the CatalogBaseTable instance
+	 * @return a Hive table
+	 */
+	public static Table createHiveTable(ObjectPath tablePath, CatalogBaseTable table) {
+		Map<String, String> properties = new HashMap<>(table.getProperties());
+
+		// Table description
+		if (table.getDescription().isPresent()) {
+			properties.put(HiveTableConfig.TABLE_DESCRITPION, table.getDescription().get());
+		}
+
+		Table hiveTable = new Table();
+		hiveTable.setDbName(tablePath.getDatabaseName());
+		hiveTable.setTableName(tablePath.getObjectName());
+		hiveTable.setCreateTime((int) (System.currentTimeMillis() / 1000));
+
+		// Table properties
+		hiveTable.setParameters(buildFlinkProperties(properties));
+		hiveTable.getParameters().putAll(EXTERNAL_TABLE_PROPERTY);
+
+		// Hive table's StorageDescriptor
+		StorageDescriptor sd = new StorageDescriptor();
+		sd.setSerdeInfo(new SerDeInfo(null, null, new HashMap<>()));
+
+		List<FieldSchema> allColumns = createHiveColumns(table.getSchema());
+
+		// Table columns and partition keys
+		CatalogTable catalogTable = (CatalogTable) table;
+
+		if (catalogTable.isPartitioned()) {
+			int partitionKeySize = catalogTable.getPartitionKeys().size();
+			List<FieldSchema> regularColumns = allColumns.subList(0, allColumns.size() - partitionKeySize);
+			List<FieldSchema> partitionColumns = allColumns.subList(allColumns.size() - partitionKeySize,
allColumns.size());
+
+			sd.setCols(regularColumns);
+			hiveTable.setPartitionKeys(partitionColumns);
+		} else {
+			sd.setCols(allColumns);
+			hiveTable.setPartitionKeys(new ArrayList<>());
+		}
+
+		hiveTable.setSd(sd);
+
+		if (table instanceof CatalogTable) {
+			// TODO: [FLINK-12240] Support view related operations in GenericHiveMetastoreCatalog
+			throw new UnsupportedOperationException();
+		}
+
+		return hiveTable;
+	}
+
+	/**
+	 * Creates a CatalogBaseTable from a Hive table.
+	 *
+	 * @param hiveTable the Hive table
+	 * @return a CatalogBaseTable
+	 */
+	public static CatalogBaseTable createCatalogTable(Table hiveTable) {
 
 Review comment:
   Yes, and that'll be taken care of in [[FLINK-12240] Support view related operations in
GenericHiveMetastoreCatalog](https://issues.apache.org/jira/browse/FLINK-12240). I've also
added TODOs here

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message