Search in sources :

Example 21 with QueryResults

use of com.facebook.presto.client.QueryResults in project urban-eureka by errir503.

the class TestServer method testQuery.

@Test
public void testQuery() {
    // start query
    Request request = preparePost().setUri(uriFor("/v1/statement")).setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)).setHeader(PRESTO_USER, "user").setHeader(PRESTO_SOURCE, "source").setHeader(PRESTO_CATALOG, "catalog").setHeader(PRESTO_SCHEMA, "schema").setHeader(PRESTO_CLIENT_INFO, "{\"clientVersion\":\"testVersion\"}").addHeader(PRESTO_SESSION, QUERY_MAX_MEMORY + "=1GB").addHeader(PRESTO_SESSION, JOIN_DISTRIBUTION_TYPE + "=partitioned," + HASH_PARTITION_COUNT + " = 43").addHeader(PRESTO_PREPARED_STATEMENT, "foo=select * from bar").addHeader(PRESTO_SESSION_FUNCTION, format("%s=%s", urlEncode(SERIALIZED_SQL_FUNCTION_ID_ADD), urlEncode(SERIALIZED_SQL_FUNCTION_ADD))).build();
    QueryResults queryResults = client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC));
    ImmutableList.Builder<List<Object>> data = ImmutableList.builder();
    while (queryResults.getNextUri() != null) {
        queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_CODEC));
        if (queryResults.getData() != null) {
            data.addAll(queryResults.getData());
        }
    }
    assertNull(queryResults.getError());
    // get the query info
    BasicQueryInfo queryInfo = server.getQueryManager().getQueryInfo(new QueryId(queryResults.getId()));
    // verify session properties
    assertEquals(queryInfo.getSession().getSystemProperties(), ImmutableMap.builder().put(QUERY_MAX_MEMORY, "1GB").put(JOIN_DISTRIBUTION_TYPE, "partitioned").put(HASH_PARTITION_COUNT, "43").build());
    // verify client info in session
    assertEquals(queryInfo.getSession().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    // verify prepared statements
    assertEquals(queryInfo.getSession().getPreparedStatements(), ImmutableMap.builder().put("foo", "select * from bar").build());
    // verify session functions
    assertEquals(queryInfo.getSession().getSessionFunctions(), ImmutableMap.of(SQL_FUNCTION_ID_ADD, SQL_FUNCTION_ADD));
    // only the system catalog exists by default
    List<List<Object>> rows = data.build();
    assertEquals(rows, ImmutableList.of(ImmutableList.of("system")));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) QueryId(com.facebook.presto.spi.QueryId) Request(com.facebook.airlift.http.client.Request) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) QueryResults(com.facebook.presto.client.QueryResults) Test(org.testng.annotations.Test)

Example 22 with QueryResults

use of com.facebook.presto.client.QueryResults in project urban-eureka by errir503.

the class TestServer method testTransactionSupport.

@Test
public void testTransactionSupport() {
    Request request = preparePost().setUri(uriFor("/v1/statement")).setBodyGenerator(createStaticBodyGenerator("start transaction", UTF_8)).setHeader(PRESTO_USER, "user").setHeader(PRESTO_SOURCE, "source").setHeader(PRESTO_TRANSACTION_ID, "none").build();
    JsonResponse<QueryResults> queryResults = client.execute(request, createFullJsonResponseHandler(QUERY_RESULTS_CODEC));
    ImmutableList.Builder<List<Object>> data = ImmutableList.builder();
    while (true) {
        if (queryResults.getValue().getData() != null) {
            data.addAll(queryResults.getValue().getData());
        }
        if (queryResults.getValue().getNextUri() == null) {
            break;
        }
        queryResults = client.execute(prepareGet().setUri(queryResults.getValue().getNextUri()).build(), createFullJsonResponseHandler(QUERY_RESULTS_CODEC));
    }
    assertNull(queryResults.getValue().getError());
    assertNotNull(queryResults.getHeader(PRESTO_STARTED_TRANSACTION_ID));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Request(com.facebook.airlift.http.client.Request) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) QueryResults(com.facebook.presto.client.QueryResults) Test(org.testng.annotations.Test)

Example 23 with QueryResults

use of com.facebook.presto.client.QueryResults in project urban-eureka by errir503.

the class TestServer method testNoTransactionSupport.

@Test
public void testNoTransactionSupport() {
    Request request = preparePost().setUri(uriFor("/v1/statement")).setBodyGenerator(createStaticBodyGenerator("start transaction", UTF_8)).setHeader(PRESTO_USER, "user").setHeader(PRESTO_SOURCE, "source").build();
    QueryResults queryResults = client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC));
    while (queryResults.getNextUri() != null) {
        queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_CODEC));
    }
    assertNotNull(queryResults.getError());
    assertEquals(queryResults.getError().getErrorCode(), INCOMPATIBLE_CLIENT.toErrorCode().getCode());
}
Also used : Request(com.facebook.airlift.http.client.Request) QueryResults(com.facebook.presto.client.QueryResults) Test(org.testng.annotations.Test)

Example 24 with QueryResults

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);
}
Also used : StatementClient(com.facebook.presto.client.StatementClient) List(java.util.List) QueryRunner(com.airbnb.airpal.presto.QueryRunner) Nullable(javax.annotation.Nullable) QueryResults(com.facebook.presto.client.QueryResults) QueryClient(com.airbnb.airpal.core.execution.QueryClient)

Example 25 with QueryResults

use of com.facebook.presto.client.QueryResults in project airpal by airbnb.

the class PreviewTableCache method queryRows.

private List<List<Object>> queryRows(String query) {
    final ImmutableList.Builder<List<Object>> cache = ImmutableList.builder();
    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) {
                    cache.addAll(results.getData());
                }
                return null;
            }
        });
    } catch (QueryClient.QueryTimeOutException e) {
        log.error("Caught timeout loading columns", e);
    }
    return cache.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) StatementClient(com.facebook.presto.client.StatementClient) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) QueryRunner(com.airbnb.airpal.presto.QueryRunner) Nullable(javax.annotation.Nullable) QueryResults(com.facebook.presto.client.QueryResults) QueryClient(com.airbnb.airpal.core.execution.QueryClient)

Aggregations

QueryResults (com.facebook.presto.client.QueryResults)38 URI (java.net.URI)15 Request (com.facebook.airlift.http.client.Request)14 List (java.util.List)13 ImmutableList (com.google.common.collect.ImmutableList)10 Test (org.testng.annotations.Test)10 StatementClient (com.facebook.presto.client.StatementClient)5 Nullable (javax.annotation.Nullable)5 QueryClient (com.airbnb.airpal.core.execution.QueryClient)4 QueryRunner (com.airbnb.airpal.presto.QueryRunner)4 Column (com.facebook.presto.client.Column)4 HiveColumn (com.airbnb.airpal.presto.hive.HiveColumn)2 HttpClient (com.facebook.airlift.http.client.HttpClient)2 HttpUriBuilder.uriBuilderFrom (com.facebook.airlift.http.client.HttpUriBuilder.uriBuilderFrom)2 JsonResponseHandler.createJsonResponseHandler (com.facebook.airlift.http.client.JsonResponseHandler.createJsonResponseHandler)2 Builder.prepareGet (com.facebook.airlift.http.client.Request.Builder.prepareGet)2 Builder.preparePost (com.facebook.airlift.http.client.Request.Builder.preparePost)2 StaticBodyGenerator.createStaticBodyGenerator (com.facebook.airlift.http.client.StaticBodyGenerator.createStaticBodyGenerator)2 UnexpectedResponseException (com.facebook.airlift.http.client.UnexpectedResponseException)2 JettyHttpClient (com.facebook.airlift.http.client.jetty.JettyHttpClient)2