Search in sources :

Example 26 with Statement

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

the class CompactionTxnHandler method cleanEmptyAbortedTxns.

/**
   * Clean up aborted transactions from txns that have no components in txn_components.  The reason such
   * txns exist can be that now work was done in this txn (e.g. Streaming opened TransactionBatch and
   * abandoned it w/o doing any work) or due to {@link #markCleaned(CompactionInfo)} being called.
   */
@Override
@RetrySemantics.SafeToRetry
public void cleanEmptyAbortedTxns() throws MetaException {
    try {
        Connection dbConn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //Aborted is a terminal state, so nothing about the txn can change
            //after that, so READ COMMITTED is sufficient.
            dbConn = getDbConn(Connection.TRANSACTION_READ_COMMITTED);
            stmt = dbConn.createStatement();
            String s = "select txn_id from TXNS where " + "txn_id not in (select tc_txnid from TXN_COMPONENTS) and " + "txn_state = '" + TXN_ABORTED + "'";
            LOG.debug("Going to execute query <" + s + ">");
            rs = stmt.executeQuery(s);
            List<Long> txnids = new ArrayList<>();
            while (rs.next()) txnids.add(rs.getLong(1));
            close(rs);
            if (txnids.size() <= 0) {
                return;
            }
            //easier to read logs
            Collections.sort(txnids);
            List<String> queries = new ArrayList<String>();
            StringBuilder prefix = new StringBuilder();
            StringBuilder suffix = new StringBuilder();
            prefix.append("delete from TXNS where ");
            suffix.append("");
            TxnUtils.buildQueryWithINClause(conf, queries, prefix, suffix, txnids, "txn_id", false, false);
            for (String query : queries) {
                LOG.debug("Going to execute update <" + query + ">");
                int rc = stmt.executeUpdate(query);
                LOG.info("Removed " + rc + "  empty Aborted transactions from TXNS");
            }
            LOG.info("Aborted transactions removed from TXNS: " + txnids);
            LOG.debug("Going to commit");
            dbConn.commit();
        } catch (SQLException e) {
            LOG.error("Unable to delete from txns table " + e.getMessage());
            LOG.debug("Going to rollback");
            rollbackDBConn(dbConn);
            checkRetryable(dbConn, e, "cleanEmptyAbortedTxns");
            throw new MetaException("Unable to connect to transaction database " + StringUtils.stringifyException(e));
        } finally {
            close(rs, stmt, dbConn);
        }
    } catch (RetryException e) {
        cleanEmptyAbortedTxns();
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) MetaException(org.apache.hadoop.hive.metastore.api.MetaException)

Example 27 with Statement

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

the class TestTxnUtils method runAgainstDerby.

/** Verify queries can run against Derby DB.
   *  As long as Derby doesn't complain, we assume the query is syntactically/semantically correct.
   */
private void runAgainstDerby(List<String> queries) throws Exception {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        conn = TxnDbUtil.getConnection();
        stmt = conn.createStatement();
        for (String query : queries) {
            rs = stmt.executeQuery(query);
            Assert.assertTrue("The query is not valid", rs.next());
        }
    } finally {
        TxnDbUtil.closeResources(conn, stmt, rs);
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 28 with Statement

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

the class ThriftCliServiceMessageSizeTest method testMessageSize.

@Test
public void testMessageSize() throws Exception {
    String transportMode = "binary";
    hiveConf.setBoolVar(ConfVars.HIVE_SERVER2_ENABLE_DOAS, false);
    hiveConf.setVar(ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST, host);
    hiveConf.setIntVar(ConfVars.HIVE_SERVER2_THRIFT_PORT, port);
    hiveConf.setVar(ConfVars.HIVE_SERVER2_AUTHENTICATION, AuthTypes.NONE.toString());
    hiveConf.setVar(ConfVars.HIVE_SERVER2_TRANSPORT_MODE, transportMode);
    HiveServer2 hiveServer2 = new HiveServer2();
    String url = "jdbc:hive2://localhost:" + port + "/default";
    Class.forName("org.apache.hive.jdbc.HiveDriver");
    try {
        // First start HS2 with high message size limit. This should allow connections
        hiveConf.setIntVar(ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE, 100 * 1024 * 1024);
        startHiveServer2WithConf(hiveServer2, hiveConf);
        System.out.println("Started Thrift CLI service with message size limit " + hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE));
        // With the high message size limit this connection should work
        Connection connection = DriverManager.getConnection(url, "hiveuser", "hive");
        Statement stmt = connection.createStatement();
        assertNotNull("Statement is null", stmt);
        stmt.execute("set hive.support.concurrency = false");
        connection.close();
        stopHiveServer2(hiveServer2);
        // Now start HS2 with low message size limit. This should prevent any connections
        hiveConf.setIntVar(ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE, 1);
        hiveServer2 = new HiveServer2();
        startHiveServer2WithConf(hiveServer2, hiveConf);
        System.out.println("Started Thrift CLI service with message size limit " + hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE));
        Exception caughtException = null;
        try {
            // This should fail
            connection = DriverManager.getConnection(url, "hiveuser", "hive");
        } catch (Exception err) {
            caughtException = err;
        }
        // Verify we hit an error while connecting
        assertNotNull(caughtException);
    } finally {
        stopHiveServer2(hiveServer2);
        hiveServer2 = null;
    }
}
Also used : HiveServer2(org.apache.hive.service.server.HiveServer2) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Example 29 with Statement

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

the class TestHS2ImpersonationWithRemoteMS method testImpersonation.

@Test
public void testImpersonation() throws Exception {
    assertTrue("Test setup failed. MiniHS2 is not initialized", miniHS2 != null && miniHS2.isStarted());
    Class.forName(MiniHS2.getJdbcDriverName());
    // Create two tables one as user "foo" and other as user "bar"
    Connection hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL(), "foo", null);
    Statement stmt = hs2Conn.createStatement();
    String tableName = "foo_table";
    stmt.execute("drop table if exists " + tableName);
    stmt.execute("create table " + tableName + " (value string)");
    stmt.close();
    hs2Conn.close();
    hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL(), "bar", null);
    stmt = hs2Conn.createStatement();
    tableName = "bar_table";
    stmt.execute("drop table if exists " + tableName);
    stmt.execute("create table " + tableName + " (value string)");
    stmt.close();
    hs2Conn.close();
    MiniDFSShim dfs = miniHS2.getDfs();
    FileSystem fs = dfs.getFileSystem();
    FileStatus[] files = fs.listStatus(miniHS2.getWareHouseDir());
    boolean fooTableValidated = false;
    boolean barTableValidated = false;
    for (FileStatus file : files) {
        final String name = file.getPath().getName();
        final String owner = file.getOwner();
        if (name.equals("foo_table")) {
            fooTableValidated = owner.equals("foo");
            assertTrue(String.format("User 'foo' table has wrong ownership '%s'", owner), fooTableValidated);
        } else if (name.equals("bar_table")) {
            barTableValidated = owner.equals("bar");
            assertTrue(String.format("User 'bar' table has wrong ownership '%s'", owner), barTableValidated);
        } else {
            fail(String.format("Unexpected table directory '%s' in warehouse", name));
        }
        System.out.println(String.format("File: %s, Owner: %s", name, owner));
    }
    assertTrue("User 'foo' table not found in warehouse", fooTableValidated);
    assertTrue("User 'bar' table not found in warehouse", barTableValidated);
}
Also used : MiniDFSShim(org.apache.hadoop.hive.shims.HadoopShims.MiniDFSShim) FileStatus(org.apache.hadoop.fs.FileStatus) Statement(java.sql.Statement) FileSystem(org.apache.hadoop.fs.FileSystem) Connection(java.sql.Connection) Test(org.junit.Test)

Example 30 with Statement

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

the class cbo_rp_TestJdbcDriver2 method testErrorDiag.

@Test
public void testErrorDiag() throws SQLException {
    Statement stmt = con.createStatement();
    // verify syntax error
    try {
        stmt.executeQuery("select from " + dataTypeTableName);
        fail("SQLException is expected");
    } catch (SQLException e) {
        assertEquals("42000", e.getSQLState());
    }
    // verify table not fuond error
    try {
        stmt.executeQuery("select * from nonTable");
        fail("SQLException is expected");
    } catch (SQLException e) {
        assertEquals("42S02", e.getSQLState());
    }
    // verify invalid column error
    try {
        stmt.executeQuery("select zzzz from " + dataTypeTableName);
        fail("SQLException is expected");
    } catch (SQLException e) {
        assertEquals("42000", e.getSQLState());
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Test(org.junit.Test)

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