Search in sources :

Example 11 with DynamoDbAsyncClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient in project openhab-addons by openhab.

the class BaseIntegrationTest method clearData.

protected static void clearData() {
    DynamoDBPersistenceService localService = service;
    assert localService != null;
    DynamoDbAsyncClient lowLevelClient = localService.getLowLevelClient();
    assertNotNull(lowLevelClient);
    // To get rid of null exception
    assert lowLevelClient != null;
    // Clear data
    for (String table : new String[] { "dynamodb-integration-tests-bigdecimal", "dynamodb-integration-tests-string", TABLE }) {
        try {
            try {
                lowLevelClient.describeTable(req -> req.tableName(table)).get();
            } catch (ExecutionException e) {
                if (e.getCause() instanceof ResourceNotFoundException) {
                    // Table does not exist, this table does not need cleaning, continue to next table
                    continue;
                }
            }
            lowLevelClient.deleteTable(req -> req.tableName(table)).get();
            final WaiterResponse<DescribeTableResponse> waiterResponse;
            waiterResponse = lowLevelClient.waiter().waitUntilTableNotExists(req -> req.tableName(table)).get();
            Optional<Throwable> waiterException = waiterResponse.matched().exception().filter(e -> !(e instanceof ResourceNotFoundException));
            assertTrue(waiterException.isEmpty(), waiterException::toString);
        } catch (ExecutionException | InterruptedException e) {
            fail("Error cleaning up test (deleting table)", e);
        }
    }
}
Also used : Unit(javax.measure.Unit) RegistryChangeListener(org.openhab.core.common.registry.RegistryChangeListener) LoggerFactory(org.slf4j.LoggerFactory) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) ServerSocket(java.net.ServerSocket) AfterAll(org.junit.jupiter.api.AfterAll) DateTimeItem(org.openhab.core.library.items.DateTimeItem) CallItem(org.openhab.core.library.items.CallItem) I18nProviderImpl(org.openhab.core.internal.i18n.I18nProviderImpl) Temperature(javax.measure.quantity.Temperature) Nullable(org.eclipse.jdt.annotation.Nullable) BeforeAll(org.junit.jupiter.api.BeforeAll) GenericItem(org.openhab.core.items.GenericItem) Map(java.util.Map) URI(java.net.URI) WaiterResponse(software.amazon.awssdk.core.waiters.WaiterResponse) Dimensionless(javax.measure.quantity.Dimensionless) NumberItem(org.openhab.core.library.items.NumberItem) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Collection(java.util.Collection) UnitProvider(org.openhab.core.i18n.UnitProvider) ServerRunner(com.amazonaws.services.dynamodbv2.local.main.ServerRunner) TestInfo(org.junit.jupiter.api.TestInfo) BundleContext(org.osgi.framework.BundleContext) Stream(java.util.stream.Stream) Entry(java.util.Map.Entry) Optional(java.util.Optional) DynamoDBProxyServer(com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer) SIUnits(org.openhab.core.library.unit.SIUnits) DescribeTableResponse(software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) ComponentContext(org.osgi.service.component.ComponentContext) HashMap(java.util.HashMap) ColorItem(org.openhab.core.library.items.ColorItem) ItemNotUniqueException(org.openhab.core.items.ItemNotUniqueException) Hashtable(java.util.Hashtable) Units(org.openhab.core.library.unit.Units) LocationItem(org.openhab.core.library.items.LocationItem) GroupItem(org.openhab.core.items.GroupItem) Logger(org.slf4j.Logger) RollershutterItem(org.openhab.core.library.items.RollershutterItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) Mockito.when(org.mockito.Mockito.when) Item(org.openhab.core.items.Item) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) ItemRegistry(org.openhab.core.items.ItemRegistry) StringItem(org.openhab.core.library.items.StringItem) Mockito(org.mockito.Mockito) RegistryHook(org.openhab.core.items.RegistryHook) PlayerItem(org.openhab.core.library.items.PlayerItem) ContactItem(org.openhab.core.library.items.ContactItem) JavaTest(org.openhab.core.test.java.JavaTest) Assertions(org.junit.jupiter.api.Assertions) DescribeTableResponse(software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) ExecutionException(java.util.concurrent.ExecutionException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)

Example 12 with DynamoDbAsyncClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient in project thunder by RohanNagar.

the class DynamoDbUsersDaoFactoryTest method testCreateUsersDaoTableExists.

@Test
void testCreateUsersDaoTableExists() {
    UsersDaoFactory usersDaoFactory = TestResources.readResourceYaml(UsersDaoFactory.class, "fixtures/configuration/dao/dynamodb-config.yaml");
    assertTrue(usersDaoFactory instanceof DynamoDbUsersDaoFactory);
    DynamoDbUsersDaoFactory dynamoDbUsersDaoFactory = (DynamoDbUsersDaoFactory) usersDaoFactory;
    // Set the client to already be created
    DynamoDbAsyncClient client = mock(DynamoDbAsyncClient.class);
    when(client.listTables()).thenReturn(CompletableFuture.completedFuture(ListTablesResponse.builder().tableNames("test-table").build()));
    dynamoDbUsersDaoFactory.dynamoDbClient = client;
    usersDaoFactory.createUsersDao(TestResources.MAPPER);
    // List tables would have returned the right table
    verify(client, times(1)).listTables();
    verify(client, times(0)).createTable(any(CreateTableRequest.class));
}
Also used : DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) UsersDaoFactory(com.sanctionco.thunder.dao.UsersDaoFactory) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) Test(org.junit.jupiter.api.Test)

Example 13 with DynamoDbAsyncClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient in project graylog-plugin-integrations by Graylog2.

the class KinesisConsumer method run.

public void run() {
    LOG.debug("Starting the Kinesis Consumer.");
    AwsCredentialsProvider credentialsProvider = AWSAuthFactory.create(request.region(), request.awsAccessKeyId(), request.awsSecretAccessKey(), request.assumeRoleArn());
    final Region region = Region.of(request.region());
    final DynamoDbAsyncClientBuilder dynamoDbClientBuilder = DynamoDbAsyncClient.builder();
    AWSClientBuilderUtil.initializeBuilder(dynamoDbClientBuilder, request.dynamodbEndpoint(), region, credentialsProvider);
    final DynamoDbAsyncClient dynamoClient = dynamoDbClientBuilder.build();
    final CloudWatchAsyncClientBuilder cloudwatchClientBuilder = CloudWatchAsyncClient.builder();
    AWSClientBuilderUtil.initializeBuilder(cloudwatchClientBuilder, request.cloudwatchEndpoint(), region, credentialsProvider);
    final CloudWatchAsyncClient cloudWatchClient = cloudwatchClientBuilder.build();
    final KinesisAsyncClientBuilder kinesisAsyncClientBuilder = KinesisAsyncClient.builder();
    AWSClientBuilderUtil.initializeBuilder(kinesisAsyncClientBuilder, request.kinesisEndpoint(), region, credentialsProvider);
    final KinesisAsyncClient kinesisAsyncClient = KinesisClientUtil.createKinesisAsyncClient(kinesisAsyncClientBuilder);
    final String workerId = String.format(Locale.ENGLISH, "graylog-node-%s", nodeId.anonymize());
    LOG.debug("Using workerId [{}].", workerId);
    // The application name needs to be unique per input/consumer.
    final String applicationName = String.format(Locale.ENGLISH, "graylog-aws-plugin-%s", kinesisStreamName);
    LOG.debug("Using Kinesis applicationName [{}].", applicationName);
    // The KinesisShardProcessorFactory contains the message processing logic.
    final KinesisShardProcessorFactory kinesisShardProcessorFactory = new KinesisShardProcessorFactory(objectMapper, transport, handleMessageCallback, kinesisStreamName, awsMessageType);
    ConfigsBuilder configsBuilder = new ConfigsBuilder(kinesisStreamName, applicationName, kinesisAsyncClient, dynamoClient, cloudWatchClient, workerId, kinesisShardProcessorFactory);
    final PollingConfig pollingConfig = new PollingConfig(kinesisStreamName, kinesisAsyncClient);
    // Default max records per request is 10k.
    if (recordBatchSize != null) {
        LOG.debug("Using explicit batch size [{}]", recordBatchSize);
        pollingConfig.maxRecords(recordBatchSize);
    }
    this.kinesisScheduler = new Scheduler(configsBuilder.checkpointConfig(), configsBuilder.coordinatorConfig(), configsBuilder.leaseManagementConfig(), configsBuilder.lifecycleConfig(), configsBuilder.metricsConfig(), configsBuilder.processorConfig(), configsBuilder.retrievalConfig().retrievalSpecificConfig(pollingConfig));
    LOG.debug("Starting Kinesis scheduler.");
    kinesisScheduler.run();
    LOG.debug("After Kinesis scheduler stopped.");
}
Also used : KinesisAsyncClient(software.amazon.awssdk.services.kinesis.KinesisAsyncClient) CloudWatchAsyncClient(software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient) Scheduler(software.amazon.kinesis.coordinator.Scheduler) PollingConfig(software.amazon.kinesis.retrieval.polling.PollingConfig) ConfigsBuilder(software.amazon.kinesis.common.ConfigsBuilder) CloudWatchAsyncClientBuilder(software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClientBuilder) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) Region(software.amazon.awssdk.regions.Region) DynamoDbAsyncClientBuilder(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClientBuilder) KinesisAsyncClientBuilder(software.amazon.awssdk.services.kinesis.KinesisAsyncClientBuilder)

Example 14 with DynamoDbAsyncClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient in project aws-doc-sdk-examples by awsdocs.

the class AsyncPagination method AutoPaginationOnCollection.

private static void AutoPaginationOnCollection() throws InterruptedException, ExecutionException {
    // snippet-start:[dynamodb.java2.async_pagination.asyncclient]
    System.out.println("running AutoPagination - iterating on item collection...\n");
    // Creates a default client with credentials and regions loaded from the environment
    final DynamoDbAsyncClient asyncClient = DynamoDbAsyncClient.create();
    ListTablesRequest listTablesRequest = ListTablesRequest.builder().limit(3).build();
    // snippet-end:[dynamodb.java2.async_pagination.asyncclient]
    ListTablesPublisher listTablesPublisher = asyncClient.listTablesPaginator(listTablesRequest);
    SdkPublisher<String> publisher = listTablesPublisher.tableNames();
    // snippet-start:[dynamodb.java2.async_pagination.foreach]
    // Use forEach
    CompletableFuture<Void> future = publisher.subscribe(System.out::println);
    future.get();
    // snippet-end:[dynamodb.java2.async_pagination.foreach]
    // snippet-start:[dynamodb.java2.async_pagination.subscriber]
    // Use subscriber
    publisher.subscribe(new Subscriber<String>() {

        private Subscription subscription;

        @Override
        public void onSubscribe(Subscription s) {
            subscription = s;
            subscription.request(1);
        }

        @Override
        public void onNext(String tableName) {
            System.out.println(tableName);
            subscription.request(1);
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onComplete() {
        }
    });
    // As the above code is non-blocking, make sure your application doesn't end immediately
    // For this example, I am using Thread.sleep to wait for all pages to get delivered
    Thread.sleep(3_000);
}
Also used : DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) ListTablesRequest(software.amazon.awssdk.services.dynamodb.model.ListTablesRequest) Subscription(org.reactivestreams.Subscription) ListTablesPublisher(software.amazon.awssdk.services.dynamodb.paginators.ListTablesPublisher)

Example 15 with DynamoDbAsyncClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBAsyncGetItem method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    DynamoDBAsyncGetItem <table> <key> <keyVal>\n\n" + "Where:\n" + "    table - the table from which an item is retrieved (i.e., Music3)\n" + "    key -  the key used in the table (i.e., Artist) \n" + "    keyval  - the key value that represents the item to get (i.e., Famous Band)\n" + " Example:\n" + "    Music3 Artist Famous Band\n" + "  **Warning** This program will actually retrieve an item\n" + "            that you specify!\n";
    if (args.length < 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String tableName = args[0];
    String key = args[1];
    String keyVal = args[2];
    // Create the DynamoDbAsyncClient object
    Region region = Region.US_WEST_2;
    DynamoDbAsyncClient client = DynamoDbAsyncClient.builder().region(region).build();
    System.out.format("Retrieving item \"%s\" from \"%s\"\n", keyVal, tableName);
    getItem(client, tableName, key, keyVal);
}
Also used : DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) Region(software.amazon.awssdk.regions.Region)

Aggregations

DynamoDbAsyncClient (software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient)24 Region (software.amazon.awssdk.regions.Region)9 List (java.util.List)5 ExecutionException (java.util.concurrent.ExecutionException)5 URI (java.net.URI)4 Map (java.util.Map)4 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)4 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)4 NumberItem (org.openhab.core.library.items.NumberItem)4 DynamoDbAsyncClientBuilder (software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClientBuilder)4 ListTablesResponse (software.amazon.awssdk.services.dynamodb.model.ListTablesResponse)4 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)4 UsersDaoFactory (com.sanctionco.thunder.dao.UsersDaoFactory)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Unit (javax.measure.Unit)3 Nullable (org.eclipse.jdt.annotation.Nullable)3 Test (org.junit.jupiter.api.Test)3 ListTablesPublisher (software.amazon.awssdk.services.dynamodb.paginators.ListTablesPublisher)3