Search in sources :

Example 26 with ListTablesResponse

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

the class CloudwatchMetadataHandler method doListTables.

/**
 * List LogStreams within the requested schema (aka LogGroup) in your Cloudwatch account treating each as a 'table'.
 *
 * @see MetadataHandler
 */
@Override
public ListTablesResponse doListTables(BlockAllocator blockAllocator, ListTablesRequest listTablesRequest) throws TimeoutException {
    String nextToken = null;
    String logGroupName = tableResolver.validateSchema(listTablesRequest.getSchemaName());
    DescribeLogStreamsRequest request = new DescribeLogStreamsRequest(logGroupName);
    DescribeLogStreamsResult result;
    List<TableName> tables = new ArrayList<>();
    if (listTablesRequest.getPageSize() == UNLIMITED_PAGE_SIZE_VALUE) {
        do {
            if (tables.size() > MAX_RESULTS) {
                throw new RuntimeException("Too many log streams, exceeded max metadata results for table count.");
            }
            result = invoker.invoke(() -> awsLogs.describeLogStreams(request));
            result.getLogStreams().forEach(next -> tables.add(toTableName(listTablesRequest, next)));
            request.setNextToken(result.getNextToken());
            logger.info("doListTables: Listing log streams  with token {} and size {}", result.getNextToken(), tables.size());
        } while (result.getNextToken() != null);
    } else {
        request.setNextToken(listTablesRequest.getNextToken());
        request.setLimit(listTablesRequest.getPageSize());
        result = invoker.invoke(() -> awsLogs.describeLogStreams(request));
        result.getLogStreams().forEach(next -> tables.add(toTableName(listTablesRequest, next)));
        nextToken = result.getNextToken();
        logger.info("doListTables: Listing log streams with token {} and size {}", result.getNextToken(), tables.size());
    }
    // We add a special table that represents all log streams. This is helpful depending on how
    // you have your logs organized.
    tables.add(new TableName(listTablesRequest.getSchemaName(), ALL_LOG_STREAMS_TABLE));
    return new ListTablesResponse(listTablesRequest.getCatalogName(), tables, nextToken);
}
Also used : TableName(com.amazonaws.athena.connector.lambda.domain.TableName) ArrayList(java.util.ArrayList) DescribeLogStreamsRequest(com.amazonaws.services.logs.model.DescribeLogStreamsRequest) ListTablesResponse(com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse) DescribeLogStreamsResult(com.amazonaws.services.logs.model.DescribeLogStreamsResult)

Example 27 with ListTablesResponse

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

the class CloudwatchMetadataHandlerTest method doListTables.

@Test
public void doListTables() throws TimeoutException {
    logger.info("doListTables - enter");
    when(mockAwsLogs.describeLogStreams(any(DescribeLogStreamsRequest.class))).thenAnswer((InvocationOnMock invocationOnMock) -> {
        DescribeLogStreamsRequest request = (DescribeLogStreamsRequest) invocationOnMock.getArguments()[0];
        DescribeLogStreamsResult result = new DescribeLogStreamsResult();
        Integer nextToken;
        if (request.getNextToken() == null) {
            nextToken = 1;
        } else if (Integer.valueOf(request.getNextToken()) < 3) {
            nextToken = Integer.valueOf(request.getNextToken()) + 1;
        } else {
            nextToken = null;
        }
        List<LogStream> logStreams = new ArrayList<>();
        if (request.getNextToken() == null || Integer.valueOf(request.getNextToken()) < 3) {
            for (int i = 0; i < 10; i++) {
                LogStream nextLogStream = new LogStream();
                nextLogStream.setLogStreamName("table-" + String.valueOf(i));
                logStreams.add(nextLogStream);
            }
        }
        result.withLogStreams(logStreams);
        if (nextToken != null) {
            result.setNextToken(String.valueOf(nextToken));
        }
        return result;
    });
    ListTablesRequest req = new ListTablesRequest(identity, "queryId", "default", "schema-1", null, UNLIMITED_PAGE_SIZE_VALUE);
    ListTablesResponse res = handler.doListTables(allocator, req);
    logger.info("doListTables - {}", res.getTables());
    assertTrue(res.getTables().contains(new TableName("schema-1", "all_log_streams")));
    assertTrue(res.getTables().size() == 31);
    verify(mockAwsLogs, times(4)).describeLogStreams(any(DescribeLogStreamsRequest.class));
    verify(mockAwsLogs, times(1)).describeLogGroups(any(DescribeLogGroupsRequest.class));
    verifyNoMoreInteractions(mockAwsLogs);
    logger.info("doListTables - exit");
}
Also used : TableName(com.amazonaws.athena.connector.lambda.domain.TableName) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) ListTablesRequest(com.amazonaws.athena.connector.lambda.metadata.ListTablesRequest) DescribeLogStreamsRequest(com.amazonaws.services.logs.model.DescribeLogStreamsRequest) LogStream(com.amazonaws.services.logs.model.LogStream) ListTablesResponse(com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse) DescribeLogStreamsResult(com.amazonaws.services.logs.model.DescribeLogStreamsResult) DescribeLogGroupsRequest(com.amazonaws.services.logs.model.DescribeLogGroupsRequest) Test(org.junit.Test)

Example 28 with ListTablesResponse

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

the class DocDBMetadataHandler method doListTables.

/**
 * List collections in the requested schema in your DocumentDB instance treating the requested schema as an DocumentDB
 * database.
 *
 * @see GlueMetadataHandler
 */
@Override
public ListTablesResponse doListTables(BlockAllocator blockAllocator, ListTablesRequest request) {
    MongoClient client = getOrCreateConn(request);
    List<TableName> tables = new ArrayList<>();
    try (MongoCursor<String> itr = client.getDatabase(request.getSchemaName()).listCollectionNames().iterator()) {
        while (itr.hasNext()) {
            tables.add(new TableName(request.getSchemaName(), itr.next()));
        }
        return new ListTablesResponse(request.getCatalogName(), tables, null);
    }
}
Also used : MongoClient(com.mongodb.client.MongoClient) TableName(com.amazonaws.athena.connector.lambda.domain.TableName) ArrayList(java.util.ArrayList) ListTablesResponse(com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse)

Example 29 with ListTablesResponse

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

the class DocDBMetadataHandlerTest method doListTables.

@Test
public void doListTables() {
    List<String> tableNames = new ArrayList<>();
    tableNames.add("table1");
    tableNames.add("table2");
    tableNames.add("table3");
    MongoDatabase mockDatabase = mock(MongoDatabase.class);
    when(mockClient.getDatabase(eq(DEFAULT_SCHEMA))).thenReturn(mockDatabase);
    when(mockDatabase.listCollectionNames()).thenReturn(StubbingCursor.iterate(tableNames));
    ListTablesRequest req = new ListTablesRequest(IDENTITY, QUERY_ID, DEFAULT_CATALOG, DEFAULT_SCHEMA, null, UNLIMITED_PAGE_SIZE_VALUE);
    ListTablesResponse res = handler.doListTables(allocator, req);
    logger.info("doListTables - {}", res.getTables());
    for (TableName next : res.getTables()) {
        assertEquals(DEFAULT_SCHEMA, next.getSchemaName());
        assertTrue(tableNames.contains(next.getTableName()));
    }
    assertEquals(tableNames.size(), res.getTables().size());
}
Also used : TableName(com.amazonaws.athena.connector.lambda.domain.TableName) ArrayList(java.util.ArrayList) ListTablesRequest(com.amazonaws.athena.connector.lambda.metadata.ListTablesRequest) Matchers.anyString(org.mockito.Matchers.anyString) ListTablesResponse(com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 30 with ListTablesResponse

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

the class GlueMetadataHandler method doListTables.

/**
 * Returns a paginated list of tables from AWS Glue DataCatalog with optional filtering for the requested schema
 * (aka database).
 *
 * @param blockAllocator Tool for creating and managing Apache Arrow Blocks.
 * @param request Provides details on who made the request and which Athena catalog they are querying.
 * @param filter The TableFilter to apply to all tables before adding them to the results list.
 * @return The ListTablesResponse which mostly contains the list of table names.
 * @implNote A complete (un-paginated) list of tables should be returned if the request's pageSize is set to
 * ListTablesRequest.UNLIMITED_PAGE_SIZE_VALUE.
 */
protected ListTablesResponse doListTables(BlockAllocator blockAllocator, ListTablesRequest request, TableFilter filter) throws Exception {
    GetTablesRequest getTablesRequest = new GetTablesRequest();
    getTablesRequest.setCatalogId(getCatalog(request));
    getTablesRequest.setDatabaseName(request.getSchemaName());
    Set<TableName> tables = new HashSet<>();
    String nextToken = request.getNextToken();
    int pageSize = request.getPageSize();
    do {
        getTablesRequest.setNextToken(nextToken);
        if (pageSize != UNLIMITED_PAGE_SIZE_VALUE) {
            // Paginated requests will include the maxResults argument determined by the minimum value between the
            // pageSize and the maximum results supported by Glue (as defined in the Glue API docs).
            int maxResults = Math.min(pageSize, GET_TABLES_REQUEST_MAX_RESULTS);
            getTablesRequest.setMaxResults(maxResults);
            pageSize -= maxResults;
        }
        GetTablesResult result = awsGlue.getTables(getTablesRequest);
        for (Table next : result.getTableList()) {
            if (filter == null || filter.filter(next)) {
                tables.add(new TableName(request.getSchemaName(), next.getName()));
            }
        }
        nextToken = result.getNextToken();
    } while (nextToken != null && (pageSize == UNLIMITED_PAGE_SIZE_VALUE || pageSize > 0));
    return new ListTablesResponse(request.getCatalogName(), tables, nextToken);
}
Also used : TableName(com.amazonaws.athena.connector.lambda.domain.TableName) Table(com.amazonaws.services.glue.model.Table) GetTablesResult(com.amazonaws.services.glue.model.GetTablesResult) ListTablesResponse(com.amazonaws.athena.connector.lambda.metadata.ListTablesResponse) GetTablesRequest(com.amazonaws.services.glue.model.GetTablesRequest) HashSet(java.util.HashSet)

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