Search in sources :

Example 76 with SQLException

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

the class TestJdbcWithLocalClusterSpark method testTempTable.

@Test
public void testTempTable() throws Exception {
    // Create temp table with current connection
    String tempTableName = "tmp1";
    stmt.execute("CREATE TEMPORARY TABLE " + tempTableName + " (key string, value string)");
    stmt.execute("load data local inpath '" + dataFilePath.toString() + "' into table " + tempTableName);
    String resultVal = "val_238";
    String queryStr = "SELECT * FROM " + tempTableName + " where value = '" + resultVal + "'";
    verifyResult(queryStr, resultVal, 2);
    // A second connection should not be able to see the table
    Connection conn2 = DriverManager.getConnection(miniHS2.getJdbcURL(dbName), System.getProperty("user.name"), "bar");
    Statement stmt2 = conn2.createStatement();
    stmt2.execute("USE " + dbName);
    boolean gotException = false;
    try {
        stmt2.executeQuery(queryStr);
    } catch (SQLException err) {
        // This is expected to fail.
        assertTrue("Expecting table not found error, instead got: " + err, err.getMessage().contains("Table not found"));
        gotException = true;
    }
    assertTrue("Exception while querying non-existing temp table", gotException);
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Example 77 with SQLException

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

the class TestJdbcWithMiniHS2 method testUdfWhiteBlackList.

/**
   * Test UDF whitelist
   * - verify default value
   * - verify udf allowed with default whitelist
   * - verify udf allowed with specific whitelist
   * - verify udf disallowed when not in whitelist
   * @throws Exception
   */
@Test
public void testUdfWhiteBlackList() throws Exception {
    HiveConf testConf = new HiveConf();
    assertTrue(testConf.getVar(ConfVars.HIVE_SERVER2_BUILTIN_UDF_WHITELIST).isEmpty());
    // verify that udf in default whitelist can be executed
    Statement stmt = conDefault.createStatement();
    stmt.executeQuery("SELECT substr('foobar', 4) ");
    stmt.close();
    // setup whitelist
    stopMiniHS2();
    Set<String> funcNames = FunctionRegistry.getFunctionNames();
    funcNames.remove("reflect");
    String funcNameStr = "";
    for (String funcName : funcNames) {
        funcNameStr += "," + funcName;
    }
    // remove ',' at begining
    funcNameStr = funcNameStr.substring(1);
    testConf.setVar(ConfVars.HIVE_SERVER2_BUILTIN_UDF_WHITELIST, funcNameStr);
    startMiniHS2(testConf);
    Connection conn = getConnection(miniHS2.getJdbcURL(testDbName), System.getProperty("user.name"), "bar");
    stmt = conn.createStatement();
    // verify that udf in whitelist can be executed
    stmt.executeQuery("SELECT substr('foobar', 3) ");
    // verify that udf not in whitelist fails
    try {
        stmt.executeQuery("SELECT reflect('java.lang.String', 'valueOf', 1) ");
        fail("reflect() udf invocation should fail");
    } catch (SQLException e) {
    // expected
    }
    conn.close();
    // Restore original state
    restoreMiniHS2AndConnections();
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) HiveConf(org.apache.hadoop.hive.conf.HiveConf) Test(org.junit.Test)

Example 78 with SQLException

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

the class TestJdbcDriver2 method testQueryTimeout.

@Test
public void testQueryTimeout() throws Exception {
    String udfName = SleepMsUDF.class.getName();
    Statement stmt1 = con.createStatement();
    stmt1.execute("create temporary function sleepMsUDF as '" + udfName + "'");
    stmt1.close();
    Statement stmt = con.createStatement();
    // Test a query where timeout kicks in
    // Set query timeout to 1 second
    stmt.setQueryTimeout(1);
    System.err.println("Executing query: ");
    try {
        // The test table has 500 rows, so total query time should be ~ 2500ms
        stmt.executeQuery("select sleepMsUDF(t1.under_col, 5) as u0, t1.under_col as u1, " + "t2.under_col as u2 from " + tableName + " t1 join " + tableName + " t2 on t1.under_col = t2.under_col");
        fail("Expecting SQLTimeoutException");
    } catch (SQLTimeoutException e) {
        assertNotNull(e);
        System.err.println(e.toString());
    } catch (SQLException e) {
        fail("Expecting SQLTimeoutException, but got SQLException: " + e);
        e.printStackTrace();
    }
    // Test a query where timeout does not kick in. Set it to 5s;
    // show tables should be faster than that
    stmt.setQueryTimeout(5);
    try {
        stmt.executeQuery("show tables");
    } catch (SQLException e) {
        fail("Unexpected SQLException: " + e);
        e.printStackTrace();
    }
    stmt.close();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SQLTimeoutException(java.sql.SQLTimeoutException) String(java.lang.String) Test(org.junit.Test)

Example 79 with SQLException

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

the class TestJdbcDriver2 method testResultSetColumnNameCaseInsensitive.

@Test
public void testResultSetColumnNameCaseInsensitive() throws SQLException {
    Statement stmt = con.createStatement();
    ResultSet res;
    res = stmt.executeQuery("select c1 from " + dataTypeTableName + " limit 1");
    try {
        int count = 0;
        while (res.next()) {
            res.findColumn("c1");
            res.findColumn("C1");
            count++;
        }
        assertEquals(count, 1);
    } catch (Exception e) {
        String msg = "Unexpected exception: " + e;
        LOG.info(msg, e);
        fail(msg);
    }
    res = stmt.executeQuery("select c1 C1 from " + dataTypeTableName + " limit 1");
    try {
        int count = 0;
        while (res.next()) {
            res.findColumn("c1");
            res.findColumn("C1");
            count++;
        }
        assertEquals(count, 1);
    } catch (Exception e) {
        String msg = "Unexpected exception: " + e;
        LOG.info(msg, e);
        fail(msg);
    }
    stmt.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) String(java.lang.String) SQLTimeoutException(java.sql.SQLTimeoutException) ParseException(java.text.ParseException) Exception(java.lang.Exception) SQLException(java.sql.SQLException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 80 with SQLException

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

the class TestJdbcDriver2 method testGetQueryLog.

/**
   * Test getting query log method in Jdbc
   * @throws Exception
   */
@Test
public void testGetQueryLog() throws Exception {
    // Prepare
    String[] expectedLogs = { "Compiling command", "Completed compiling command", "Starting Semantic Analysis", "Semantic Analysis Completed", "Executing command", "Completed executing command" };
    String sql = "select count(*) from " + tableName;
    // Verify the fetched log (from the beginning of log file)
    HiveStatement stmt = (HiveStatement) con.createStatement();
    assertNotNull("Statement is null", stmt);
    stmt.executeQuery(sql);
    List<String> logs = stmt.getQueryLog(false, 10000);
    stmt.close();
    verifyFetchedLog(logs, expectedLogs);
    // Verify the fetched log (incrementally)
    final HiveStatement statement = (HiveStatement) con.createStatement();
    assertNotNull("Statement is null", statement);
    statement.setFetchSize(10000);
    final List<String> incrementalLogs = new ArrayList<String>();
    Runnable logThread = new Runnable() {

        @Override
        public void run() {
            while (statement.hasMoreLogs()) {
                try {
                    incrementalLogs.addAll(statement.getQueryLog());
                    Thread.sleep(500);
                } catch (SQLException e) {
                    LOG.error("Failed getQueryLog. Error message: " + e.getMessage());
                    fail("error in getting log thread");
                } catch (InterruptedException e) {
                    LOG.error("Getting log thread is interrupted. Error message: " + e.getMessage());
                    fail("error in getting log thread");
                }
            }
        }
    };
    Thread thread = new Thread(logThread);
    thread.setDaemon(true);
    thread.start();
    statement.executeQuery(sql);
    thread.interrupt();
    thread.join(10000);
    // fetch remaining logs
    List<String> remainingLogs;
    do {
        remainingLogs = statement.getQueryLog();
        incrementalLogs.addAll(remainingLogs);
    } while (remainingLogs.size() > 0);
    statement.close();
    verifyFetchedLog(incrementalLogs, expectedLogs);
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) String(java.lang.String) Test(org.junit.Test)

Aggregations

SQLException (java.sql.SQLException)6792 PreparedStatement (java.sql.PreparedStatement)3048 ResultSet (java.sql.ResultSet)2426 Connection (java.sql.Connection)1871 ArrayList (java.util.ArrayList)972 Test (org.junit.Test)873 Statement (java.sql.Statement)779 IOException (java.io.IOException)341 List (java.util.List)335 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)298 Properties (java.util.Properties)255 DatabaseException (net.jforum.exceptions.DatabaseException)249 HashMap (java.util.HashMap)232 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)184 Timestamp (java.sql.Timestamp)171 CallableStatement (java.sql.CallableStatement)165 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)160 DalHints (com.ctrip.platform.dal.dao.DalHints)159 Map (java.util.Map)125 Date (java.util.Date)123