Search in sources :

Example 11 with ListTablesResponse

use of com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse in project aws-athena-query-federation by awslabs.

the class ElasticsearchMetadataHandler method doListTables.

/**
 * Used to get the list of indices contained in the specified domain.
 * @param allocator Tool for creating and managing Apache Arrow Blocks.
 * @param request Provides details on who made the request and which Athena catalog and database they are querying.
 * @return A ListTablesResponse which primarily contains a List<TableName> enumerating the tables in this
 * catalog, database tuple. It also contains the catalog name corresponding the Athena catalog that was queried.
 * @throws RuntimeException when the domain does not exist in the map, or the client is unable to retrieve the
 * indices from the Elasticsearch instance.
 */
@Override
public ListTablesResponse doListTables(BlockAllocator allocator, ListTablesRequest request) throws RuntimeException {
    logger.debug("doListTables: enter - " + request);
    List<TableName> indices = new ArrayList<>();
    try {
        String endpoint = getDomainEndpoint(request.getSchemaName());
        AwsRestHighLevelClient client = clientFactory.getOrCreateClient(endpoint);
        try {
            for (String index : client.getAliases()) {
                // Ignore all system indices starting with period `.` (e.g. .kibana, .tasks, etc...)
                if (index.startsWith(".")) {
                    logger.info("Ignoring system index: {}", index);
                    continue;
                }
                indices.add(new TableName(request.getSchemaName(), index));
            }
        } catch (IOException error) {
            throw new RuntimeException("Error retrieving indices: " + error.getMessage(), error);
        }
    } catch (RuntimeException error) {
        throw new RuntimeException("Error processing request to list indices: " + error.getMessage(), error);
    }
    return new ListTablesResponse(request.getCatalogName(), indices, null);
}
Also used : TableName(com.amazonaws.athena.connector.lambda.domain.TableName) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ListTablesResponse(com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse)

Example 12 with ListTablesResponse

use of com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse in project aws-athena-query-federation by awslabs.

the class ConnectorValidator method showTables.

private static Collection<TableName> showTables(TestConfig testConfig, String db) {
    ListTablesResponse tablesResponse = LambdaMetadataProvider.listTables(testConfig.getCatalogId(), db, testConfig.getMetadataFunction(), testConfig.getIdentity());
    final Collection<TableName> tables = tablesResponse.getTables();
    log.info("Found tables: " + tables.stream().map(t -> toQualifiedTableName(t)).collect(Collectors.toList()));
    requireNonNull(tables, "Returned collection of tables was null!");
    if (!testConfig.isAllowEmptyTables()) {
        checkState(!tables.isEmpty(), "No tables were returned!");
        List<String> notLower = tables.stream().filter(t -> !t.equals(new TableName(t.getSchemaName().toLowerCase(), t.getTableName().toLowerCase()))).limit(5).map(t -> toQualifiedTableName(t)).collect(Collectors.toList());
        checkState(notLower.isEmpty(), "All returned tables must be lowercase! Found these non-lowercase tables: " + notLower);
    } else {
        log.info("showTables is allowing empty table list, actual size is " + tables.size());
    }
    return tables;
}
Also used : GetSplitsResponse(com.amazonaws.athena.connector.lambda.metadata.GetSplitsResponse) Schema(org.apache.arrow.vector.types.pojo.Schema) Options(org.apache.commons.cli.Options) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) BlockAllocator(com.amazonaws.athena.connector.lambda.data.BlockAllocator) HelpFormatter(org.apache.commons.cli.HelpFormatter) DefaultParser(org.apache.commons.cli.DefaultParser) ArrayList(java.util.ArrayList) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Objects.requireNonNull(java.util.Objects.requireNonNull) CommandLine(org.apache.commons.cli.CommandLine) BlockAllocatorImpl(com.amazonaws.athena.connector.lambda.data.BlockAllocatorImpl) FederatedIdentity(com.amazonaws.athena.connector.lambda.security.FederatedIdentity) ListTablesResponse(com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse) ListSchemasResponse(com.amazonaws.athena.connector.lambda.metadata.ListSchemasResponse) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Collection(java.util.Collection) Split(com.amazonaws.athena.connector.lambda.domain.Split) ReadRecordsResponse(com.amazonaws.athena.connector.lambda.records.ReadRecordsResponse) Set(java.util.Set) BlockUtils.rowToString(com.amazonaws.athena.connector.lambda.data.BlockUtils.rowToString) Field(org.apache.arrow.vector.types.pojo.Field) Collectors(java.util.stream.Collectors) TableName(com.amazonaws.athena.connector.lambda.domain.TableName) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Constraints(com.amazonaws.athena.connector.lambda.domain.predicate.Constraints) GetTableLayoutResponse(com.amazonaws.athena.connector.lambda.metadata.GetTableLayoutResponse) List(java.util.List) ConstraintParser.parseConstraints(com.amazonaws.athena.connector.validation.ConstraintParser.parseConstraints) ParseException(org.apache.commons.cli.ParseException) Optional(java.util.Optional) Collections(java.util.Collections) GetTableResponse(com.amazonaws.athena.connector.lambda.metadata.GetTableResponse) TableName(com.amazonaws.athena.connector.lambda.domain.TableName) ListTablesResponse(com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse) BlockUtils.rowToString(com.amazonaws.athena.connector.lambda.data.BlockUtils.rowToString)

Example 13 with ListTablesResponse

use of com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse in project aws-athena-query-federation by awslabs.

the class ExampleMetadataHandler method doListTables.

/**
 * Returns a paginated list of TableNames.
 *
 * @param allocator Tool for creating and managing Apache Arrow Blocks.
 * @param request Provides details on who made the request and which Athena catalog and database they are querying.
 * @return A ListTablesResponse containing the list of available TableNames and a nextToken String for the start of
 * the next pagination (may be null if there are no more tables to paginate).
 * @implNote A complete (un-paginated) list of tables should be returned if the request's pageSize is set to
 * ListTablesRequest.UNLIMITED_PAGE_SIZE_VALUE.
 */
@Override
public ListTablesResponse doListTables(BlockAllocator allocator, ListTablesRequest request) {
    logCaller(request);
    String nextToken = null;
    int pageSize = request.getPageSize();
    // The following list is purposely unordered to demonstrate that the pagination logic does not consider the
    // order of the tables deterministic (i.e. pagination will work irrespective of the order that the tables are
    // returned from the source).
    List<TableName> tables = new ImmutableList.Builder<TableName>().add(new TableName("schema", "table4")).add(new TableName("schema", "table3")).add(new TableName("schema", "table5")).add(new TableName("schema", "table1")).add(new TableName("schema", "table2")).build();
    // Check if request has unlimited page size. If not, then list of tables will be paginated.
    if (pageSize != UNLIMITED_PAGE_SIZE_VALUE) {
        // Get the stating table for this page (if null, then this is the first page).
        String startToken = request.getNextToken();
        // Sort the list. Include all tables if the startToken is null, or only the tables whose names are equal to
        // or higher in value than startToken. Limit the number of tables in the list to the pageSize + 1 (the
        // nextToken).
        List<TableName> paginatedTables = tables.stream().sorted(Comparator.comparing(TableName::getTableName)).filter(table -> startToken == null || table.getTableName().compareTo(startToken) >= 0).limit(pageSize + 1).collect(Collectors.toList());
        if (paginatedTables.size() > pageSize) {
            // Paginated list contains full page of results + nextToken.
            // nextToken is the last element in the paginated list.
            // In an actual connector, the nextToken's value should be obfuscated.
            nextToken = paginatedTables.get(pageSize).getTableName();
            // nextToken is removed to include only the paginated results.
            tables = paginatedTables.subList(0, pageSize);
        } else {
            // Paginated list contains all remaining tables - end of the pagination.
            tables = paginatedTables;
        }
    }
    // that is exercised in our unit test suite.
    return new ListTablesResponse(request.getCatalogName(), tables.stream().filter(table -> request.getSchemaName() == null || request.getSchemaName().equals(table.getSchemaName())).collect(Collectors.toList()), nextToken);
}
Also used : TableName(com.amazonaws.athena.connector.lambda.domain.TableName) SchemaBuilder(com.amazonaws.athena.connector.lambda.data.SchemaBuilder) FieldBuilder(com.amazonaws.athena.connector.lambda.data.FieldBuilder) ListTablesResponse(com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse)

Example 14 with ListTablesResponse

use of com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse in project aws-athena-query-federation by awslabs.

the class ExampleMetadataHandlerTest method doListTables.

@Test
public void doListTables() {
    logger.info("doListTables - enter");
    // Test request with unlimited page size
    logger.info("doListTables - Test unlimited page size");
    ListTablesRequest req = new ListTablesRequest(IdentityUtil.fakeIdentity(), "queryId", "default", "schema", null, UNLIMITED_PAGE_SIZE_VALUE);
    ListTablesResponse expectedResponse = new ListTablesResponse("default", new ImmutableList.Builder<TableName>().add(new TableName("schema", "table1")).add(new TableName("schema", "table2")).add(new TableName("schema", "table3")).add(new TableName("schema", "table4")).add(new TableName("schema", "table5")).build(), null);
    ObjectMapperUtil.assertSerialization(req);
    ListTablesResponse res = metadataHandler.doListTables(allocator, req);
    ObjectMapperUtil.assertSerialization(res);
    logger.info("doListTables - {}", res);
    assertEquals("Expecting a different response", expectedResponse, res);
    // Test first paginated request with pageSize: 3, nextToken: null
    logger.info("doListTables - Test first pagination request");
    req = new ListTablesRequest(IdentityUtil.fakeIdentity(), "queryId", "default", "schema", null, 3);
    expectedResponse = new ListTablesResponse("default", new ImmutableList.Builder<TableName>().add(new TableName("schema", "table1")).add(new TableName("schema", "table2")).add(new TableName("schema", "table3")).build(), "table4");
    ObjectMapperUtil.assertSerialization(req);
    res = metadataHandler.doListTables(allocator, req);
    ObjectMapperUtil.assertSerialization(res);
    logger.info("doListTables - {}", res);
    assertEquals("Expecting a different response", expectedResponse, res);
    // Test second paginated request with pageSize: 3, nextToken: res.getNextToken()
    logger.info("doListTables - Test second pagination request");
    req = new ListTablesRequest(IdentityUtil.fakeIdentity(), "queryId", "default", "schema", res.getNextToken(), 3);
    expectedResponse = new ListTablesResponse("default", new ImmutableList.Builder<TableName>().add(new TableName("schema", "table4")).add(new TableName("schema", "table5")).build(), null);
    ObjectMapperUtil.assertSerialization(req);
    res = metadataHandler.doListTables(allocator, req);
    ObjectMapperUtil.assertSerialization(res);
    logger.info("doListTables - {}", res);
    assertEquals("Expecting a different response", expectedResponse, res);
    logger.info("doListTables - exit");
}
Also used : TableName(com.amazonaws.athena.connector.lambda.domain.TableName) ImmutableList(com.google.common.collect.ImmutableList) SchemaBuilder(com.amazonaws.athena.connector.lambda.data.SchemaBuilder) ListTablesRequest(com.amazonaws.athena.connector.lambda.metadata.ListTablesRequest) ListTablesResponse(com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse) Test(org.junit.Test)

Example 15 with ListTablesResponse

use of com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse in project aws-athena-query-federation by awslabs.

the class HbaseMetadataHandler method doListTables.

/**
 * List tables in the requested schema in your HBase instance treating the requested schema as an HBase
 * namespace.
 *
 * @see GlueMetadataHandler
 */
@Override
public ListTablesResponse doListTables(BlockAllocator blockAllocator, ListTablesRequest request) throws IOException {
    List<com.amazonaws.athena.connector.lambda.domain.TableName> tableNames = new ArrayList<>();
    TableName[] tables = getOrCreateConn(request).listTableNamesByNamespace(request.getSchemaName());
    for (int i = 0; i < tables.length; i++) {
        TableName tableName = tables[i];
        tableNames.add(new com.amazonaws.athena.connector.lambda.domain.TableName(request.getSchemaName(), tableName.getNameAsString().replace(request.getSchemaName() + ":", "")));
    }
    return new ListTablesResponse(request.getCatalogName(), tableNames, null);
}
Also used : ArrayList(java.util.ArrayList) TableName(org.apache.hadoop.hbase.TableName) ListTablesResponse(com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse)

Aggregations

ListTablesResponse (com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse)31 TableName (com.amazonaws.athena.connector.lambda.domain.TableName)24 ListTablesRequest (com.amazonaws.athena.connector.lambda.metadata.ListTablesRequest)18 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)13 Table (com.amazonaws.services.glue.model.Table)5 SchemaBuilder (com.amazonaws.athena.connector.lambda.data.SchemaBuilder)4 GetSplitsResponse (com.amazonaws.athena.connector.lambda.metadata.GetSplitsResponse)4 GetTableLayoutResponse (com.amazonaws.athena.connector.lambda.metadata.GetTableLayoutResponse)4 GetTableResponse (com.amazonaws.athena.connector.lambda.metadata.GetTableResponse)4 ListSchemasResponse (com.amazonaws.athena.connector.lambda.metadata.ListSchemasResponse)4 GetTablesResult (com.amazonaws.services.glue.model.GetTablesResult)4 ImmutableList (com.google.common.collect.ImmutableList)4 BlockAllocatorImpl (com.amazonaws.athena.connector.lambda.data.BlockAllocatorImpl)3 Before (org.junit.Before)3 BlockAllocator (com.amazonaws.athena.connector.lambda.data.BlockAllocator)2 Split (com.amazonaws.athena.connector.lambda.domain.Split)2 Constraints (com.amazonaws.athena.connector.lambda.domain.predicate.Constraints)2 GetSplitsRequest (com.amazonaws.athena.connector.lambda.metadata.GetSplitsRequest)2 GetTableLayoutRequest (com.amazonaws.athena.connector.lambda.metadata.GetTableLayoutRequest)2