Search in sources :

Example 6 with DynamoDbAsyncClient

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

the class AsyncPagination method useThirdPartySubscriber_Reactor.

private static void useThirdPartySubscriber_Reactor() {
    System.out.println("running AutoPagination - using third party subscriber...\n");
    DynamoDbAsyncClient asyncClient = DynamoDbAsyncClient.create();
    ListTablesPublisher publisher = asyncClient.listTablesPaginator(ListTablesRequest.builder().build());
    // The Flux class has many helper methods that work with any reactive streams compatible publisher implementation
    List<String> tables = Flux.from(publisher).flatMapIterable(ListTablesResponse::tableNames).collectList().block();
    System.out.println(tables);
}
Also used : DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) ListTablesResponse(software.amazon.awssdk.services.dynamodb.model.ListTablesResponse) ListTablesPublisher(software.amazon.awssdk.services.dynamodb.paginators.ListTablesPublisher)

Example 7 with DynamoDbAsyncClient

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

the class DynamoDBAsyncCreateTable method main.

public static void main(String[] args) throws InterruptedException {
    final String USAGE = "\n" + "Usage:\n" + "    DynamoDBAsyncCreateTable <table> <key >\n\n" + "Where:\n" + "    table - the table to create (i.e., Music3)\n\n" + "    key   - the key for the table (i.e., Artist)\n" + "Example:\n" + "    Music3 Artist \n";
    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    /* Read the name from command args */
    String tableName = args[0];
    String key = args[1];
    // Create the DynamoDbAsyncClient object
    Region region = Region.US_EAST_1;
    DynamoDbAsyncClient client = DynamoDbAsyncClient.builder().region(region).build();
    createTable(client, tableName, key);
}
Also used : DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) Region(software.amazon.awssdk.regions.Region)

Example 8 with DynamoDbAsyncClient

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

the class DynamoDBAsyncListTables method main.

public static void main(String[] args) throws InterruptedException {
    // Create the DynamoDbAsyncClient object
    Region region = Region.US_EAST_1;
    DynamoDbAsyncClient client = DynamoDbAsyncClient.builder().region(region).build();
    listTables(client);
}
Also used : DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) Region(software.amazon.awssdk.regions.Region)

Example 9 with DynamoDbAsyncClient

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

the class DynamoDBPersistenceService method store.

@Override
public void store(Item item, @Nullable String alias) {
    // Timestamp and capture state immediately as rest of the store is asynchronous (state might change in between)
    ZonedDateTime time = ZonedDateTime.now();
    logIfManyQueuedTasks();
    if (!(item instanceof GenericItem)) {
        return;
    }
    if (item.getState() instanceof UnDefType) {
        logger.debug("Undefined item state received. Not storing item {}.", item.getName());
        return;
    }
    if (!isReadyToStore()) {
        logger.warn("Not ready to store (config error?), not storing item {}.", item.getName());
        return;
    }
    // Get Item describing the real type of data
    // With non-group items this is same as the argument item. With Group items, this is item describing the type of
    // state stored in the group.
    final Item itemTemplate;
    try {
        itemTemplate = getEffectiveItem(item);
    } catch (IllegalStateException e) {
        // Logged already
        return;
    }
    String effectiveName = (alias != null) ? alias : item.getName();
    // We do not want to rely item.state since async context below can execute much later.
    // We 'copy' the item for local use. copyItem also normalizes the unit with NumberItems.
    final GenericItem copiedItem = copyItem(itemTemplate, item, effectiveName, null);
    resolveTableSchema().thenAcceptAsync(resolved -> {
        if (!resolved) {
            logger.warn("Table schema not resolved, not storing item {}.", copiedItem.getName());
            return;
        }
        DynamoDbEnhancedAsyncClient localClient = client;
        DynamoDbAsyncClient localLowlevelClient = lowLevelClient;
        DynamoDBConfig localConfig = dbConfig;
        DynamoDBTableNameResolver localTableNameResolver = tableNameResolver;
        if (!isProperlyConfigured || localClient == null || localLowlevelClient == null || localConfig == null || localTableNameResolver == null) {
            logger.warn("Not ready to store (config error?), not storing item {}.", item.getName());
            return;
        }
        Integer expireDays = localConfig.getExpireDays();
        final DynamoDBItem<?> dto;
        switch(localTableNameResolver.getTableSchema()) {
            case NEW:
                dto = AbstractDynamoDBItem.fromStateNew(copiedItem, time, expireDays);
                break;
            case LEGACY:
                dto = AbstractDynamoDBItem.fromStateLegacy(copiedItem, time);
                break;
            default:
                throw new IllegalStateException("Unexpected. Bug");
        }
        logger.trace("store() called with item {} {} '{}', which was converted to DTO {}", copiedItem.getClass().getSimpleName(), effectiveName, copiedItem.getState(), dto);
        dto.accept(new DynamoDBItemVisitor<TableCreatingPutItem<? extends DynamoDBItem<?>>>() {

            @Override
            public TableCreatingPutItem<? extends DynamoDBItem<?>> visit(DynamoDBBigDecimalItem dynamoBigDecimalItem) {
                return new TableCreatingPutItem<DynamoDBBigDecimalItem>(DynamoDBPersistenceService.this, dynamoBigDecimalItem, getTable(DynamoDBBigDecimalItem.class));
            }

            @Override
            public TableCreatingPutItem<? extends DynamoDBItem<?>> visit(DynamoDBStringItem dynamoStringItem) {
                return new TableCreatingPutItem<DynamoDBStringItem>(DynamoDBPersistenceService.this, dynamoStringItem, getTable(DynamoDBStringItem.class));
            }
        }).putItemAsync();
    }, executor).exceptionally(e -> {
        logger.error("Unexcepted error", e);
        return null;
    });
}
Also used : Unit(javax.measure.Unit) Constants(org.osgi.framework.Constants) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) PersistenceStrategy(org.openhab.core.persistence.strategy.PersistenceStrategy) NettyNioAsyncHttpClient(software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient) Nullable(org.eclipse.jdt.annotation.Nullable) GenericItem(org.openhab.core.items.GenericItem) Locale(java.util.Locale) Duration(java.time.Duration) Map(java.util.Map) DynamoDbAsyncTable(software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable) URI(java.net.URI) NumberItem(org.openhab.core.library.items.NumberItem) ClientAsyncConfiguration(software.amazon.awssdk.core.client.config.ClientAsyncConfiguration) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Deactivate(org.osgi.service.component.annotations.Deactivate) UnDefType(org.openhab.core.types.UnDefType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) TableSchema(software.amazon.awssdk.enhanced.dynamodb.TableSchema) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) BundleContext(org.osgi.framework.BundleContext) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) PersistenceService(org.openhab.core.persistence.PersistenceService) SdkAdvancedAsyncClientOption(software.amazon.awssdk.core.client.config.SdkAdvancedAsyncClientOption) QueryEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.QueryEnhancedRequest) AwsRequestOverrideConfiguration(software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration) FilterCriteria(org.openhab.core.persistence.FilterCriteria) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CompletableFuture(java.util.concurrent.CompletableFuture) DynamoDbEnhancedAsyncClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedAsyncClient) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) HistoricItem(org.openhab.core.persistence.HistoricItem) Component(org.osgi.service.component.annotations.Component) DefaultsMode(software.amazon.awssdk.awscore.defaultsmode.DefaultsMode) QueryablePersistenceService(org.openhab.core.persistence.QueryablePersistenceService) Activate(org.osgi.service.component.annotations.Activate) Subscriber(org.reactivestreams.Subscriber) ExecutorService(java.util.concurrent.ExecutorService) DynamoDbAsyncClientBuilder(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClientBuilder) QuantityType(org.openhab.core.library.types.QuantityType) ThreadPoolManager(org.openhab.core.common.ThreadPoolManager) GroupItem(org.openhab.core.items.GroupItem) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ConfigurableService(org.openhab.core.config.core.ConfigurableService) State(org.openhab.core.types.State) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) SdkPublisher(software.amazon.awssdk.core.async.SdkPublisher) PersistenceItemInfo(org.openhab.core.persistence.PersistenceItemInfo) 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) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) ClientOverrideConfiguration(software.amazon.awssdk.core.client.config.ClientOverrideConfiguration) GenericItem(org.openhab.core.items.GenericItem) UnDefType(org.openhab.core.types.UnDefType) GenericItem(org.openhab.core.items.GenericItem) NumberItem(org.openhab.core.library.items.NumberItem) HistoricItem(org.openhab.core.persistence.HistoricItem) GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) ZonedDateTime(java.time.ZonedDateTime) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) DynamoDbEnhancedAsyncClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedAsyncClient)

Example 10 with DynamoDbAsyncClient

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

the class DynamoDBPersistenceService method disconnect.

private void disconnect() {
    DynamoDbAsyncClient localLowLevelClient = lowLevelClient;
    if (client == null || localLowLevelClient == null) {
        return;
    }
    localLowLevelClient.close();
    lowLevelClient = null;
    client = null;
    dbConfig = null;
    tableNameResolver = null;
    isProperlyConfigured = false;
    tableCache.clear();
}
Also used : DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient)

Aggregations

DynamoDbAsyncClient (software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient)32 Region (software.amazon.awssdk.regions.Region)9 ExecutorService (java.util.concurrent.ExecutorService)7 Test (org.junit.jupiter.api.Test)6 List (java.util.List)5 ClientConfiguration (com.amazonaws.ClientConfiguration)4 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)4 ClientResources (io.lettuce.core.resource.ClientResources)4 URI (java.net.URI)4 Map (java.util.Map)4 ListTablesResponse (software.amazon.awssdk.services.dynamodb.model.ListTablesResponse)4 Context (io.vertx.core.Context)3 Timeout (io.vertx.junit5.Timeout)3 VertxTestContext (io.vertx.junit5.VertxTestContext)3 Clock (java.time.Clock)3 Set (java.util.Set)3 ExecutionException (java.util.concurrent.ExecutionException)3 Order (org.junit.jupiter.api.Order)3 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)3 ExternalServiceCredentialGenerator (org.whispersystems.textsecuregcm.auth.ExternalServiceCredentialGenerator)3