Search in sources :

Example 56 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project cdap by caskdata.

the class BaseHiveExploreService method createNamespace.

public QueryHandle createNamespace(NamespaceMeta namespaceMeta) throws ExploreException, SQLException {
    startAndWait();
    try {
        // This check prevents the extra warn log.
        if (NamespaceId.DEFAULT.equals(namespaceMeta.getNamespaceId())) {
            return QueryHandle.NO_OP;
        }
        Map<String, String> sessionConf = startSession();
        SessionHandle sessionHandle = null;
        OperationHandle operationHandle = null;
        try {
            sessionHandle = openHiveSession(sessionConf);
            QueryHandle handle;
            if (Strings.isNullOrEmpty(namespaceMeta.getConfig().getHiveDatabase())) {
                // if no custom hive database was provided get the hive database according to cdap format and create it
                // if one does not exists since cdap is responsible for managing the lifecycle of such databases
                String database = createHiveDBName(namespaceMeta.getName());
                // "IF NOT EXISTS" so that this operation is idempotent.
                String statement = String.format("CREATE DATABASE IF NOT EXISTS %s", database);
                operationHandle = executeAsync(sessionHandle, statement);
                handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, statement, database);
                LOG.info("Creating database {} with handle {}", database, handle);
            } else {
                // a custom database name was provided so check its existence
                // there is no way to check if a hive database exists or not other than trying to use it and see whether
                // it fails or not. So, run a USE databaseName command and see if it throws exception
                // Other way can be to list all database and check if the database exists or not but we are doing USE to
                // make sure that user can acutally use the database once we have impersonation.
                String statement = String.format("USE %s", namespaceMeta.getConfig().getHiveDatabase());
                // if the database does not exists the below line will throw exception from hive
                try {
                    operationHandle = executeAsync(sessionHandle, statement);
                } catch (HiveSQLException e) {
                    // then we will get an exception from Hive with error code 10072 which represent database was not found
                    if (e.toTStatus().getErrorCode() == ErrorMsg.DATABASE_NOT_EXISTS.getErrorCode()) {
                        //TODO: Add username here
                        throw new ExploreException(String.format("A custom Hive Database %s was provided for namespace %s " + "which does not exists. Please create the database in hive " + "for the user and try creating the namespace again.", namespaceMeta.getConfig().getHiveDatabase(), namespaceMeta.getName()), e);
                    } else {
                        // some other exception was generated while checking the existense of the database
                        throw new ExploreException(String.format("Failed to check existence of given custom hive database " + "%s for namespace %s", namespaceMeta.getConfig().getHiveDatabase(), namespaceMeta.getName()), e);
                    }
                }
                // if we didn't got an exception on the line above we know that the database exists
                handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, statement, namespaceMeta.getConfig().getHiveDatabase());
                LOG.debug("Custom database {} existence verified with handle {}", namespaceMeta.getConfig().getHiveDatabase(), handle);
            }
            return handle;
        } catch (Throwable e) {
            closeInternal(getQueryHandle(sessionConf), new ReadOnlyOperationInfo(sessionHandle, operationHandle, sessionConf, "", ""));
            throw e;
        }
    } catch (HiveSQLException e) {
        throw getSqlException(e);
    } catch (Throwable e) {
        throw new ExploreException(e);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) QueryHandle(co.cask.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 57 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project cdap by caskdata.

the class BaseHiveExploreService method getTables.

@Override
public QueryHandle getTables(String catalog, String schemaPattern, String tableNamePattern, List<String> tableTypes) throws ExploreException, SQLException {
    startAndWait();
    try {
        SessionHandle sessionHandle = null;
        OperationHandle operationHandle = null;
        Map<String, String> sessionConf = startSession();
        String database = getHiveDatabase(schemaPattern);
        try {
            sessionHandle = openHiveSession(sessionConf);
            operationHandle = cliService.getTables(sessionHandle, catalog, database, tableNamePattern, tableTypes);
            QueryHandle handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, "", database);
            LOG.trace("Retrieving tables: catalog {}, schemaNamePattern {}, tableNamePattern {}, tableTypes {}", catalog, database, tableNamePattern, tableTypes);
            return handle;
        } catch (Throwable e) {
            closeInternal(getQueryHandle(sessionConf), new ReadOnlyOperationInfo(sessionHandle, operationHandle, sessionConf, "", database));
            throw e;
        }
    } catch (HiveSQLException e) {
        throw getSqlException(e);
    } catch (Throwable e) {
        throw new ExploreException(e);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) QueryHandle(co.cask.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 58 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project cdap by caskdata.

the class BaseHiveExploreService method getTableTypes.

@Override
public QueryHandle getTableTypes() throws ExploreException, SQLException {
    startAndWait();
    try {
        SessionHandle sessionHandle = null;
        OperationHandle operationHandle = null;
        Map<String, String> sessionConf = startSession();
        try {
            sessionHandle = openHiveSession(sessionConf);
            operationHandle = cliService.getTableTypes(sessionHandle);
            QueryHandle handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, "", "");
            LOG.trace("Retrieving table types");
            return handle;
        } catch (Throwable e) {
            closeInternal(getQueryHandle(sessionConf), new ReadOnlyOperationInfo(sessionHandle, operationHandle, sessionConf, "", ""));
            throw e;
        }
    } catch (HiveSQLException e) {
        throw getSqlException(e);
    } catch (Throwable e) {
        throw new ExploreException(e);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) QueryHandle(co.cask.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 59 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project cdap by caskdata.

the class BaseHiveExploreService method execute.

@Override
public QueryHandle execute(NamespaceId namespace, String statement, @Nullable Map<String, String> additionalSessionConf) throws ExploreException, SQLException {
    startAndWait();
    try {
        SessionHandle sessionHandle = null;
        OperationHandle operationHandle = null;
        LOG.trace("Got statement '{}' with additional session configuration {}", statement, additionalSessionConf);
        Map<String, String> sessionConf = startSession(namespace, additionalSessionConf);
        String database = getHiveDatabase(namespace.getNamespace());
        try {
            sessionHandle = openHiveSession(sessionConf);
            // Switch database to the one being passed in.
            setCurrentDatabase(database);
            operationHandle = executeAsync(sessionHandle, statement);
            QueryHandle handle = saveReadWriteOperation(operationHandle, sessionHandle, sessionConf, statement, database);
            LOG.trace("Executing statement: {} with handle {}", statement, handle);
            return handle;
        } catch (Throwable e) {
            closeInternal(getQueryHandle(sessionConf), new ReadWriteOperationInfo(sessionHandle, operationHandle, sessionConf, "", database));
            throw e;
        }
    } catch (HiveSQLException e) {
        throw getSqlException(e);
    } catch (Throwable e) {
        throw new ExploreException(e);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) QueryHandle(co.cask.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 60 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project cdap by caskdata.

the class BaseHiveExploreService method getColumns.

@Override
public QueryHandle getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws ExploreException, SQLException {
    startAndWait();
    try {
        SessionHandle sessionHandle = null;
        OperationHandle operationHandle = null;
        Map<String, String> sessionConf = startSession();
        String database = getHiveDatabase(schemaPattern);
        try {
            sessionHandle = openHiveSession(sessionConf);
            operationHandle = cliService.getColumns(sessionHandle, catalog, database, tableNamePattern, columnNamePattern);
            QueryHandle handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, "", database);
            LOG.trace("Retrieving columns: catalog {}, schemaPattern {}, tableNamePattern {}, columnNamePattern {}", catalog, database, tableNamePattern, columnNamePattern);
            return handle;
        } catch (Throwable e) {
            closeInternal(getQueryHandle(sessionConf), new ReadOnlyOperationInfo(sessionHandle, operationHandle, sessionConf, "", database));
            throw e;
        }
    } catch (HiveSQLException e) {
        throw getSqlException(e);
    } catch (Throwable e) {
        throw new ExploreException(e);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) QueryHandle(co.cask.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) ExploreException(co.cask.cdap.explore.service.ExploreException)

Aggregations

SessionHandle (org.apache.hive.service.cli.SessionHandle)67 OperationHandle (org.apache.hive.service.cli.OperationHandle)38 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)36 Test (org.junit.Test)27 IOException (java.io.IOException)16 TException (org.apache.thrift.TException)16 UnknownHostException (java.net.UnknownHostException)14 LoginException (javax.security.auth.login.LoginException)14 ServiceException (org.apache.hive.service.ServiceException)14 ExploreException (co.cask.cdap.explore.service.ExploreException)12 QueryHandle (co.cask.cdap.proto.QueryHandle)11 HashMap (java.util.HashMap)9 CLIServiceClient (org.apache.hive.service.cli.CLIServiceClient)8 RowSet (org.apache.hive.service.cli.RowSet)6 TimeoutException (java.util.concurrent.TimeoutException)4 HiveConf (org.apache.hadoop.hive.conf.HiveConf)4 ThriftCLIServiceClient (org.apache.hive.service.cli.thrift.ThriftCLIServiceClient)4 CLIService (org.apache.hive.service.cli.CLIService)3 OperationState (org.apache.hive.service.cli.OperationState)3 OperationStatus (org.apache.hive.service.cli.OperationStatus)3