Example usage for com.amazonaws.services.dynamodbv2.document DynamoDB DynamoDB

List of usage examples for com.amazonaws.services.dynamodbv2.document DynamoDB DynamoDB

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document DynamoDB DynamoDB.

Prototype

public DynamoDB(Regions regionEnum) 

Source Link

Document

Create a DynamoDB object that talks to the specified AWS region.

Usage

From source file:com.envirover.spl.stream.MAVLinkMessagesTable.java

License:Open Source License

public MAVLinkMessagesTable() {
    if (System.getenv(SPL_DYNAMODB_TABLE) != null) {
        tableName = System.getenv(SPL_DYNAMODB_TABLE);
    }/*from   w w w .j a v  a 2s  . c  o m*/

    AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient();

    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);

    table = dynamoDB.getTable(tableName);
}

From source file:com.erudika.para.persistence.AWSDynamoUtils.java

License:Apache License

/**
 * Returns a client instance for AWS DynamoDB.
 * @return a client that talks to DynamoDB
 */// w  w w . ja  va 2  s  . c  om
public static AmazonDynamoDBClient getClient() {
    if (ddbClient != null) {
        return ddbClient;
    }

    if (Config.IN_PRODUCTION) {
        Region region = Regions.getCurrentRegion();
        region = region != null ? region : Region.getRegion(Regions.fromName(Config.AWS_REGION));
        ddbClient = new AmazonDynamoDBClient(
                new BasicAWSCredentials(Config.AWS_ACCESSKEY, Config.AWS_SECRETKEY)).withRegion(region);

    } else {
        ddbClient = new AmazonDynamoDBClient(new BasicAWSCredentials("local", "null"))
                .withEndpoint(LOCAL_ENDPOINT);
    }

    if (!existsTable(Config.APP_NAME_NS)) {
        createTable(Config.APP_NAME_NS);
    }

    ddb = new DynamoDB(ddbClient);

    Para.addDestroyListener(new DestroyListener() {
        public void onDestroy() {
            shutdownClient();
        }
    });

    return ddbClient;
}

From source file:com.euclidespaim.dynamodb.CreateTableFunction.java

License:Open Source License

public static void main(String[] args) throws Exception {

    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
            new AwsClientBuilder.EndpointConfiguration("https://dynamodb.us-east-1.amazonaws.com", "us-east-1"))
            .build();//from  ww  w  .j a v  a2 s .co m

    DynamoDB dynamoDB = new DynamoDB(client);

    String tableName = "Movies";

    try {
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName, Arrays.asList(new KeySchemaElement("year", KeyType.HASH), // Partition
                // key
                new KeySchemaElement("title", KeyType.RANGE)), // Sort key
                Arrays.asList(new AttributeDefinition("year", ScalarAttributeType.N),
                        new AttributeDefinition("title", ScalarAttributeType.S)),
                new ProvisionedThroughput(10L, 10L));
        table.waitForActive();
        System.out.println("Success.  Table status: " + table.getDescription().getTableStatus());

    } catch (Exception e) {
        System.err.println("Unable to create table: ");
        System.err.println(e.getMessage());
    }

}

From source file:com.exorath.service.lobbymsg.impl.DynamoDBProvider.java

License:Apache License

public DynamoDB getDB() {
    AWSCredentials credentials = new EnvironmentVariableCredentialsProvider().getCredentials();
    return new DynamoDB(new AmazonDynamoDBClient(credentials).withRegion(Regions.EU_CENTRAL_1));
}

From source file:com.github.sdmcraft.slingdynamo.impl.DynamoDBResourceProviderFactory.java

License:Open Source License

/**
 * Activate./*from w  w  w .  ja v  a 2s .c  o  m*/
 *
 * @param context the context
 * @param config the config
 */
@Activate
protected void activate(BundleContext context, Map<String, Object> config) {
    this.root = PropertiesUtil.toString(config.get(ResourceProvider.ROOTS), DEFAULT_ROOT);

    if ((this.root == null) || this.root.isEmpty()) {
        this.root = DEFAULT_ROOT;
    }

    this.resourceType = PropertiesUtil.toString(config.get(SlingConstants.PROPERTY_RESOURCE_TYPE),
            Constants.DEFAULT_GET_SERVLET);

    if ((this.resourceType == null) || this.resourceType.isEmpty()) {
        this.resourceType = Constants.DEFAULT_GET_SERVLET;
    }

    dynamoDBClient = new AmazonDynamoDBClient(awsCredentialsProvider.getCredentials());

    this.region = PropertiesUtil.toString(config.get(PROP_REGION), Constants.DEFAULT_REGION);

    if ((this.region != null) && !this.region.isEmpty()) {
        Region awsRegion = Region.getRegion(Regions.fromName(region));
        dynamoDBClient.setRegion(awsRegion);
    }

    this.endpoint = PropertiesUtil.toString(config.get(PROP_ENDPOINT), Constants.DEFAULT_ENDPOINT);

    if ((this.endpoint != null) && !this.endpoint.isEmpty()) {
        dynamoDBClient.setEndpoint(this.endpoint);
    }

    dynamoDB = new DynamoDB(dynamoDBClient);
}

From source file:com.innoq.hagmans.bachelor.TemperatureConsumer.java

License:Open Source License

public static void main(String[] args) throws InterruptedException {
    if (args.length == 2) {
        streamName = args[0];//from w w  w .  j a v a2  s.co  m
        db_name = args[1];
    }

    // Initialize Utils
    KinesisClientLibConfiguration config = new KinesisClientLibConfiguration(db_name, streamName,
            new DefaultAWSCredentialsProviderChain(), "KinesisProducerLibSampleConsumer")
                    .withRegionName(TemperatureProducer.REGION)
                    .withInitialPositionInStream(InitialPositionInStream.TRIM_HORIZON);

    Region region = RegionUtils.getRegion(TemperatureProducer.REGION);
    AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
    AmazonDynamoDB amazonDynamoDB = new AmazonDynamoDBClient(credentialsProvider, new ClientConfiguration());
    AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentialsProvider);
    client.setRegion(region);
    DynamoDB dynamoDB = new DynamoDB(client);
    amazonDynamoDB.setRegion(region);
    DynamoDBUtils dbUtils = new DynamoDBUtils(dynamoDB, amazonDynamoDB, client);
    AmazonKinesis kinesis = new AmazonKinesisClient(credentialsProvider, new ClientConfiguration());
    kinesis.setRegion(region);
    StreamUtils streamUtils = new StreamUtils(kinesis);
    try {
        if (!streamUtils.isActive(kinesis.describeStream(streamName))) {
            log.info("Stream is not active. Waiting for Stream to become active....");
            streamUtils.waitForStreamToBecomeActive(streamName);
        }
    } catch (ResourceNotFoundException e) {
        log.info("Stream is not created right now. Waiting for stream to get created and become active....");
        streamUtils.waitForStreamToBecomeActive(streamName);
    }
    dbUtils.deleteTable(db_name);
    dbUtils.createTemperatureTableIfNotExists(tableName);

    Thread.sleep(1000);

    final TemperatureConsumer consumer = new TemperatureConsumer();

    new Worker.Builder().recordProcessorFactory(consumer).config(config).build().run();
}

From source file:com.intuit.tank.persistence.databases.AmazonDynamoDatabaseDocApi.java

License:Open Source License

private void addItemsToTable(String tableName, final BatchWriteItemRequest request) {

    boolean shouldRetry;
    int retries = 0;

    do {/*from   w ww.  j a  va  2  s. c  o m*/
        shouldRetry = false;
        try {
            BatchWriteItemResult result = dynamoDb.batchWriteItem(request);
            if (result != null) {
                try {
                    List<ConsumedCapacity> consumedCapacity = result.getConsumedCapacity();
                    for (ConsumedCapacity cap : consumedCapacity) {
                        logger.info(cap.getCapacityUnits());
                    }
                } catch (Exception e) {
                    // ignore this
                }
            }
        } catch (AmazonServiceException e) {
            if (e instanceof ProvisionedThroughputExceededException) {
                try {
                    DynamoDB db = new DynamoDB(dynamoDb);
                    Table table = db.getTable(tableName);
                    ProvisionedThroughputDescription oldThroughput = table.getDescription()
                            .getProvisionedThroughput();
                    logger.info("ProvisionedThroughputExceeded throughput = " + oldThroughput);
                    ProvisionedThroughput newThroughput = new ProvisionedThroughput()
                            .withReadCapacityUnits(
                                    table.getDescription().getProvisionedThroughput().getReadCapacityUnits())
                            .withWriteCapacityUnits(getIncreasedThroughput(
                                    table.getDescription().getProvisionedThroughput().getReadCapacityUnits()));
                    if (!oldThroughput.equals(newThroughput)) {
                        logger.info("Updating throughput to " + newThroughput);
                        table.updateTable(newThroughput);
                        table.waitForActive();
                    }
                } catch (Exception e1) {
                    logger.error("Error increasing capacity: " + e, e);
                }
            }
            int status = e.getStatusCode();
            if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR || status == HttpStatus.SC_SERVICE_UNAVAILABLE) {
                shouldRetry = true;
                long delay = (long) (Math.random() * (Math.pow(4, retries++) * 100L));
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException iex) {
                    logger.error("Caught InterruptedException exception", iex);
                }
            } else {
                logger.error("Error writing to DB: " + e.getMessage());
                throw new RuntimeException(e);
            }
        }
    } while (shouldRetry && retries < MAX_NUMBER_OF_RETRIES);

}

From source file:com.kitac.kinesissamples.consumer.communication.DynamoDBManager.java

License:Open Source License

private DynamoDBManager() {
    config = new ClientConfiguration();

    /* TODO: ClientConfiguration setting code is here. */

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(
            // For use IAM role, specify DefaultAWSCredentialsProviderChain instance.
            new DefaultAWSCredentialsProviderChain(), config);

    client.setRegion(Regions.AP_NORTHEAST_1);

    dynamoDBConnection = new DynamoDB(client);
}

From source file:com.netflix.hollow.example.consumer.infrastructure.DynamoDBAnnouncementWatcher.java

License:Apache License

public DynamoDBAnnouncementWatcher(AWSCredentials credentials, String tableName, String blobNamespace) {
    this.dynamoDB = new DynamoDB(new AmazonDynamoDBClient(credentials));
    this.tableName = tableName;
    this.blobNamespace = blobNamespace;
    this.latestVersion = readLatestVersion();
}

From source file:com.netflix.hollow.example.producer.infrastructure.DynamoDBAnnouncer.java

License:Apache License

public DynamoDBAnnouncer(AWSCredentials credentials, String tableName, String blobNamespace) {
    this.dynamoDB = new DynamoDB(new AmazonDynamoDBClient(credentials));
    this.tableName = tableName;
    this.blobNamespace = blobNamespace;
}