use of co.cask.cdap.explore.service.ExploreException in project cdap by caskdata.
the class BaseHiveExploreService method getFunctions.
@Override
public QueryHandle getFunctions(String catalog, String schemaPattern, String functionNamePattern) 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.getFunctions(sessionHandle, catalog, database, functionNamePattern);
QueryHandle handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, "", database);
LOG.trace("Retrieving functions: catalog {}, schema {}, function {}", catalog, database, functionNamePattern);
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);
}
}
use of co.cask.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 co.cask.cdap.explore.service.ExploreException in project cdap by caskdata.
the class BaseHiveExploreService method getTypeInfo.
@Override
public QueryHandle getTypeInfo() throws ExploreException, SQLException {
startAndWait();
try {
SessionHandle sessionHandle = null;
OperationHandle operationHandle = null;
Map<String, String> sessionConf = startSession();
try {
sessionHandle = openHiveSession(sessionConf);
operationHandle = cliService.getTypeInfo(sessionHandle);
QueryHandle handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, "", "");
LOG.trace("Retrieving type info");
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);
}
}
use of co.cask.cdap.explore.service.ExploreException in project cdap by caskdata.
the class DistributedStorageProviderNamespaceAdmin method create.
@Override
public void create(NamespaceMeta namespaceMeta) throws IOException, ExploreException, SQLException {
// create filesystem directory
super.create(namespaceMeta);
// skip namespace creation in HBase for default namespace
if (NamespaceId.DEFAULT.equals(namespaceMeta.getNamespaceId())) {
return;
}
// create HBase namespace and set group C(reate) permission if a group is configured
String hbaseNamespace = tableUtil.getHBaseNamespace(namespaceMeta);
if (Strings.isNullOrEmpty(namespaceMeta.getConfig().getHbaseNamespace())) {
try (HBaseDDLExecutor executor = hBaseDDLExecutorFactory.get()) {
boolean created = executor.createNamespaceIfNotExists(hbaseNamespace);
if (namespaceMeta.getConfig().getGroupName() != null) {
try {
executor.grantPermissions(hbaseNamespace, null, ImmutableMap.of("@" + namespaceMeta.getConfig().getGroupName(), "C"));
} catch (IOException | RuntimeException e) {
// don't leave a partial state behind, as this fails the create(), the namespace should be removed
if (created) {
try {
executor.deleteNamespaceIfExists(hbaseNamespace);
} catch (Throwable t) {
e.addSuppressed(t);
}
}
throw e;
}
}
} catch (Throwable t) {
try {
// if we failed to create a namespace in hbase then do clean up for above creations
super.delete(namespaceMeta.getNamespaceId());
} catch (Exception e) {
t.addSuppressed(e);
}
throw t;
}
}
try (HBaseAdmin admin = new HBaseAdmin(hConf)) {
if (!tableUtil.hasNamespace(admin, hbaseNamespace)) {
throw new IOException(String.format("HBase namespace '%s' specified for new namespace '%s' does not" + " exist. Please specify an existing HBase namespace.", hbaseNamespace, namespaceMeta.getName()));
}
}
}
use of co.cask.cdap.explore.service.ExploreException in project cdap by caskdata.
the class ExploreHttpClient method getSchemas.
@Override
public QueryHandle getSchemas(@Nullable String catalog, @Nullable String schemaPattern) throws ExploreException, SQLException {
String body = GSON.toJson(new SchemasArgs(catalog, schemaPattern));
String resource = String.format("namespaces/%s/data/explore/jdbc/schemas", schemaPattern);
HttpResponse response = doPost(resource, body, null);
if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
return QueryHandle.fromId(parseResponseAsMap(response, "handle"));
}
throw new ExploreException("Cannot get the schemas. Reason: " + response);
}
Aggregations