Search in sources :

Example 1 with QueryRunner

use of com.airbnb.airpal.presto.QueryRunner in project airpal by airbnb.

the class ColumnCache method queryPartitions.

private List<HivePartition> queryPartitions(String query) {
    final ImmutableList.Builder<HivePartition> cache = ImmutableList.builder();
    final Map<Column, List<Object>> objects = 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 && results.getColumns() != null) {
                    final List<Column> columns = results.getColumns();
                    for (Column column : columns) {
                        objects.put(column, Lists.newArrayList());
                    }
                    for (List<Object> row : results.getData()) {
                        for (int i = 0; i < row.size(); i++) {
                            Column column = columns.get(i);
                            objects.get(column).add(row.get(i));
                        }
                    }
                }
                return null;
            }
        });
    } catch (QueryClient.QueryTimeOutException e) {
        log.error("Caught timeout loading columns", e);
    }
    for (Map.Entry<Column, List<Object>> entry : objects.entrySet()) {
        cache.add(HivePartition.fromColumn(entry.getKey(), entry.getValue()));
    }
    return cache.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) StatementClient(com.facebook.presto.client.StatementClient) QueryRunner(com.airbnb.airpal.presto.QueryRunner) QueryResults(com.facebook.presto.client.QueryResults) QueryClient(com.airbnb.airpal.core.execution.QueryClient) Column(com.facebook.presto.client.Column) HiveColumn(com.airbnb.airpal.presto.hive.HiveColumn) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map) Nullable(javax.annotation.Nullable) HivePartition(com.airbnb.airpal.presto.hive.HivePartition)

Example 2 with QueryRunner

use of com.airbnb.airpal.presto.QueryRunner in project airpal by airbnb.

the class ColumnCache method queryColumns.

private List<HiveColumn> queryColumns(String query) {
    final ImmutableList.Builder<HiveColumn> 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) {
                    for (List<Object> row : results.getData()) {
                        Column column = new Column((String) row.get(0), (String) row.get(1), new ClientTypeSignature(TypeSignature.parseTypeSignature((String) row.get(1))));
                        boolean isNullable = (Boolean) row.get(2);
                        boolean isPartition = (Boolean) row.get(3);
                        cache.add(HiveColumn.fromColumn(column, isNullable, isPartition));
                    }
                }
                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) QueryRunner(com.airbnb.airpal.presto.QueryRunner) QueryResults(com.facebook.presto.client.QueryResults) QueryClient(com.airbnb.airpal.core.execution.QueryClient) Column(com.facebook.presto.client.Column) HiveColumn(com.airbnb.airpal.presto.hive.HiveColumn) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) HiveColumn(com.airbnb.airpal.presto.hive.HiveColumn) ClientTypeSignature(com.facebook.presto.client.ClientTypeSignature) Nullable(javax.annotation.Nullable)

Example 3 with QueryRunner

use of com.airbnb.airpal.presto.QueryRunner 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 4 with QueryRunner

use of com.airbnb.airpal.presto.QueryRunner 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

QueryClient (com.airbnb.airpal.core.execution.QueryClient)4 QueryRunner (com.airbnb.airpal.presto.QueryRunner)4 QueryResults (com.facebook.presto.client.QueryResults)4 StatementClient (com.facebook.presto.client.StatementClient)4 List (java.util.List)4 Nullable (javax.annotation.Nullable)4 ImmutableList (com.google.common.collect.ImmutableList)3 HiveColumn (com.airbnb.airpal.presto.hive.HiveColumn)2 Column (com.facebook.presto.client.Column)2 HivePartition (com.airbnb.airpal.presto.hive.HivePartition)1 ClientTypeSignature (com.facebook.presto.client.ClientTypeSignature)1 Map (java.util.Map)1