Search in sources :

Example 1 with QueryClient

use of io.prestosql.queryeditorui.execution.QueryClient in project hetu-core by openlookeng.

the class ColumnService method queryColumns.

private List<Column> queryColumns(String fqnTableName, String user) {
    String statement = format("SHOW COLUMNS FROM %s", fqnTableName);
    QueryRunner queryRunner = queryRunnerFactory.create(QueryEditorUIModule.UI_QUERY_SOURCE, user);
    QueryClient queryClient = new QueryClient(queryRunner, Duration.standardSeconds(60), statement);
    final ImmutableList.Builder<Column> cache = ImmutableList.builder();
    try {
        queryClient.executeWith(new Function<StatementClient, Void>() {

            @Nullable
            @Override
            public Void apply(StatementClient client) {
                QueryData results = client.currentData();
                if (results.getData() != null) {
                    for (List<Object> row : results.getData()) {
                        TypeSignature typeSignature = TypeSignature.parseTypeSignature((String) row.get(1));
                        Column column = new Column((String) row.get(0), (String) row.get(1), Query.toClientTypeSignature(typeSignature));
                        cache.add(column);
                    }
                }
                return null;
            }
        });
    } catch (QueryClient.QueryTimeOutException e) {
        log.error("Caught timeout loading columns", e);
    }
    return cache.build();
}
Also used : QueryData(io.prestosql.client.QueryData) ImmutableList(com.google.common.collect.ImmutableList) StatementClient(io.prestosql.client.StatementClient) QueryRunner(io.prestosql.queryeditorui.execution.QueryRunner) QueryClient(io.prestosql.queryeditorui.execution.QueryClient) TypeSignature(io.prestosql.spi.type.TypeSignature) Column(io.prestosql.client.Column) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Nullable(javax.annotation.Nullable)

Example 2 with QueryClient

use of io.prestosql.queryeditorui.execution.QueryClient in project hetu-core by openlookeng.

the class PreviewTableService method queryRows.

private List<List<Object>> queryRows(String fqnTableName, String user) {
    String statement = format("SELECT * FROM %s LIMIT %d", fqnTableName, PREVIEW_LIMIT);
    QueryRunner queryRunner = queryRunnerFactory.create(QueryEditorUIModule.UI_QUERY_SOURCE, user);
    QueryClient queryClient = new QueryClient(queryRunner, Duration.standardSeconds(60), statement);
    final ImmutableList.Builder<List<Object>> cache = ImmutableList.builder();
    try {
        queryClient.executeWith(new Function<StatementClient, Void>() {

            @Nullable
            @Override
            public Void apply(StatementClient client) {
                QueryData results = client.currentData();
                if (results.getData() != null) {
                    List<List<Object>> resultsData = ImmutableList.copyOf(results.getData());
                    for (List<Object> row : resultsData) {
                        final String[] values = new String[row.size()];
                        for (int i = 0; i < values.length; i++) {
                            // Display byte array as Hexadecimal in order to keep consistent with OpenLooKeng client
                            if (row.get(i) instanceof byte[]) {
                                byte[] bytes = (byte[]) row.get(i);
                                StringBuilder sb = new StringBuilder();
                                for (byte b : bytes) {
                                    String hex = Integer.toHexString(b & 0xFF);
                                    if (hex.length() < 2) {
                                        sb.append(0);
                                    }
                                    sb.append(hex);
                                }
                                values[i] = sb.toString();
                            } else {
                                final Object value = row.get(i);
                                values[i] = (value == null) ? "" : value.toString();
                            }
                        }
                        cache.add(Arrays.asList(values));
                    }
                }
                return null;
            }
        });
    } catch (QueryClient.QueryTimeOutException e) {
        log.error("Caught timeout loading columns", e);
    }
    return cache.build();
}
Also used : QueryData(io.prestosql.client.QueryData) ImmutableList(com.google.common.collect.ImmutableList) StatementClient(io.prestosql.client.StatementClient) QueryRunner(io.prestosql.queryeditorui.execution.QueryRunner) QueryClient(io.prestosql.queryeditorui.execution.QueryClient) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Nullable(javax.annotation.Nullable)

Example 3 with QueryClient

use of io.prestosql.queryeditorui.execution.QueryClient in project hetu-core by openlookeng.

the class SchemaService method queryStatement.

private Set<String> queryStatement(QueryRunner queryRunner, String statement) {
    QueryClient queryClient = new QueryClient(queryRunner, Duration.standardSeconds(120), statement);
    final Set<String> resultSet = new HashSet();
    try {
        queryClient.executeWith(new Function<StatementClient, Void>() {

            @Nullable
            @Override
            public Void apply(StatementClient client) {
                QueryData results = client.currentData();
                if (results.getData() != null) {
                    for (List<Object> row : results.getData()) {
                        resultSet.add((String) row.get(0));
                    }
                }
                return null;
            }
        });
    } catch (QueryClient.QueryTimeOutException e) {
        LOG.error("Caught timeout loading data", e);
    }
    return resultSet;
}
Also used : QueryData(io.prestosql.client.QueryData) StatementClient(io.prestosql.client.StatementClient) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Nullable(javax.annotation.Nullable) QueryClient(io.prestosql.queryeditorui.execution.QueryClient) HashSet(java.util.HashSet)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)3 QueryData (io.prestosql.client.QueryData)3 StatementClient (io.prestosql.client.StatementClient)3 QueryClient (io.prestosql.queryeditorui.execution.QueryClient)3 List (java.util.List)3 Nullable (javax.annotation.Nullable)3 QueryRunner (io.prestosql.queryeditorui.execution.QueryRunner)2 Column (io.prestosql.client.Column)1 TypeSignature (io.prestosql.spi.type.TypeSignature)1 HashSet (java.util.HashSet)1