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);
}
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();
}
}
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);
}
}
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);
}
}
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;
}
Aggregations