use of com.facebook.presto.client.QueryResults in project airpal by airbnb.
the class SchemaCache method queryMetadata.
private Map<String, List<String>> queryMetadata(String query) {
final Map<String, List<String>> cache = Maps.newHashMap();
QueryRunner queryRunner = queryRunnerFactory.create();
QueryClient queryClient = new QueryClient(queryRunner, io.dropwizard.util.Duration.seconds(60), query);
try {
queryClient.executeWith(new Function<StatementClient, Void>() {
@Nullable
@Override
public Void apply(StatementClient client) {
QueryResults results = client.current();
if (results.getData() != null) {
for (List<Object> row : results.getData()) {
String schema = (String) row.get(1);
String table = (String) row.get(2);
if (EXCLUDED_SCHEMAS.contains(schema)) {
continue;
}
List<String> tables = cache.get(schema);
if (tables == null) {
tables = Lists.newArrayList();
cache.put(schema, tables);
}
tables.add(table);
}
}
return null;
}
});
} catch (QueryClient.QueryTimeOutException e) {
log.error("Caught timeout loading columns", e);
}
return ImmutableMap.copyOf(cache);
}
use of com.facebook.presto.client.QueryResults in project presto by prestodb.
the class Query method renderQueryOutput.
private void renderQueryOutput(PrintStream out, OutputFormat outputFormat, boolean interactive) {
StatusPrinter statusPrinter = null;
@SuppressWarnings("resource") PrintStream errorChannel = interactive ? out : System.err;
if (interactive) {
statusPrinter = new StatusPrinter(client, out);
statusPrinter.printInitialStatusUpdates();
} else {
waitForData();
}
if ((!client.isFailed()) && (!client.isGone()) && (!client.isClosed())) {
QueryResults results = client.isValid() ? client.current() : client.finalResults();
if (results.getUpdateType() != null) {
renderUpdate(errorChannel, results);
} else if (results.getColumns() == null) {
errorChannel.printf("Query %s has no columns\n", results.getId());
return;
} else {
renderResults(out, outputFormat, interactive, results.getColumns());
}
}
if (statusPrinter != null) {
statusPrinter.printFinalInfo();
}
if (client.isClosed()) {
errorChannel.println("Query aborted by user");
} else if (client.isGone()) {
errorChannel.println("Query is gone (server restarted?)");
} else if (client.isFailed()) {
renderFailure(errorChannel);
}
}
use of com.facebook.presto.client.QueryResults in project presto by prestodb.
the class TableNameCompleter method queryMetadata.
private List<String> queryMetadata(String query) {
ImmutableList.Builder<String> cache = ImmutableList.builder();
try (StatementClient client = queryRunner.startInternalQuery(query)) {
while (client.isValid() && !Thread.currentThread().isInterrupted()) {
QueryResults results = client.current();
if (results.getData() != null) {
for (List<Object> row : results.getData()) {
cache.add((String) row.get(0));
}
}
client.advance();
}
}
return cache.build();
}
use of com.facebook.presto.client.QueryResults in project presto by prestodb.
the class AbstractTestingPrestoClient method execute.
public T execute(Session session, @Language("SQL") String sql) {
ResultsSession<T> resultsSession = getResultSession(session);
ClientSession clientSession = toClientSession(session, prestoServer.getBaseUrl(), true, new Duration(2, TimeUnit.MINUTES));
try (StatementClient client = new StatementClient(httpClient, QUERY_RESULTS_CODEC, clientSession, sql)) {
while (client.isValid()) {
QueryResults results = client.current();
resultsSession.addResults(results);
client.advance();
}
if (!client.isFailed()) {
QueryResults results = client.finalResults();
if (results.getUpdateType() != null) {
resultsSession.setUpdateType(results.getUpdateType());
}
if (results.getUpdateCount() != null) {
resultsSession.setUpdateCount(results.getUpdateCount());
}
return resultsSession.build(client.getSetSessionProperties(), client.getResetSessionProperties());
}
QueryError error = client.finalResults().getError();
verify(error != null, "no error");
if (error.getFailureInfo() != null) {
throw error.getFailureInfo().toException();
}
throw new RuntimeException("Query failed: " + error.getMessage());
// dump query info to console for debugging (NOTE: not pretty printed)
// JsonCodec<QueryInfo> queryInfoJsonCodec = createCodecFactory().prettyPrint().jsonCodec(QueryInfo.class);
// log.info("\n" + queryInfoJsonCodec.toJson(queryInfo));
}
}
use of com.facebook.presto.client.QueryResults in project presto by prestodb.
the class PrestoResultSet method getColumns.
private static List<Column> getColumns(StatementClient client, Consumer<QueryStats> progressCallback) throws SQLException {
while (client.isValid()) {
QueryResults results = client.current();
progressCallback.accept(QueryStats.create(results.getId(), results.getStats()));
List<Column> columns = results.getColumns();
if (columns != null) {
return columns;
}
client.advance();
}
QueryResults results = client.finalResults();
if (!client.isFailed()) {
throw new SQLException(format("Query has no columns (#%s)", results.getId()));
}
throw resultsException(results);
}
Aggregations