Search in sources :

Example 16 with AttributeValue

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

the class DeleteItem method deleteDymamoDBItem.

// snippet-start:[dynamodb.java2.delete_item.main]
public static void deleteDymamoDBItem(DynamoDbClient ddb, String tableName, String key, String keyVal) {
    HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
    keyToGet.put(key, AttributeValue.builder().s(keyVal).build());
    DeleteItemRequest deleteReq = DeleteItemRequest.builder().tableName(tableName).key(keyToGet).build();
    try {
        ddb.deleteItem(deleteReq);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

Example 17 with AttributeValue

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

the class GetItem method getDynamoDBItem.

// snippet-start:[dynamodb.java2.get_item.main]
public static void getDynamoDBItem(DynamoDbClient ddb, String tableName, String key, String keyVal) {
    HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
    keyToGet.put(key, AttributeValue.builder().s(keyVal).build());
    GetItemRequest request = GetItemRequest.builder().key(keyToGet).tableName(tableName).build();
    try {
        Map<String, AttributeValue> returnedItem = ddb.getItem(request).item();
        if (returnedItem != null) {
            Set<String> keys = returnedItem.keySet();
            System.out.println("Amazon DynamoDB table attributes: \n");
            for (String key1 : keys) {
                System.out.format("%s: %s\n", key1, returnedItem.get(key1).toString());
            }
        } else {
            System.out.format("No item found with the key %s!\n", key);
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest)

Example 18 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue 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().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).s())).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) Default(org.apache.beam.sdk.options.Default) Combine(org.apache.beam.sdk.transforms.Combine) RunWith(org.junit.runner.RunWith) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Description(org.apache.beam.sdk.options.Description) ProvisionedThroughput(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput) Map(java.util.Map) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) TypeDescriptors.strings(org.apache.beam.sdk.values.TypeDescriptors.strings) WriteRequest(software.amazon.awssdk.services.dynamodb.model.WriteRequest) TestRow.getExpectedHashForRowCount(org.apache.beam.sdk.io.common.TestRow.getExpectedHashForRowCount) PutRequest(software.amazon.awssdk.services.dynamodb.model.PutRequest) ClassRule(org.junit.ClassRule) ScalarAttributeType(software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType) Flatten(org.apache.beam.sdk.transforms.Flatten) MapElements(org.apache.beam.sdk.transforms.MapElements) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) HashingFn(org.apache.beam.sdk.io.common.HashingFn) DeterministicallyConstructTestRowFn(org.apache.beam.sdk.io.common.TestRow.DeterministicallyConstructTestRowFn) TableStatus(software.amazon.awssdk.services.dynamodb.model.TableStatus) PAssert(org.apache.beam.sdk.testing.PAssert) TestRow(org.apache.beam.sdk.io.common.TestRow) KeyType(software.amazon.awssdk.services.dynamodb.model.KeyType) ITEnvironment(org.apache.beam.sdk.io.aws2.ITEnvironment) GenerateSequence(org.apache.beam.sdk.io.GenerateSequence) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) PCollection(org.apache.beam.sdk.values.PCollection) Rule(org.junit.Rule) ExternalResource(org.junit.rules.ExternalResource) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement) ParDo(org.apache.beam.sdk.transforms.ParDo) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) 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 19 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project beam by apache.

the class AttributeValueCoderTest method shouldPassForOneListOfNumber.

@Test
public void shouldPassForOneListOfNumber() throws IOException {
    AttributeValue expected = AttributeValue.builder().ns(ImmutableList.of("123", "456")).build();
    AttributeValueCoder coder = AttributeValueCoder.of();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    coder.encode(expected, output);
    ByteArrayInputStream in = new ByteArrayInputStream(output.toByteArray());
    AttributeValue actual = coder.decode(in);
    Assert.assertEquals(expected, actual);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 20 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project beam by apache.

the class AttributeValueCoderTest method shouldPassForMapType.

@Test
public void shouldPassForMapType() throws IOException {
    Map<String, AttributeValue> attrMap = new HashMap<>();
    attrMap.put("innerMapAttr1", AttributeValue.builder().s("innerMapValue1").build());
    attrMap.put("innerMapAttr2", AttributeValue.builder().b(SdkBytes.fromByteArray("8976234".getBytes(StandardCharsets.UTF_8))).build());
    AttributeValue expected = AttributeValue.builder().m(attrMap).build();
    AttributeValueCoder coder = AttributeValueCoder.of();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    coder.encode(expected, output);
    ByteArrayInputStream in = new ByteArrayInputStream(output.toByteArray());
    AttributeValue actual = coder.decode(in);
    Assert.assertEquals(expected, actual);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)31 HashMap (java.util.HashMap)14 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)13 Test (org.junit.Test)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Map (java.util.Map)6 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)5 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)5 ScanRequest (software.amazon.awssdk.services.dynamodb.model.ScanRequest)5 Collectors (java.util.stream.Collectors)4 PutItemRequest (software.amazon.awssdk.services.dynamodb.model.PutItemRequest)4 List (java.util.List)3 Slf4j (lombok.extern.slf4j.Slf4j)3 lombok.val (lombok.val)3 AttributeDefinition (software.amazon.awssdk.services.dynamodb.model.AttributeDefinition)3 KeySchemaElement (software.amazon.awssdk.services.dynamodb.model.KeySchemaElement)3 ScanResponse (software.amazon.awssdk.services.dynamodb.model.ScanResponse)3 Set (java.util.Set)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2