Search in sources :

Example 1 with AmazonAthena

use of com.amazonaws.services.athena.AmazonAthena in project cerberus by Nike-Inc.

the class AthenaClientFactoryTest method testGetClientAlwaysReturnsSameAthenaInstance.

@Test
public void testGetClientAlwaysReturnsSameAthenaInstance() {
    AthenaClientFactory athenaClientFactory = new AthenaClientFactory();
    AmazonAthena clientInstance1 = athenaClientFactory.getClient("region-2");
    AmazonAthena clientInstance2 = athenaClientFactory.getClient("region-2");
    Assert.assertSame(clientInstance1, clientInstance2);
}
Also used : AmazonAthena(com.amazonaws.services.athena.AmazonAthena) Test(org.junit.Test)

Example 2 with AmazonAthena

use of com.amazonaws.services.athena.AmazonAthena in project cerberus by Nike-Inc.

the class AthenaClientFactory method getClient.

public AmazonAthena getClient(String region) {
    AmazonAthena client = athenaClientMap.get(region);
    if (client == null) {
        client = AmazonAthenaClient.builder().withRegion(region).build();
        athenaClientMap.put(region, client);
    }
    return client;
}
Also used : AmazonAthena(com.amazonaws.services.athena.AmazonAthena)

Example 3 with AmazonAthena

use of com.amazonaws.services.athena.AmazonAthena in project cerberus by Nike-Inc.

the class AthenaService method addPartitionIfMissing.

public void addPartitionIfMissing(String region, String bucket, String year, String month, String day, String hour) {
    String partition = String.format("year=%s/month=%s/day=%s/hour=%s", year, month, day, hour);
    String table = String.format(TABLE_TEMPLATE, environmentName);
    if (!partitions.contains(partition)) {
        try {
            String query = String.format("ALTER TABLE %s ADD PARTITION (year='%s', month='%s', day='%s', hour='%s') " + "LOCATION 's3://%s/audit-logs/partitioned/year=%s/month=%s/day=%s/hour=%s'", table, year, month, day, hour, bucket, year, month, day, hour);
            AmazonAthena athena = athenaClientFactory.getClient(region);
            StartQueryExecutionResult result = athena.startQueryExecution(new StartQueryExecutionRequest().withQueryString(query).withResultConfiguration(new ResultConfiguration().withOutputLocation(String.format("s3://%s/results/", bucket))));
            log.debug("Started query: '{}' to add partition: '{}' to table: '{}'", result.getQueryExecutionId(), partition, table);
            partitions.add(partition);
        } catch (AmazonClientException e) {
            log.error("Failed to start add partition query for year={}/month={}/day={}/hour={}", year, month, day, hour, e);
        }
    }
}
Also used : StartQueryExecutionResult(com.amazonaws.services.athena.model.StartQueryExecutionResult) ResultConfiguration(com.amazonaws.services.athena.model.ResultConfiguration) AmazonClientException(com.amazonaws.AmazonClientException) StartQueryExecutionRequest(com.amazonaws.services.athena.model.StartQueryExecutionRequest) AmazonAthena(com.amazonaws.services.athena.AmazonAthena)

Example 4 with AmazonAthena

use of com.amazonaws.services.athena.AmazonAthena in project cerberus by Nike-Inc.

the class AthenaServiceTest method test_addPartition_fails_when_amazon_client_exception.

@Test
public void test_addPartition_fails_when_amazon_client_exception() {
    String awsRegion = "us-west-2";
    AmazonAthena athena = mock(AmazonAthena.class);
    when(athenaClientFactory.getClient(awsRegion)).thenReturn(athena);
    when(athena.startQueryExecution(Mockito.any())).thenThrow(AmazonClientException.class);
    athenaService.addPartitionIfMissing(awsRegion, "fake-bucket", "2018", "01", "29", "12");
    verify(athenaClientFactory, times(1)).getClient(anyString());
    assertEquals(0, partitions.size());
}
Also used : AmazonAthena(com.amazonaws.services.athena.AmazonAthena) Test(org.junit.Test)

Example 5 with AmazonAthena

use of com.amazonaws.services.athena.AmazonAthena in project cerberus by Nike-Inc.

the class AthenaServiceTest method test_that_addPartition_works.

@Test
public void test_that_addPartition_works() {
    String awsRegion = "us-west-2";
    AmazonAthena athena = mock(AmazonAthena.class);
    when(athenaClientFactory.getClient(awsRegion)).thenReturn(athena);
    when(athena.startQueryExecution(Mockito.any())).thenReturn(new StartQueryExecutionResult().withQueryExecutionId("query-execution-id"));
    athenaService.addPartitionIfMissing(awsRegion, "fake-bucket", "2018", "01", "29", "12");
    verify(athenaClientFactory, times(1)).getClient(anyString());
    assertEquals(1, partitions.size());
}
Also used : StartQueryExecutionResult(com.amazonaws.services.athena.model.StartQueryExecutionResult) AmazonAthena(com.amazonaws.services.athena.AmazonAthena) Test(org.junit.Test)

Aggregations

AmazonAthena (com.amazonaws.services.athena.AmazonAthena)5 Test (org.junit.Test)3 StartQueryExecutionResult (com.amazonaws.services.athena.model.StartQueryExecutionResult)2 AmazonClientException (com.amazonaws.AmazonClientException)1 ResultConfiguration (com.amazonaws.services.athena.model.ResultConfiguration)1 StartQueryExecutionRequest (com.amazonaws.services.athena.model.StartQueryExecutionRequest)1