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 #8205: [FLINK-12238] [TABLE/SQL] Support database related operations in GenericHiveMetastoreCatalog and setup flink-connector-hive module
Date Fri, 19 Apr 2019 07:33:10 GMT
bowenli86 commented on a change in pull request #8205: [FLINK-12238] [TABLE/SQL] Support database
related operations in GenericHiveMetastoreCatalog and setup flink-connector-hive module
URL: https://github.com/apache/flink/pull/8205#discussion_r276919562
 
 

 ##########
 File path: flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/GenericHiveMetastoreCatalog.java
 ##########
 @@ -0,0 +1,259 @@
+/*
+ * 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.flink.table.catalog.hive;
+
+import org.apache.flink.table.catalog.CatalogBaseTable;
+import org.apache.flink.table.catalog.CatalogDatabase;
+import org.apache.flink.table.catalog.GenericCatalogDatabase;
+import org.apache.flink.table.catalog.ObjectPath;
+import org.apache.flink.table.catalog.ReadableWritableCatalog;
+import org.apache.flink.table.catalog.exceptions.CatalogException;
+import org.apache.flink.table.catalog.exceptions.DatabaseAlreadyExistException;
+import org.apache.flink.table.catalog.exceptions.DatabaseNotEmptyException;
+import org.apache.flink.table.catalog.exceptions.DatabaseNotExistException;
+import org.apache.flink.table.catalog.exceptions.TableAlreadyExistException;
+import org.apache.flink.table.catalog.exceptions.TableNotExistException;
+import org.apache.flink.util.StringUtils;
+
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.metastore.RetryingMetaStoreClient;
+import org.apache.hadoop.hive.metastore.api.AlreadyExistsException;
+import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hadoop.hive.metastore.api.InvalidOperationException;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A catalog that persists all Flink streaming and batch metadata by using Hive metastore
as a persistent storage.
+ */
+public class GenericHiveMetastoreCatalog implements ReadableWritableCatalog {
+	private static final Logger LOG = LoggerFactory.getLogger(GenericHiveMetastoreCatalog.class);
+
+	public static final String DEFAULT_DB = "default";
+
+	private final String catalogName;
+	private final HiveConf hiveConf;
+
+	private String currentDatabase = DEFAULT_DB;
+	private IMetaStoreClient client;
+
+	public GenericHiveMetastoreCatalog(String catalogName, String hivemetastoreURI) {
+		this(catalogName, getHiveConf(hivemetastoreURI));
+	}
+
+	public GenericHiveMetastoreCatalog(String catalogName, HiveConf hiveConf) {
+		checkArgument(!StringUtils.isNullOrWhitespaceOnly(catalogName), "catalogName cannot be
null or empty");
+		this.catalogName = catalogName;
+
+		this.hiveConf = checkNotNull(hiveConf, "hiveConf cannot be null");
+		LOG.info("Created GenericHiveMetastoreCatalog '{}'", catalogName);
+	}
+
+	private static HiveConf getHiveConf(String hiveMetastoreURI) {
+		checkArgument(!StringUtils.isNullOrWhitespaceOnly(hiveMetastoreURI), "hiveMetastoreURI
cannot be null or empty");
+
+		HiveConf hiveConf = new HiveConf();
+		hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, hiveMetastoreURI);
+		return hiveConf;
+	}
+
+	private static IMetaStoreClient getMetastoreClient(HiveConf hiveConf) {
+		try {
+			return RetryingMetaStoreClient.getProxy(
+				hiveConf,
+				null,
+				null,
+				HiveMetaStoreClient.class.getName(),
+				true);
+		} catch (MetaException e) {
+			throw new CatalogException("Failed to create Hive metastore client", e);
+		}
+	}
+
+	@Override
+	public void open() throws CatalogException {
+		if (client == null) {
+			client = getMetastoreClient(hiveConf);
+			LOG.info("Connected to Hive metastore");
+		}
+	}
+
+	@Override
+	public void close() throws CatalogException {
+		if (client != null) {
+			client.close();
+			client = null;
+			LOG.info("Close connection to Hive metastore");
+		}
+	}
+
+	// ------ databases ------
+
+	@Override
+	public String getCurrentDatabase() throws CatalogException {
+		return currentDatabase;
+	}
+
+	@Override
+	public void setCurrentDatabase(String databaseName) throws DatabaseNotExistException, CatalogException
{
+		checkArgument(!StringUtils.isNullOrWhitespaceOnly(databaseName));
+
+		if (!databaseExists(databaseName)) {
+			throw new DatabaseNotExistException(catalogName, databaseName);
+		}
+
+		currentDatabase = databaseName;
+	}
+
+	@Override
+	public List<String> listDatabases() throws CatalogException {
+		try {
+			return client.getAllDatabases();
+		} catch (TException e) {
+			throw new CatalogException(
+				String.format("Failed to list all databases in %s", catalogName), e);
+		}
+	}
+
+	@Override
+	public CatalogDatabase getDatabase(String databaseName) throws DatabaseNotExistException,
CatalogException {
+		Database hiveDb;
+
+		try {
+			hiveDb = client.getDatabase(databaseName);
+		} catch (NoSuchObjectException e) {
+			throw new DatabaseNotExistException(catalogName, databaseName);
+		} catch (TException e) {
+			throw new CatalogException(
+				String.format("Failed to get database %s from %s", databaseName, catalogName), e);
+		}
+
+		return new GenericCatalogDatabase(hiveDb.getParameters(), hiveDb.getDescription());
+	}
+
+	@Override
+	public boolean databaseExists(String databaseName) throws CatalogException {
+		try {
+			return client.getDatabase(databaseName) != null;
 
 Review comment:
   `client.getDatabase(databaseName) != null;` is used only for the purpose to return a boolean,
which will be `true` if the`x  != null` gets to run, for this method. `client.getDatabase()`
will throw `NoSuchObjectException` if the database doesn't exist, and we catch that to return
a `false`.

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