paul-rogers commented on a change in pull request #2060: DRILL-7706: Implement 
Drill RDBMS Metastore for Tables component
URL: https://github.com/apache/drill/pull/2060#discussion_r410831312
 
 

 ##########
 File path: 
metastore/rdbms-metastore/src/main/java/org/apache/drill/metastore/rdbms/RdbmsMetastore.java
 ##########
 @@ -0,0 +1,166 @@
+/*
+ * 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.drill.metastore.rdbms;
+
+import com.typesafe.config.Config;
+import com.zaxxer.hikari.HikariConfig;
+import com.zaxxer.hikari.HikariDataSource;
+import liquibase.Liquibase;
+import liquibase.database.Database;
+import liquibase.database.DatabaseFactory;
+import liquibase.database.jvm.JdbcConnection;
+import liquibase.resource.ClassLoaderResourceAccessor;
+import org.apache.drill.common.config.DrillConfig;
+import org.apache.drill.metastore.Metastore;
+import org.apache.drill.metastore.components.tables.Tables;
+import org.apache.drill.metastore.components.views.Views;
+import org.apache.drill.metastore.config.MetastoreConfigConstants;
+import org.apache.drill.metastore.rdbms.components.tables.RdbmsTables;
+import org.apache.drill.metastore.rdbms.config.RdbmsConfigConstants;
+import org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException;
+import org.jooq.SQLDialect;
+import org.jooq.tools.jdbc.JDBCUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.sql.Connection;
+import java.util.Properties;
+
+/**
+ * RDBMS Drill Metastore implementation that creates necessary tables using 
Liquibase,
+ * initializes data source using provided config.
+ */
+public class RdbmsMetastore implements Metastore {
+
+  private static final Logger logger = 
LoggerFactory.getLogger(RdbmsMetastore.class);
+
+  private static final String LIQUIBASE_CHANGELOG_FILE = 
"db/changelog/changelog.yaml";
+
+  private final QueryExecutorProvider executorProvider;
+
+  public RdbmsMetastore(DrillConfig config) {
+    HikariDataSource dataSource = dataSource(config);
+    this.executorProvider = new QueryExecutorProvider(dataSource);
+    initTables(dataSource);
+  }
+
+  @Override
+  public Tables tables() {
+    return new RdbmsTables(executorProvider);
+  }
+
+  @Override
+  public Views views() {
+    throw new UnsupportedOperationException("Views metadata support is not 
implemented");
+  }
+
+  @Override
+  public void close() {
+    executorProvider.close();
+  }
+
+  /**
+   * Prepares database before initializing data source based on its type,
+   * initializes {@link HikariDataSource} instance and configures it based on 
given
+   * Metastore configuration.
+   * Basic parameters such as driver, url, user name and password are set 
using setters.
+   * Other source parameters are set dynamically through the properties. See 
the list
+   * of available Hikari properties: <a 
href="https://github.com/brettwooldridge/HikariCP";>.
+   *
+   * @param config Metastore config
+   * @return Hikari data source instance
+   * @throws RdbmsMetastoreException if unable to configure Hikari data source
+   */
+  private HikariDataSource dataSource(DrillConfig config) {
+    prepareDatabase(config);
+    try {
+      Properties properties = new Properties();
+      if (config.hasPath(RdbmsConfigConstants.DATA_SOURCE_PROPERTIES)) {
+        Config propertiesConfig = 
config.getConfig(RdbmsConfigConstants.DATA_SOURCE_PROPERTIES);
+        propertiesConfig.entrySet().forEach(e -> properties.put(e.getKey(), 
e.getValue().unwrapped()));
+      }
+      HikariConfig hikariConfig = new HikariConfig(properties);
+      
hikariConfig.setDriverClassName(config.getString(RdbmsConfigConstants.DATA_SOURCE_DRIVER));
+      
hikariConfig.setJdbcUrl(config.getString(RdbmsConfigConstants.DATA_SOURCE_URL));
+      if (config.hasPath(RdbmsConfigConstants.DATA_SOURCE_USER_NAME)) {
+        
hikariConfig.setUsername(config.getString(RdbmsConfigConstants.DATA_SOURCE_USER_NAME));
+      }
+      if (config.hasPath(RdbmsConfigConstants.DATA_SOURCE_PASSWORD)) {
+        
hikariConfig.setPassword(config.getString(RdbmsConfigConstants.DATA_SOURCE_PASSWORD));
+      }
+      return new HikariDataSource(hikariConfig);
+    } catch (RuntimeException e) {
+      throw new RdbmsMetastoreException("Unable to init RDBMS Metastore data 
source: " + e.getMessage(), e);
+    }
+  }
+
+  /**
+   * Prepares database prior to data source configuration based on its type.
+   * For example, SQLite database requires database path to exists prior to 
database initialization.
+   *
+   * @param config Metastore config
+   */
+  private void prepareDatabase(DrillConfig config) {
+    SQLDialect dialect = 
JDBCUtils.dialect(config.getString(RdbmsConfigConstants.DATA_SOURCE_URL));
+    switch (dialect) {
+      case SQLITE:
 
 Review comment:
   This kind of approach will get complex as we add more DBs. Suggestion: 
define a `DbHelper` (pick a better name) interface with calls for 
`prepareDatabase()`, etc. Implement the interface for each DB that needs extra 
work. Provide a do-nothing default implementation for other DBs.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to