[ 
https://issues.apache.org/jira/browse/HIVE-25594?focusedWorklogId=670745&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-670745
 ]

ASF GitHub Bot logged work on HIVE-25594:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 27/Oct/21 14:02
            Start Date: 27/Oct/21 14:02
    Worklog Time Spent: 10m 
      Work Description: asolimando commented on a change in pull request #2742:
URL: https://github.com/apache/hive/pull/2742#discussion_r737501273



##########
File path: 
itests/util/src/main/java/org/apache/hadoop/hive/ql/externalDB/AbstractExternalDB.java
##########
@@ -193,96 +147,62 @@ public final String getContainerHostAddress() {
         }
     }
 
-    public abstract void setJdbcUrl(String hostAddress);
-
-    public abstract void setJdbcDriver();
-
-    public abstract String getDockerImageName();
-
-    public abstract String[] getDockerAdditionalArgs();
-
-    public abstract boolean isContainerReady(ProcessResults pr);
+    /**
+     * Return the name of the root user.
+     * 
+     * Override the method if the name of the root user must be different than 
the default.
+     */
+    protected String getRootUser() {
+        return "qtestuser";
+    }
 
-    protected String[] buildArray(String... strs) {
-        return strs;
+    /**
+     * Return the password of the root user.
+     * 
+     * Override the method if the password must be different than the default.
+     */
+    protected String getRootPassword() {
+        return  "qtestpassword";
     }
+    
+    protected abstract String getJdbcUrl();
 
-    public Connection getConnectionToExternalDB() throws SQLException, 
ClassNotFoundException {
-        try {
-            LOG.info("external database connection URL:\t " + url);
-            LOG.info("JDBC Driver :\t " + driver);
-            LOG.info("external database connection User:\t " + userName);
-            LOG.info("external database connection Password:\t " + password);
+    protected abstract String getJdbcDriver();
 
-            // load required JDBC driver
-            Class.forName(driver);
+    protected abstract String getDockerImageName();
 
-            // Connect using the JDBC URL and user/password
-            Connection conn = DriverManager.getConnection(url, userName, 
password);
-            return conn;
-        } catch (SQLException e) {
-            LOG.error("Failed to connect to external databse", e);
-            throw new SQLException(e);
-        } catch (ClassNotFoundException e) {
-            LOG.error("Unable to find driver class", e);
-            throw new ClassNotFoundException("Unable to find driver class");
-        }
-    }
+    protected abstract String[] getDockerAdditionalArgs();
 
-    public void testConnectionToExternalDB() throws SQLException, 
ClassNotFoundException {
-        Connection conn = getConnectionToExternalDB();
-        try {
-            conn.close();
-        } catch (SQLException e) {
-            LOG.error("Failed to close external database connection", e);
-        }
-    }
+    protected abstract boolean isContainerReady(ProcessResults pr);
 
-    protected String[] SQLLineCmdBuild(String sqlScriptFile) throws 
IOException {
-        return new String[] {"-u", url,
-                            "-d", driver,
-                            "-n", userName,
-                            "-p", password,
+    private String[] SQLLineCmdBuild(String sqlScriptFile) {
+        return new String[] {"-u", getJdbcUrl(),
+                            "-d", getJdbcDriver(),
+                            "-n", getRootUser(),
+                            "-p", getRootPassword(),
                             "--isolation=TRANSACTION_READ_COMMITTED",
                             "-f", sqlScriptFile};
 
     }
 
-    protected void execSql(String sqlScriptFile) throws IOException {
-        // run the script using SqlLine
-        SqlLine sqlLine = new SqlLine();
-        ByteArrayOutputStream outputForLog = null;
-        OutputStream out;
-        if (LOG.isDebugEnabled()) {
-            out = outputForLog = new ByteArrayOutputStream();
-        } else {
-            out = new NullOutputStream();
+    public void execute(String script) throws IOException, SQLException, 
ClassNotFoundException {
+        // Test we can connect to database
+        Class.forName(getJdbcDriver());
+        try (Connection ignored = DriverManager.getConnection(getJdbcUrl(), 
getRootUser(), getRootPassword())) {
+            LOG.info("Successfully connected to {} with user {} and password 
{}", getJdbcUrl(), getRootUser(), getRootPassword());
         }
+        LOG.info("Starting {} initialization", getClass().getSimpleName());
+        SqlLine sqlLine = new SqlLine();
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
         sqlLine.setOutputStream(new PrintStream(out));
+        sqlLine.setErrorStream(new PrintStream(out));
         System.setProperty("sqlline.silent", "true");
-
-        SqlLine.Status status = sqlLine.begin(SQLLineCmdBuild(sqlScriptFile), 
null, false);
-        if (LOG.isDebugEnabled() && outputForLog != null) {
-            LOG.debug("Received following output from Sqlline:");
-            LOG.debug(outputForLog.toString("UTF-8"));
-        }
+        SqlLine.Status status = sqlLine.begin(SQLLineCmdBuild(script), null, 
false);
+        LOG.debug("Printing output from SQLLine:");
+        LOG.debug(out.toString());
         if (status != SqlLine.Status.OK) {
-            throw new IOException("external database script failed, errorcode 
" + status);
+            throw new RuntimeException("Database script " + script + " failed 
with status " + status);
         }
-    }
-
-    public void execute(String script) throws IOException, SQLException, 
ClassNotFoundException {
-        testConnectionToExternalDB();
-        LOG.info("Starting external database initialization to " + 
this.externalDBType);
-
-        try {
-            LOG.info("Initialization script " + script);
-            execSql(script);
-            LOG.info("Initialization script completed in external database");
-
-        } catch (IOException e) {
-            throw new IOException("initialization in external database 
FAILED!");
-        }
-
+        LOG.info("Completed {} initialization", getClass().getSimpleName());
     }
 }

Review comment:
       missing newline




-- 
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: gitbox-unsubscr...@hive.apache.org

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


Issue Time Tracking
-------------------

    Worklog Id:     (was: 670745)
    Time Spent: 40m  (was: 0.5h)

> Setup JDBC databases in tests via QT options
> --------------------------------------------
>
>                 Key: HIVE-25594
>                 URL: https://issues.apache.org/jira/browse/HIVE-25594
>             Project: Hive
>          Issue Type: Improvement
>          Components: Testing Infrastructure
>            Reporter: Stamatis Zampetakis
>            Assignee: Stamatis Zampetakis
>            Priority: Major
>              Labels: pull-request-available
>          Time Spent: 40m
>  Remaining Estimate: 0h
>
> The goal of this jira is to add a new QT option for setting up JDBC DBMS and 
> using it in qtests which need a JDBC endpoint up and running. It can be used 
> in tests with external JDBC tables, connectors, etc.
> A sample file using the proposed option ({{qt:database}}) is shown below.
> {code:sql}
> --!qt:database:postgres:init_sript_1234.sql:cleanup_script_1234.sql
> CREATE EXTERNAL TABLE country (name varchar(80))
> STORED BY 'org.apache.hive.storage.jdbc.JdbcStorageHandler'
> TBLPROPERTIES (
> "hive.sql.database.type" = "POSTGRES",
> "hive.sql.jdbc.driver" = "org.postgresql.Driver",
> "hive.sql.jdbc.url" = "jdbc:postgresql://localhost:5432/qtestDB",
> "hive.sql.dbcp.username" = "qtestuser",
> "hive.sql.dbcp.password" = "qtestpassword",
> "hive.sql.table" = "country");
> EXPLAIN CBO SELECT COUNT(*) from country;
> SELECT COUNT(*) from country;
> {code}
> This builds upon HIVE-25423 but proposes to use JDBC datasources without the 
> need for a using a specific CLI driver. Furthermore, the proposed QT option 
> syntax allows using customised init/cleanup scripts for the JDBC datasource 
> per test.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to