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);
}
}
}
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));
}
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.");
}
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);
}
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);
}
Aggregations