From f06815ec32c734b47ce2b520c49dbc626f870b9a Mon Sep 17 00:00:00 2001 From: Benni Rogge Date: Wed, 9 Oct 2024 09:41:49 +0200 Subject: [PATCH 1/2] feat: adding Kotlin examples for Working with Items https://github.com/aws-samples/aws-dynamodb-examples/issues/153 --- .../kotlin/data_plane/WorkingWithItems/.todo | 0 .../WorkingWithItems/build.gradle.kts | 16 +++++ .../src/main/kotlin/BatchGetItem.kt | 47 +++++++++++++++ .../src/main/kotlin/BatchWriteItem.kt | 58 +++++++++++++++++++ .../src/main/kotlin/DeleteItem.kt | 23 ++++++++ .../src/main/kotlin/GetItem.kt | 27 +++++++++ .../src/main/kotlin/PutItem.kt | 30 ++++++++++ .../src/main/kotlin/PutItemConditional.kt | 31 ++++++++++ .../src/main/kotlin/TransactGetItem.kt | 38 ++++++++++++ .../src/main/kotlin/TransactPutItem.kt | 41 +++++++++++++ .../src/main/kotlin/UpdateItem.kt | 35 +++++++++++ .../src/main/kotlin/UpdateItemConditional.kt | 38 ++++++++++++ 12 files changed, 384 insertions(+) delete mode 100644 examples/SDK/kotlin/data_plane/WorkingWithItems/.todo create mode 100644 examples/SDK/kotlin/data_plane/WorkingWithItems/build.gradle.kts create mode 100644 examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchGetItem.kt create mode 100644 examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchWriteItem.kt create mode 100644 examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/DeleteItem.kt create mode 100644 examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/GetItem.kt create mode 100644 examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/PutItem.kt create mode 100644 examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/PutItemConditional.kt create mode 100644 examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactGetItem.kt create mode 100644 examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactPutItem.kt create mode 100644 examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/UpdateItem.kt create mode 100644 examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/UpdateItemConditional.kt diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/.todo b/examples/SDK/kotlin/data_plane/WorkingWithItems/.todo deleted file mode 100644 index e69de29..0000000 diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/build.gradle.kts b/examples/SDK/kotlin/data_plane/WorkingWithItems/build.gradle.kts new file mode 100644 index 0000000..1994d37 --- /dev/null +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/build.gradle.kts @@ -0,0 +1,16 @@ +plugins { + kotlin("jvm") version "1.9.10" +} + +repositories { + mavenCentral() +} + +dependencies { + implementation(kotlin("stdlib")) + implementation("aws.sdk.kotlin:dynamodb:1.0.0") +} + +kotlin { + jvmToolchain(17) +} diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchGetItem.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchGetItem.kt new file mode 100644 index 0000000..ad0f4b1 --- /dev/null +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchGetItem.kt @@ -0,0 +1,47 @@ +import aws.sdk.kotlin.services.dynamodb.DynamoDbClient +import aws.sdk.kotlin.services.dynamodb.batchGetItem +import aws.sdk.kotlin.services.dynamodb.model.AttributeValue +import aws.sdk.kotlin.services.dynamodb.model.KeysAndAttributes + +suspend fun batchGetItem( + tableName: String, + keys: List>, +) { + DynamoDbClient { region = "us-west-2" }.use { ddb -> + val response = + ddb.batchGetItem { + this.requestItems = + mapOf( + tableName to + KeysAndAttributes { + this.keys = keys + }, + ) + } + + response.responses?.forEach { (tableName, items) -> + println("Items from table $tableName:") + items.forEach { item -> + println(item) + } + } + + response.unprocessedKeys?.let { unprocessedKeys -> + if (unprocessedKeys.isNotEmpty()) { + println("Unprocessed keys:") + println(unprocessedKeys) + } + } + } +} + +suspend fun main() { + val tableName = "YourTableName" + val keys = + listOf( + mapOf("PrimaryKey" to AttributeValue.S("YourPrimaryKeyValue1")), + mapOf("PrimaryKey" to AttributeValue.S("YourPrimaryKeyValue2")), + ) + + batchGetItem(tableName, keys) +} diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchWriteItem.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchWriteItem.kt new file mode 100644 index 0000000..4186eb7 --- /dev/null +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchWriteItem.kt @@ -0,0 +1,58 @@ +import aws.sdk.kotlin.services.dynamodb.DynamoDbClient +import aws.sdk.kotlin.services.dynamodb.batchWriteItem +import aws.sdk.kotlin.services.dynamodb.model.AttributeValue +import aws.sdk.kotlin.services.dynamodb.model.PutRequest +import aws.sdk.kotlin.services.dynamodb.model.WriteRequest + +suspend fun batchWriteItem( + tableName: String, + items: List>, +) { + DynamoDbClient { region = "us-west-2" }.use { ddb -> + + val putRequests = + items.map { item -> + val itemValues = item.mapValues { (_, value) -> AttributeValue.S(value) } + WriteRequest { + this.putRequest = + PutRequest { + this.item = itemValues + } + } + } + + val response = + ddb.batchWriteItem { + this.requestItems = + mapOf( + tableName to putRequests, + ) + } + + response.unprocessedItems?.let { unprocessedKeys -> + if (unprocessedKeys.isNotEmpty()) { + println("Unprocessed keys:") + println(unprocessedKeys) + } + } + } +} + +suspend fun main() { + val tableName = "YourTableName" + val items = + listOf( + mapOf( + "id" to "1234", + "name" to "John Doe", + "email" to "john.doe@example.com", + ), + mapOf( + "id" to "1235", + "name" to "Jane Doe", + "email" to "jane.doe@example.com", + ), + ) + + batchWriteItem(tableName, items) +} diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/DeleteItem.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/DeleteItem.kt new file mode 100644 index 0000000..1c249e8 --- /dev/null +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/DeleteItem.kt @@ -0,0 +1,23 @@ +import aws.sdk.kotlin.services.dynamodb.DynamoDbClient +import aws.sdk.kotlin.services.dynamodb.deleteItem +import aws.sdk.kotlin.services.dynamodb.model.AttributeValue + +suspend fun deleteItem( + tableName: String, + key: Map, +) { + DynamoDbClient { region = "us-west-2" }.use { ddb -> + ddb.deleteItem { + this.tableName = tableName + this.key = key + } + println("Deleted item with the given key.") + } +} + +suspend fun main() { + val tableName = "YourTableName" + val key = mapOf("PrimaryKey" to AttributeValue.S("YourPrimaryKeyValue")) + + deleteItem(tableName, key) +} diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/GetItem.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/GetItem.kt new file mode 100644 index 0000000..4001f21 --- /dev/null +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/GetItem.kt @@ -0,0 +1,27 @@ +import aws.sdk.kotlin.services.dynamodb.DynamoDbClient +import aws.sdk.kotlin.services.dynamodb.getItem +import aws.sdk.kotlin.services.dynamodb.model.AttributeValue + +suspend fun getItem( + tableName: String, + key: Map, +) { + DynamoDbClient { region = "us-west-2" }.use { ddb -> + val response = + ddb.getItem { + this.tableName = tableName + this.key = key + } + + response.item?.let { + println("Item: $it") + } ?: println("No item found with the given key.") + } +} + +suspend fun main() { + val tableName = "YourTableName" + val key = mapOf("PrimaryKey" to AttributeValue.S("YourPrimaryKeyValue")) + + getItem(tableName, key) +} diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/PutItem.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/PutItem.kt new file mode 100644 index 0000000..2e3be74 --- /dev/null +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/PutItem.kt @@ -0,0 +1,30 @@ +import aws.sdk.kotlin.services.dynamodb.DynamoDbClient +import aws.sdk.kotlin.services.dynamodb.model.AttributeValue +import aws.sdk.kotlin.services.dynamodb.putItem + +suspend fun putItem( + tableName: String, + item: Map, +) { + val itemValues = item.mapValues { (_, value) -> AttributeValue.S(value) } + + DynamoDbClient { region = "us-west-2" }.use { dynamoDb -> + dynamoDb.putItem { + this.tableName = tableName + this.item = itemValues + } + println("Item successfully added to table $tableName") + } +} + +suspend fun main() { + val tableName = "YourTableName" + val item = + mapOf( + "id" to "1234", + "name" to "John Doe", + "email" to "john.doe@example.com", + ) + + putItem(tableName, item) +} diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/PutItemConditional.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/PutItemConditional.kt new file mode 100644 index 0000000..38becbd --- /dev/null +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/PutItemConditional.kt @@ -0,0 +1,31 @@ +import aws.sdk.kotlin.services.dynamodb.DynamoDbClient +import aws.sdk.kotlin.services.dynamodb.model.AttributeValue +import aws.sdk.kotlin.services.dynamodb.putItem + +suspend fun putItemConditional( + tableName: String, + item: Map, +) { + val itemValues = item.mapValues { (_, value) -> AttributeValue.S(value) } + + DynamoDbClient { region = "us-west-2" }.use { dynamoDb -> + dynamoDb.putItem { + this.tableName = tableName + this.item = itemValues + this.conditionExpression = "attribute_not_exists(id)" + } + println("Item successfully added to table $tableName") + } +} + +suspend fun main() { + val tableName = "YourTableName" + val item = + mapOf( + "id" to "1234", + "name" to "John Doe", + "email" to "john.doe@example.com", + ) + + putItemConditional(tableName, item) +} diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactGetItem.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactGetItem.kt new file mode 100644 index 0000000..c26ca07 --- /dev/null +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactGetItem.kt @@ -0,0 +1,38 @@ +import aws.sdk.kotlin.services.dynamodb.DynamoDbClient +import aws.sdk.kotlin.services.dynamodb.model.AttributeValue +import aws.sdk.kotlin.services.dynamodb.model.Get +import aws.sdk.kotlin.services.dynamodb.model.TransactGetItem +import aws.sdk.kotlin.services.dynamodb.transactGetItems + +suspend fun transactGetItem(requestsPerTable: Map>) { + DynamoDbClient { region = "us-west-2" }.use { ddb -> + val response = + ddb.transactGetItems { + this.transactItems = + requestsPerTable.map { (table, requests) -> + TransactGetItem { + this.get = + Get { + this.tableName = table + this.key = requests + } + } + } + } + + response.responses?.forEach { + println("found item $it") + } + } +} + +suspend fun main() { + val tableName1 = "YourTableName" + val tableName2 = "YourTableName2" + val requests = + mapOf( + tableName1 to mapOf("PrimaryKey1" to AttributeValue.S("YourPrimaryKeyValue1")), + tableName2 to mapOf("PrimaryKey2" to AttributeValue.S("YourPrimaryKeyValue2")), + ) + transactGetItem(requests) +} diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactPutItem.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactPutItem.kt new file mode 100644 index 0000000..ba5db4b --- /dev/null +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactPutItem.kt @@ -0,0 +1,41 @@ +import aws.sdk.kotlin.services.dynamodb.DynamoDbClient +import aws.sdk.kotlin.services.dynamodb.model.AttributeValue +import aws.sdk.kotlin.services.dynamodb.model.TransactWriteItem +import aws.sdk.kotlin.services.dynamodb.transactWriteItems + +suspend fun transactPutItem(requestsPerTable: Map>) { + DynamoDbClient { region = "us-west-2" }.use { ddb -> + ddb.transactWriteItems { + this.transactItems = + requestsPerTable.map { (table, requests) -> + TransactWriteItem { + this.put { + this.tableName = table + this.item = requests.mapValues { (_, value) -> AttributeValue.S(value) } + } + } + } + } + } +} + +suspend fun main() { + val tableName1 = "YourTableName" + val tableName2 = "YourTableName2" + + val requests = + mapOf( + tableName1 to + mapOf( + "id" to "1234", + "name" to "John Doe", + "email" to "john.doe@example.com", + ), + tableName2 to + mapOf( + "order" to "5678", + "customer" to "1234", + ), + ) + transactPutItem(requests) +} diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/UpdateItem.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/UpdateItem.kt new file mode 100644 index 0000000..d0f0a17 --- /dev/null +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/UpdateItem.kt @@ -0,0 +1,35 @@ +import aws.sdk.kotlin.services.dynamodb.DynamoDbClient +import aws.sdk.kotlin.services.dynamodb.model.AttributeAction +import aws.sdk.kotlin.services.dynamodb.model.AttributeValue +import aws.sdk.kotlin.services.dynamodb.model.AttributeValueUpdate +import aws.sdk.kotlin.services.dynamodb.model.UpdateItemRequest +import aws.sdk.kotlin.services.dynamodb.updateItem + +suspend fun updateItem( + tableName: String, + id: String, + newEmail: String, +) { + DynamoDbClient { region = "us-west-2" }.use { dynamoDb -> + dynamoDb.updateItem { + UpdateItemRequest { + this.tableName = tableName + this.key = mapOf("PrimaryKey" to AttributeValue.S(id)) + this.attributeUpdates = + mapOf( + "email" to + AttributeValueUpdate { + this.action = AttributeAction.Put + this.value = AttributeValue.S(newEmail) + }, + ) + } + } + println("Item updated") + } +} + +suspend fun main() { + val tableName = "YourTableName" + updateItem(tableName, id = "1234", newEmail = "new@example.com") +} diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/UpdateItemConditional.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/UpdateItemConditional.kt new file mode 100644 index 0000000..c7cc042 --- /dev/null +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/UpdateItemConditional.kt @@ -0,0 +1,38 @@ +import aws.sdk.kotlin.services.dynamodb.DynamoDbClient +import aws.sdk.kotlin.services.dynamodb.model.AttributeValue +import aws.sdk.kotlin.services.dynamodb.model.UpdateItemRequest +import aws.sdk.kotlin.services.dynamodb.updateItem + +suspend fun updateItemConditional( + tableName: String, + id: String, + newPrice: Long, +) { + DynamoDbClient { region = "us-west-2" }.use { dynamoDb -> + val updateExpression = "SET #attr = :val" + val conditionExpression = "#attr < :threshold" + val expressionAttributeNames = mapOf("#attr" to "price") + val expressionAttributeValues = + mapOf( + ":val" to AttributeValue.N(newPrice.toString()), + ":threshold" to AttributeValue.N("5000"), + ) + + dynamoDb.updateItem { + UpdateItemRequest { + this.tableName = tableName + this.key = mapOf("PrimaryKey" to AttributeValue.S(id)) + this.conditionExpression = conditionExpression + this.updateExpression = updateExpression + this.expressionAttributeNames = expressionAttributeNames + this.expressionAttributeValues = expressionAttributeValues + } + } + println("Item updated") + } +} + +suspend fun main() { + val tableName = "YourTableName" + updateItemConditional(tableName, id = "product_id_1", newPrice = 500) +} From ccec0c536a3bfd47391099f57030f5b4760c23ee Mon Sep 17 00:00:00 2001 From: Benni Rogge Date: Fri, 11 Apr 2025 07:10:56 -0700 Subject: [PATCH 2/2] align attribute name and values --- .../WorkingWithItems/src/main/kotlin/BatchGetItem.kt | 4 ++-- .../data_plane/WorkingWithItems/src/main/kotlin/DeleteItem.kt | 2 +- .../data_plane/WorkingWithItems/src/main/kotlin/GetItem.kt | 2 +- .../WorkingWithItems/src/main/kotlin/TransactGetItem.kt | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchGetItem.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchGetItem.kt index ad0f4b1..1eca600 100644 --- a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchGetItem.kt +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchGetItem.kt @@ -39,8 +39,8 @@ suspend fun main() { val tableName = "YourTableName" val keys = listOf( - mapOf("PrimaryKey" to AttributeValue.S("YourPrimaryKeyValue1")), - mapOf("PrimaryKey" to AttributeValue.S("YourPrimaryKeyValue2")), + mapOf("id" to AttributeValue.S("12345")), + mapOf("id" to AttributeValue.S("1234")), ) batchGetItem(tableName, keys) diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/DeleteItem.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/DeleteItem.kt index 1c249e8..65fe404 100644 --- a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/DeleteItem.kt +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/DeleteItem.kt @@ -17,7 +17,7 @@ suspend fun deleteItem( suspend fun main() { val tableName = "YourTableName" - val key = mapOf("PrimaryKey" to AttributeValue.S("YourPrimaryKeyValue")) + val key = mapOf("id" to AttributeValue.S("1234")) deleteItem(tableName, key) } diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/GetItem.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/GetItem.kt index 4001f21..73b28b1 100644 --- a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/GetItem.kt +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/GetItem.kt @@ -21,7 +21,7 @@ suspend fun getItem( suspend fun main() { val tableName = "YourTableName" - val key = mapOf("PrimaryKey" to AttributeValue.S("YourPrimaryKeyValue")) + val key = mapOf("id" to AttributeValue.S("1234")) getItem(tableName, key) } diff --git a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactGetItem.kt b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactGetItem.kt index c26ca07..0d69c2b 100644 --- a/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactGetItem.kt +++ b/examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactGetItem.kt @@ -31,8 +31,8 @@ suspend fun main() { val tableName2 = "YourTableName2" val requests = mapOf( - tableName1 to mapOf("PrimaryKey1" to AttributeValue.S("YourPrimaryKeyValue1")), - tableName2 to mapOf("PrimaryKey2" to AttributeValue.S("YourPrimaryKeyValue2")), + tableName1 to mapOf("id" to AttributeValue.S("12345")), + tableName2 to mapOf("id" to AttributeValue.S("1234")), ) transactGetItem(requests) }