Search in sources :

Example 91 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project aws-doc-sdk-examples by awsdocs.

the class LowLevelBatchGet method retrieveMultipleItemsBatchGet.

private static void retrieveMultipleItemsBatchGet() {
    try {
        Map<String, KeysAndAttributes> requestItems = new HashMap<String, KeysAndAttributes>();
        List<Map<String, AttributeValue>> tableKeys = new ArrayList<Map<String, AttributeValue>>();
        Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
        key.put("Name", new AttributeValue().withS("Amazon S3"));
        tableKeys.add(key);
        key = new HashMap<String, AttributeValue>();
        key.put("Name", new AttributeValue().withS("Amazon DynamoDB"));
        tableKeys.add(key);
        requestItems.put(table1Name, new KeysAndAttributes().withKeys(tableKeys));
        tableKeys = new ArrayList<Map<String, AttributeValue>>();
        key = new HashMap<String, AttributeValue>();
        key.put("ForumName", new AttributeValue().withS("Amazon DynamoDB"));
        key.put("Subject", new AttributeValue().withS("DynamoDB Thread 1"));
        tableKeys.add(key);
        key = new HashMap<String, AttributeValue>();
        key.put("ForumName", new AttributeValue().withS("Amazon DynamoDB"));
        key.put("Subject", new AttributeValue().withS("DynamoDB Thread 2"));
        tableKeys.add(key);
        key = new HashMap<String, AttributeValue>();
        key.put("ForumName", new AttributeValue().withS("Amazon S3"));
        key.put("Subject", new AttributeValue().withS("S3 Thread 1"));
        tableKeys.add(key);
        requestItems.put(table2Name, new KeysAndAttributes().withKeys(tableKeys));
        BatchGetItemResult result;
        BatchGetItemRequest batchGetItemRequest = new BatchGetItemRequest();
        do {
            System.out.println("Making the request.");
            batchGetItemRequest.withRequestItems(requestItems);
            result = client.batchGetItem(batchGetItemRequest);
            List<Map<String, AttributeValue>> table1Results = result.getResponses().get(table1Name);
            if (table1Results != null) {
                System.out.println("Items in table " + table1Name);
                for (Map<String, AttributeValue> item : table1Results) {
                    printItem(item);
                }
            }
            List<Map<String, AttributeValue>> table2Results = result.getResponses().get(table2Name);
            if (table2Results != null) {
                System.out.println("\nItems in table " + table2Name);
                for (Map<String, AttributeValue> item : table2Results) {
                    printItem(item);
                }
            }
            // throughput or reach the limit on response size.
            for (Map.Entry<String, KeysAndAttributes> pair : result.getUnprocessedKeys().entrySet()) {
                System.out.println("Unprocessed key pair: " + pair.getKey() + ", " + pair.getValue());
            }
            requestItems = result.getUnprocessedKeys();
        } while (result.getUnprocessedKeys().size() > 0);
    } catch (AmazonServiceException ase) {
        System.err.println("Failed to retrieve items.");
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KeysAndAttributes(com.amazonaws.services.dynamodbv2.model.KeysAndAttributes) BatchGetItemRequest(com.amazonaws.services.dynamodbv2.model.BatchGetItemRequest) BatchGetItemResult(com.amazonaws.services.dynamodbv2.model.BatchGetItemResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 92 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project aws-doc-sdk-examples by awsdocs.

the class MoviesItemOps04 method main.

public static void main(String[] args) throws Exception {
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")).build();
    DynamoDB dynamoDB = new DynamoDB(client);
    Table table = dynamoDB.getTable("Movies");
    int year = 2015;
    String title = "The Big New Movie";
    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("year", year, "title", title).withUpdateExpression("set info.rating = info.rating + :val").withValueMap(new ValueMap().withNumber(":val", 1)).withReturnValues(ReturnValue.UPDATED_NEW);
    try {
        System.out.println("Incrementing an atomic counter...");
        UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
        System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty());
    } catch (Exception e) {
        System.err.println("Unable to update item: " + year + " " + title);
        System.err.println(e.getMessage());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) UpdateItemOutcome(com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 93 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBLocalFixture method main.

public static void main(String[] args) throws Exception {
    AmazonDynamoDB dynamodb = null;
    try {
        // Create an in-memory and in-process instance of DynamoDB Local
        // that skips HTTP
        dynamodb = DynamoDBEmbedded.create();
        // use the DynamoDB API with DynamoDBEmbedded
        listTables(dynamodb.listTables(), "DynamoDB Embedded");
    } finally {
        // Shutdown the thread pools in DynamoDB Local / Embedded
        if (dynamodb != null) {
            dynamodb.shutdown();
        }
    }
    // Create an in-memory and in-process instance of DynamoDB Local that
    // runs over HTTP
    final String[] localArgs = { "-inMemory" };
    DynamoDBProxyServer server = null;
    try {
        server = ServerRunner.createServerFromCommandLineArgs(localArgs);
        server.start();
        dynamodb = new AmazonDynamoDBClient();
        dynamodb.setEndpoint("http://localhost:8000");
        // use the DynamoDB API over HTTP
        listTables(dynamodb.listTables(), "DynamoDB Local over HTTP");
    } finally {
        // Stop the DynamoDB Local endpoint
        if (server != null) {
            server.stop();
        }
    }
}
Also used : AmazonDynamoDBClient(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDBProxyServer(com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer)

Example 94 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project beam by apache.

the class DynamoDBIOIT method runRead.

/**
 * Read test dataset from DynamoDB.
 */
private void runRead() {
    int rows = env.options().getNumberOfRows();
    PCollection<Map<String, AttributeValue>> records = pipelineRead.apply("Read from DynamoDB", DynamoDBIO.read().withAwsClientsProvider(clientProvider()).withScanRequestFn(in -> buildScanRequest()).items()).apply("Flatten result", Flatten.iterables());
    PAssert.thatSingleton(records.apply("Count All", Count.globally())).isEqualTo((long) rows);
    PCollection<String> consolidatedHashcode = records.apply(MapElements.into(strings()).via(record -> record.get(COL_NAME).getS())).apply("Hash records", Combine.globally(new HashingFn()).withoutDefaults());
    PAssert.that(consolidatedHashcode).containsInAnyOrder(getExpectedHashForRowCount(rows));
    pipelineRead.run().waitUntilFinish();
}
Also used : Count(org.apache.beam.sdk.transforms.Count) KV(org.apache.beam.sdk.values.KV) PutRequest(com.amazonaws.services.dynamodbv2.model.PutRequest) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) Default(org.apache.beam.sdk.options.Default) Combine(org.apache.beam.sdk.transforms.Combine) KeyType(com.amazonaws.services.dynamodbv2.model.KeyType) RunWith(org.junit.runner.RunWith) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) WriteRequest(com.amazonaws.services.dynamodbv2.model.WriteRequest) Regions(com.amazonaws.regions.Regions) Description(org.apache.beam.sdk.options.Description) TableStatus(com.amazonaws.services.dynamodbv2.model.TableStatus) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Map(java.util.Map) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) TypeDescriptors.strings(org.apache.beam.sdk.values.TypeDescriptors.strings) ITEnvironment(org.apache.beam.sdk.io.aws.ITEnvironment) TestRow.getExpectedHashForRowCount(org.apache.beam.sdk.io.common.TestRow.getExpectedHashForRowCount) ClassRule(org.junit.ClassRule) AWSCredentials(com.amazonaws.auth.AWSCredentials) Flatten(org.apache.beam.sdk.transforms.Flatten) MapElements(org.apache.beam.sdk.transforms.MapElements) HashingFn(org.apache.beam.sdk.io.common.HashingFn) DeterministicallyConstructTestRowFn(org.apache.beam.sdk.io.common.TestRow.DeterministicallyConstructTestRowFn) PAssert(org.apache.beam.sdk.testing.PAssert) TestRow(org.apache.beam.sdk.io.common.TestRow) ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) AmazonDynamoDBClientBuilder(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder) GenerateSequence(org.apache.beam.sdk.io.GenerateSequence) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) PCollection(org.apache.beam.sdk.values.PCollection) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) ScalarAttributeType(com.amazonaws.services.dynamodbv2.model.ScalarAttributeType) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) Rule(org.junit.Rule) ExternalResource(org.junit.rules.ExternalResource) ParDo(org.apache.beam.sdk.transforms.ParDo) DYNAMODB(org.testcontainers.containers.localstack.LocalStackContainer.Service.DYNAMODB) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Map(java.util.Map) HashingFn(org.apache.beam.sdk.io.common.HashingFn)

Example 95 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project athenz by yahoo.

the class DynamoDBSSHRecordStoreConnectionTest method testDeleteSSHRecord.

@Test
public void testDeleteSSHRecord() {
    DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey("primaryKey", "cn:1234");
    Mockito.doReturn(deleteOutcome).when(table).deleteItem(deleteItemSpec);
    DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);
    boolean requestSuccess = dbConn.deleteSSHCertRecord("12345", "cn");
    assertTrue(requestSuccess);
    dbConn.close();
}
Also used : DeleteItemSpec(com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec) Test(org.testng.annotations.Test)

Aggregations

DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)38 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)32 Table (com.amazonaws.services.dynamodbv2.document.Table)25 Item (com.amazonaws.services.dynamodbv2.document.Item)19 Test (org.junit.Test)18 AmazonServiceException (com.amazonaws.AmazonServiceException)16 HashMap (java.util.HashMap)15 TestRunner (org.apache.nifi.util.TestRunner)15 AmazonClientException (com.amazonaws.AmazonClientException)14 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)14 MockFlowFile (org.apache.nifi.util.MockFlowFile)14 ArrayList (java.util.ArrayList)11 Map (java.util.Map)11 TableWriteItems (com.amazonaws.services.dynamodbv2.document.TableWriteItems)10 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)10 IOException (java.io.IOException)10 TableKeysAndAttributes (com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes)9 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)9 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)9 List (java.util.List)9