Search in sources :

Example 11 with Statement

use of java.sql.Statement in project hive by apache.

the class TestTxnHandler method updateLocks.

private void updateLocks(Connection conn) throws SQLException {
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("update HIVE_LOCKS set hl_last_heartbeat = hl_last_heartbeat + 1");
}
Also used : Statement(java.sql.Statement)

Example 12 with Statement

use of java.sql.Statement in project hive by apache.

the class Commands method getConfInternal.

/**
   * Use call statement to retrieve the configurations for substitution and sql for the substitution.
   *
   * @param call
   * @return
   */
private BufferedRows getConfInternal(boolean call) {
    Statement stmnt = null;
    BufferedRows rows = null;
    try {
        boolean hasResults = false;
        DatabaseConnection dbconn = beeLine.getDatabaseConnection();
        Connection conn = null;
        if (dbconn != null)
            conn = dbconn.getConnection();
        if (conn != null) {
            if (call) {
                stmnt = conn.prepareCall("set");
                hasResults = ((CallableStatement) stmnt).execute();
            } else {
                stmnt = beeLine.createStatement();
                hasResults = stmnt.execute("set");
            }
        }
        if (hasResults) {
            ResultSet rs = stmnt.getResultSet();
            rows = new BufferedRows(beeLine, rs);
        }
    } catch (SQLException e) {
        beeLine.error(e);
    } finally {
        if (stmnt != null) {
            try {
                stmnt.close();
            } catch (SQLException e1) {
                beeLine.error(e1);
            }
        }
    }
    return rows;
}
Also used : SQLException(java.sql.SQLException) HiveStatement(org.apache.hive.jdbc.HiveStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 13 with Statement

use of java.sql.Statement in project hive by apache.

the class Commands method executeInternal.

// Return false only occurred error when execution the sql and the sql should follow the rules
// of beeline.
private boolean executeInternal(String sql, boolean call) {
    if (!beeLine.isBeeLine()) {
        sql = cliToBeelineCmd(sql);
    }
    if (sql == null || sql.length() == 0) {
        return true;
    }
    if (beeLine.isComment(sql)) {
        //skip this and rest cmds in the line
        return true;
    }
    // is source CMD
    if (isSourceCMD(sql)) {
        return sourceFile(sql);
    }
    if (sql.startsWith(BeeLine.COMMAND_PREFIX)) {
        return beeLine.execCommandWithPrefix(sql);
    }
    String prefix = call ? "call" : "sql";
    if (sql.startsWith(prefix)) {
        sql = sql.substring(prefix.length());
    }
    // batch statements?
    if (beeLine.getBatch() != null) {
        beeLine.getBatch().add(sql);
        return true;
    }
    if (!(beeLine.assertConnection())) {
        return false;
    }
    ClientHook hook = ClientCommandHookFactory.get().getHook(beeLine, sql);
    try {
        Statement stmnt = null;
        boolean hasResults;
        Thread logThread = null;
        try {
            long start = System.currentTimeMillis();
            if (call) {
                stmnt = beeLine.getDatabaseConnection().getConnection().prepareCall(sql);
                hasResults = ((CallableStatement) stmnt).execute();
            } else {
                stmnt = beeLine.createStatement();
                if (beeLine.getOpts().isSilent()) {
                    hasResults = stmnt.execute(sql);
                } else {
                    InPlaceUpdateStream.EventNotifier eventNotifier = new InPlaceUpdateStream.EventNotifier();
                    logThread = new Thread(createLogRunnable(stmnt, eventNotifier));
                    logThread.setDaemon(true);
                    logThread.start();
                    if (stmnt instanceof HiveStatement) {
                        HiveStatement hiveStatement = (HiveStatement) stmnt;
                        hiveStatement.setInPlaceUpdateStream(new BeelineInPlaceUpdateStream(beeLine.getErrorStream(), eventNotifier));
                    }
                    hasResults = stmnt.execute(sql);
                    logThread.interrupt();
                    logThread.join(DEFAULT_QUERY_PROGRESS_THREAD_TIMEOUT);
                }
            }
            beeLine.showWarnings();
            if (hasResults) {
                do {
                    ResultSet rs = stmnt.getResultSet();
                    try {
                        int count = beeLine.print(rs);
                        long end = System.currentTimeMillis();
                        beeLine.info(beeLine.loc("rows-selected", count) + " " + beeLine.locElapsedTime(end - start));
                    } finally {
                        if (logThread != null) {
                            logThread.join(DEFAULT_QUERY_PROGRESS_THREAD_TIMEOUT);
                            showRemainingLogsIfAny(stmnt);
                            logThread = null;
                        }
                        rs.close();
                    }
                } while (BeeLine.getMoreResults(stmnt));
            } else {
                int count = stmnt.getUpdateCount();
                long end = System.currentTimeMillis();
                beeLine.info(beeLine.loc("rows-affected", count) + " " + beeLine.locElapsedTime(end - start));
            }
        } finally {
            if (logThread != null) {
                if (!logThread.isInterrupted()) {
                    logThread.interrupt();
                }
                logThread.join(DEFAULT_QUERY_PROGRESS_THREAD_TIMEOUT);
                showRemainingLogsIfAny(stmnt);
            }
            if (stmnt != null) {
                stmnt.close();
            }
        }
    } catch (Exception e) {
        return beeLine.error(e);
    }
    beeLine.showWarnings();
    if (hook != null) {
        hook.postHook(beeLine);
    }
    return true;
}
Also used : HiveStatement(org.apache.hive.jdbc.HiveStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) IOException(java.io.IOException) BeelineInPlaceUpdateStream(org.apache.hive.beeline.logs.BeelineInPlaceUpdateStream) InPlaceUpdateStream(org.apache.hive.jdbc.logs.InPlaceUpdateStream) ResultSet(java.sql.ResultSet) HiveStatement(org.apache.hive.jdbc.HiveStatement) BeelineInPlaceUpdateStream(org.apache.hive.beeline.logs.BeelineInPlaceUpdateStream)

Example 14 with Statement

use of java.sql.Statement in project hive by apache.

the class ProxyAuthTest method exStatement.

// Execute the given sql statement
private static void exStatement(String query) throws Exception {
    Statement stmt = con.createStatement();
    stmt.execute(query);
    if (!noClose) {
        stmt.close();
    }
}
Also used : Statement(java.sql.Statement)

Example 15 with Statement

use of java.sql.Statement in project hive by apache.

the class HiveSchemaTool method validateColumnNullValues.

boolean validateColumnNullValues(Connection conn) throws HiveMetaException {
    System.out.println("Validating columns for incorrect NULL values");
    boolean isValid = true;
    try {
        Statement stmt = conn.createStatement();
        String tblQuery = getDbCommandParser(dbType).needsQuotedIdentifier() ? ("select t.* from \"TBLS\" t WHERE t.\"SD_ID\" IS NULL and (t.\"TBL_TYPE\"='" + TableType.EXTERNAL_TABLE + "' or t.\"TBL_TYPE\"='" + TableType.MANAGED_TABLE + "')") : ("select t.* from TBLS t WHERE t.SD_ID IS NULL and (t.TBL_TYPE='" + TableType.EXTERNAL_TABLE + "' or t.TBL_TYPE='" + TableType.MANAGED_TABLE + "')");
        ResultSet res = stmt.executeQuery(tblQuery);
        while (res.next()) {
            long tableId = res.getLong("TBL_ID");
            String tableName = res.getString("TBL_NAME");
            String tableType = res.getString("TBL_TYPE");
            isValid = false;
            System.err.println("SD_ID in TBLS should not be NULL for Table Name=" + tableName + ", Table ID=" + tableId + ", Table Type=" + tableType);
        }
        System.out.println((isValid ? "Succeeded" : "Failed") + " in column validation for incorrect NULL values");
        return isValid;
    } catch (SQLException e) {
        throw new HiveMetaException("Failed to validate columns for incorrect NULL values", e);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) HiveMetaException(org.apache.hadoop.hive.metastore.HiveMetaException)

Aggregations

Statement (java.sql.Statement)2195 Connection (java.sql.Connection)1082 ResultSet (java.sql.ResultSet)1081 PreparedStatement (java.sql.PreparedStatement)957 SQLException (java.sql.SQLException)911 Test (org.junit.Test)547 ArrayList (java.util.ArrayList)152 CallableStatement (java.sql.CallableStatement)128 ResultSetMetaData (java.sql.ResultSetMetaData)122 Properties (java.util.Properties)110 IOException (java.io.IOException)85 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)81 DruidPooledStatement (com.alibaba.druid.pool.DruidPooledStatement)71 DataSource (javax.sql.DataSource)62 HashMap (java.util.HashMap)61 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)56 DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)47 Context (javax.naming.Context)42 MockConnection (com.alibaba.druid.mock.MockConnection)41 DatabaseMetaData (java.sql.DatabaseMetaData)40