Search in sources :

Example 21 with ExploreException

use of io.cdap.cdap.explore.service.ExploreException in project cdap by caskdata.

the class ExploreHttpClient method getQueries.

@Override
public List<QueryInfo> getQueries(NamespaceId namespace) throws ExploreException, SQLException {
    String resource = String.format("namespaces/%s/data/explore/queries/", namespace.getEntityName());
    HttpResponse response = doGet(resource);
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return parseJson(response, QUERY_INFO_LIST_TYPE);
    }
    throw new ExploreException("Cannot get list of queries. Reason: " + response);
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 22 with ExploreException

use of io.cdap.cdap.explore.service.ExploreException in project cdap by caskdata.

the class BaseHiveExploreService method previewResults.

@Override
public List<QueryResult> previewResults(QueryHandle handle) throws ExploreException, HandleNotFoundException, SQLException {
    startAndWait();
    if (inactiveHandleCache.getIfPresent(handle) != null) {
        throw new HandleNotFoundException("Query is inactive.", true);
    }
    OperationInfo operationInfo = getActiveOperationInfo(handle);
    Lock previewLock = operationInfo.getPreviewLock();
    previewLock.lock();
    try {
        File previewFile = operationInfo.getPreviewFile();
        if (previewFile != null) {
            try {
                Reader reader = com.google.common.io.Files.newReader(previewFile, Charsets.UTF_8);
                try {
                    return GSON.fromJson(reader, new TypeToken<List<QueryResult>>() {
                    }.getType());
                } finally {
                    Closeables.closeQuietly(reader);
                }
            } catch (FileNotFoundException e) {
                LOG.error("Could not retrieve preview result file {}", previewFile, e);
                throw new ExploreException(e);
            }
        }
        try {
            // Create preview results for query
            previewFile = new File(previewsDir, handle.getHandle());
            try (FileWriter fileWriter = new FileWriter(previewFile)) {
                List<QueryResult> results = fetchNextResults(handle, PREVIEW_COUNT);
                GSON.toJson(results, fileWriter);
                operationInfo.setPreviewFile(previewFile);
                return results;
            }
        } catch (IOException e) {
            LOG.error("Could not write preview results into file", e);
            throw new ExploreException(e);
        }
    } finally {
        previewLock.unlock();
    }
}
Also used : HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) QueryResult(io.cdap.cdap.proto.QueryResult) TypeToken(com.google.common.reflect.TypeToken) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) IOException(java.io.IOException) File(java.io.File) Lock(java.util.concurrent.locks.Lock) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 23 with ExploreException

use of io.cdap.cdap.explore.service.ExploreException in project cdap by caskdata.

the class BaseHiveExploreService method getInfo.

@Override
public MetaDataInfo getInfo(MetaDataInfo.InfoType infoType) throws ExploreException, SQLException {
    startAndWait();
    try {
        MetaDataInfo ret = infoType.getDefaultValue();
        if (ret != null) {
            return ret;
        }
        SessionHandle sessionHandle = null;
        Map<String, String> sessionConf = startSession();
        try {
            sessionHandle = openHiveSession(sessionConf);
            // Convert to GetInfoType
            GetInfoType hiveInfoType = null;
            for (GetInfoType t : GetInfoType.values()) {
                if (t.name().equals("CLI_" + infoType.name())) {
                    hiveInfoType = t;
                    break;
                }
            }
            if (hiveInfoType == null) {
                // Should not come here, unless there is a mismatch between Explore and Hive info types.
                LOG.warn("Could not find Hive info type %s", infoType);
                return null;
            }
            GetInfoValue val = cliService.getInfo(sessionHandle, hiveInfoType);
            LOG.trace("Retrieving info: {}, got value {}", infoType, val);
            return new MetaDataInfo(val.getStringValue(), val.getShortValue(), val.getIntValue(), val.getLongValue());
        } finally {
            closeInternal(getQueryHandle(sessionConf), new ReadOnlyOperationInfo(sessionHandle, null, sessionConf, "", ""));
        }
    } catch (HiveSQLException e) {
        throw getSqlException(e);
    } catch (Throwable e) {
        throw new ExploreException(e);
    }
}
Also used : GetInfoType(org.apache.hive.service.cli.GetInfoType) GetInfoValue(org.apache.hive.service.cli.GetInfoValue) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) MetaDataInfo(io.cdap.cdap.explore.service.MetaDataInfo) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 24 with ExploreException

use of io.cdap.cdap.explore.service.ExploreException 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(io.cdap.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 25 with ExploreException

use of io.cdap.cdap.explore.service.ExploreException in project cdap by caskdata.

the class BaseHiveExploreService method fetchStatus.

protected QueryStatus fetchStatus(OperationInfo operationInfo) throws ExploreException, HandleNotFoundException, HiveSQLException {
    QueryStatus queryStatus;
    try {
        queryStatus = doFetchStatus(operationInfo.getOperationHandle());
        if (QueryStatus.OpStatus.ERROR.equals(queryStatus.getStatus()) && queryStatus.getErrorMessage() == null) {
            queryStatus = new QueryStatus("Operation failed. See the log for more details.", null);
        }
    } catch (HiveSQLException e) {
        // it means that query execution failed, but we can successfully retrieve the status.
        if (e.getSQLState() != null) {
            queryStatus = new QueryStatus(e.getMessage(), e.getSQLState());
        } else {
            // this is an internal error - we are not able to retrieve the status
            throw new ExploreException(e.getMessage(), e);
        }
    }
    operationInfo.setStatus(queryStatus);
    return queryStatus;
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) QueryStatus(io.cdap.cdap.proto.QueryStatus) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Aggregations

ExploreException (io.cdap.cdap.explore.service.ExploreException)43 QueryHandle (io.cdap.cdap.proto.QueryHandle)16 SQLException (java.sql.SQLException)14 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)14 IOException (java.io.IOException)12 OperationHandle (org.apache.hive.service.cli.OperationHandle)12 SessionHandle (org.apache.hive.service.cli.SessionHandle)12 HttpResponse (io.cdap.common.http.HttpResponse)11 Path (javax.ws.rs.Path)7 HandleNotFoundException (io.cdap.cdap.explore.service.HandleNotFoundException)6 UnsupportedTypeException (io.cdap.cdap.api.data.schema.UnsupportedTypeException)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)4 BadRequestException (io.cdap.cdap.common.BadRequestException)4 TableNotFoundException (io.cdap.cdap.explore.service.TableNotFoundException)4 DatasetId (io.cdap.cdap.proto.id.DatasetId)4 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)4 POST (javax.ws.rs.POST)4 TException (org.apache.thrift.TException)4 PartitionKey (io.cdap.cdap.api.dataset.lib.PartitionKey)3