Example usage for com.amazonaws.services.dynamodbv2.model StreamViewType NEW_AND_OLD_IMAGES

List of usage examples for com.amazonaws.services.dynamodbv2.model StreamViewType NEW_AND_OLD_IMAGES

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model StreamViewType NEW_AND_OLD_IMAGES.

Prototype

StreamViewType NEW_AND_OLD_IMAGES

To view the source code for com.amazonaws.services.dynamodbv2.model StreamViewType NEW_AND_OLD_IMAGES.

Click Source Link

Usage

From source file:dynamok.source.DynamoDbSourceConnector.java

License:Apache License

@Override
public void start(Map<String, String> props) {
    config = new ConnectorConfig(props);
    streamShards = new HashMap<>();

    final AmazonDynamoDBClient client;
    final AmazonDynamoDBStreamsClient streamsClient;

    if (config.accessKeyId.value().isEmpty() || config.secretKeyId.value().isEmpty()) {
        client = new AmazonDynamoDBClient();
        streamsClient = new AmazonDynamoDBStreamsClient();
        log.debug("AmazonDynamoDB clients created with default credentials");
    } else {//  ww w  .j  a  va2 s. com
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(config.accessKeyId.value(),
                config.secretKeyId.value());
        client = new AmazonDynamoDBClient(awsCreds);
        streamsClient = new AmazonDynamoDBStreamsClient(awsCreds);
        log.debug("AmazonDynamoDB clients created with AWS credentials from connector configuration");
    }

    client.configureRegion(config.region);
    streamsClient.configureRegion(config.region);

    final Set<String> ignoredTables = new HashSet<>();
    final Set<String> consumeTables = new HashSet<>();

    String lastEvaluatedTableName = null;
    do {
        final ListTablesResult listResult = client.listTables(lastEvaluatedTableName);

        for (String tableName : listResult.getTableNames()) {
            if (!acceptTable(tableName)) {
                ignoredTables.add(tableName);
                continue;
            }

            final TableDescription tableDesc = client.describeTable(tableName).getTable();

            final StreamSpecification streamSpec = tableDesc.getStreamSpecification();

            if (streamSpec == null || !streamSpec.isStreamEnabled()) {
                throw new ConnectException(
                        String.format("DynamoDB table `%s` does not have streams enabled", tableName));
            }

            final String streamViewType = streamSpec.getStreamViewType();
            if (!streamViewType.equals(StreamViewType.NEW_IMAGE.name())
                    && !streamViewType.equals(StreamViewType.NEW_AND_OLD_IMAGES.name())) {
                throw new ConnectException(String.format("DynamoDB stream view type for table `%s` is %s",
                        tableName, streamViewType));
            }

            final DescribeStreamResult describeStreamResult = streamsClient
                    .describeStream(new DescribeStreamRequest().withStreamArn(tableDesc.getLatestStreamArn()));

            for (Shard shard : describeStreamResult.getStreamDescription().getShards()) {
                streamShards.put(shard, tableDesc);
            }

            consumeTables.add(tableName);
        }

        lastEvaluatedTableName = listResult.getLastEvaluatedTableName();
    } while (lastEvaluatedTableName != null);

    log.info("Tables to ignore: {}", ignoredTables);
    log.info("Tables to ingest: {}", consumeTables);

    client.shutdown();
    streamsClient.shutdown();
}

From source file:org.wildfly.camel.test.common.aws.DynamoDBUtils.java

License:Apache License

public static TableDescription createTable(AmazonDynamoDB client, String tableName)
        throws InterruptedException {
    CreateTableRequest tableReq = new CreateTableRequest().withTableName(tableName)
            .withKeySchema(new KeySchemaElement("Id", KeyType.HASH))
            .withAttributeDefinitions(new AttributeDefinition("Id", ScalarAttributeType.N))
            .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L))
            .withStreamSpecification(new StreamSpecification().withStreamEnabled(true)
                    .withStreamViewType(StreamViewType.NEW_AND_OLD_IMAGES));

    DynamoDB dynamoDB = new DynamoDB(client);
    Table table = dynamoDB.createTable(tableReq);
    return table.waitForActive();
}

From source file:tr.com.serkanozal.samba.cache.impl.SambaGlobalCache.java

License:Open Source License

private Table ensureTableAvailable() {
    boolean tableExist = false;
    try {//w  ww .  j a  v  a2s.c  o  m
        DYNAMO_DB.describeTable(DYNAMO_DB_TABLE_NAME);
        tableExist = true;
    } catch (ResourceNotFoundException e) {
    }

    if (!tableExist) {
        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("id").withAttributeType("S"));

        ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
        keySchema.add(new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH));

        StreamSpecification streamSpecification = new StreamSpecification();
        streamSpecification.setStreamEnabled(true);
        streamSpecification.setStreamViewType(StreamViewType.NEW_AND_OLD_IMAGES);

        CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(DYNAMO_DB_TABLE_NAME)
                .withKeySchema(keySchema).withAttributeDefinitions(attributeDefinitions)
                .withStreamSpecification(streamSpecification)
                .withProvisionedThroughput(new ProvisionedThroughput()
                        .withReadCapacityUnits((long) DYNAMO_DB_TABLE_READ_CAPACITY_PER_SECOND)
                        .withWriteCapacityUnits((long) DYNAMO_DB_TABLE_WRITE_CAPACITY_PER_SECOND));

        try {
            LOGGER.info(String.format("Creating DynamoDB table (%s) creation, because it is not exist",
                    DYNAMO_DB_TABLE_NAME));

            DYNAMO_DB.createTable(createTableRequest);
        } catch (ResourceInUseException e) {
            LOGGER.info(String.format("Ignoring DynamoDB table (%s) creation, because it is already exist",
                    DYNAMO_DB_TABLE_NAME));
        }
    } else {
        LOGGER.info(String.format("Ignoring DynamoDB table (%s) creation, because it is already exist",
                DYNAMO_DB_TABLE_NAME));
    }

    while (true) {
        DescribeTableResult describeTableResult = DYNAMO_DB.describeTable(DYNAMO_DB_TABLE_NAME);
        TableDescription tableDescription = describeTableResult.getTable();
        if ("ACTIVE".equals(tableDescription.getTableStatus())) {
            break;
        }
        LOGGER.info(String.format("DynamoDB table (%s) is not active yet, waiting until it is active ...",
                DYNAMO_DB_TABLE_NAME));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }

    SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(new StreamListener(), 0, 1000, TimeUnit.MILLISECONDS);

    return new Table(DYNAMO_DB, DYNAMO_DB_TABLE_NAME);
}