Skip to content

Commit f06815e

Browse files
plastic-karmaBenni Rogge
authored and
Benni Rogge
committed
feat: adding Kotlin examples for Working with Items
#153
1 parent a853f95 commit f06815e

File tree

12 files changed

+384
-0
lines changed

12 files changed

+384
-0
lines changed

examples/SDK/kotlin/data_plane/WorkingWithItems/.todo

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plugins {
2+
kotlin("jvm") version "1.9.10"
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
implementation(kotlin("stdlib"))
11+
implementation("aws.sdk.kotlin:dynamodb:1.0.0")
12+
}
13+
14+
kotlin {
15+
jvmToolchain(17)
16+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
2+
import aws.sdk.kotlin.services.dynamodb.batchGetItem
3+
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
4+
import aws.sdk.kotlin.services.dynamodb.model.KeysAndAttributes
5+
6+
suspend fun batchGetItem(
7+
tableName: String,
8+
keys: List<Map<String, AttributeValue.S>>,
9+
) {
10+
DynamoDbClient { region = "us-west-2" }.use { ddb ->
11+
val response =
12+
ddb.batchGetItem {
13+
this.requestItems =
14+
mapOf(
15+
tableName to
16+
KeysAndAttributes {
17+
this.keys = keys
18+
},
19+
)
20+
}
21+
22+
response.responses?.forEach { (tableName, items) ->
23+
println("Items from table $tableName:")
24+
items.forEach { item ->
25+
println(item)
26+
}
27+
}
28+
29+
response.unprocessedKeys?.let { unprocessedKeys ->
30+
if (unprocessedKeys.isNotEmpty()) {
31+
println("Unprocessed keys:")
32+
println(unprocessedKeys)
33+
}
34+
}
35+
}
36+
}
37+
38+
suspend fun main() {
39+
val tableName = "YourTableName"
40+
val keys =
41+
listOf(
42+
mapOf("PrimaryKey" to AttributeValue.S("YourPrimaryKeyValue1")),
43+
mapOf("PrimaryKey" to AttributeValue.S("YourPrimaryKeyValue2")),
44+
)
45+
46+
batchGetItem(tableName, keys)
47+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
2+
import aws.sdk.kotlin.services.dynamodb.batchWriteItem
3+
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
4+
import aws.sdk.kotlin.services.dynamodb.model.PutRequest
5+
import aws.sdk.kotlin.services.dynamodb.model.WriteRequest
6+
7+
suspend fun batchWriteItem(
8+
tableName: String,
9+
items: List<Map<String, String>>,
10+
) {
11+
DynamoDbClient { region = "us-west-2" }.use { ddb ->
12+
13+
val putRequests =
14+
items.map { item ->
15+
val itemValues = item.mapValues { (_, value) -> AttributeValue.S(value) }
16+
WriteRequest {
17+
this.putRequest =
18+
PutRequest {
19+
this.item = itemValues
20+
}
21+
}
22+
}
23+
24+
val response =
25+
ddb.batchWriteItem {
26+
this.requestItems =
27+
mapOf(
28+
tableName to putRequests,
29+
)
30+
}
31+
32+
response.unprocessedItems?.let { unprocessedKeys ->
33+
if (unprocessedKeys.isNotEmpty()) {
34+
println("Unprocessed keys:")
35+
println(unprocessedKeys)
36+
}
37+
}
38+
}
39+
}
40+
41+
suspend fun main() {
42+
val tableName = "YourTableName"
43+
val items =
44+
listOf(
45+
mapOf(
46+
"id" to "1234",
47+
"name" to "John Doe",
48+
"email" to "[email protected]",
49+
),
50+
mapOf(
51+
"id" to "1235",
52+
"name" to "Jane Doe",
53+
"email" to "[email protected]",
54+
),
55+
)
56+
57+
batchWriteItem(tableName, items)
58+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
2+
import aws.sdk.kotlin.services.dynamodb.deleteItem
3+
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
4+
5+
suspend fun deleteItem(
6+
tableName: String,
7+
key: Map<String, AttributeValue>,
8+
) {
9+
DynamoDbClient { region = "us-west-2" }.use { ddb ->
10+
ddb.deleteItem {
11+
this.tableName = tableName
12+
this.key = key
13+
}
14+
println("Deleted item with the given key.")
15+
}
16+
}
17+
18+
suspend fun main() {
19+
val tableName = "YourTableName"
20+
val key = mapOf("PrimaryKey" to AttributeValue.S("YourPrimaryKeyValue"))
21+
22+
deleteItem(tableName, key)
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
2+
import aws.sdk.kotlin.services.dynamodb.getItem
3+
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
4+
5+
suspend fun getItem(
6+
tableName: String,
7+
key: Map<String, AttributeValue>,
8+
) {
9+
DynamoDbClient { region = "us-west-2" }.use { ddb ->
10+
val response =
11+
ddb.getItem {
12+
this.tableName = tableName
13+
this.key = key
14+
}
15+
16+
response.item?.let {
17+
println("Item: $it")
18+
} ?: println("No item found with the given key.")
19+
}
20+
}
21+
22+
suspend fun main() {
23+
val tableName = "YourTableName"
24+
val key = mapOf("PrimaryKey" to AttributeValue.S("YourPrimaryKeyValue"))
25+
26+
getItem(tableName, key)
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
2+
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
3+
import aws.sdk.kotlin.services.dynamodb.putItem
4+
5+
suspend fun putItem(
6+
tableName: String,
7+
item: Map<String, String>,
8+
) {
9+
val itemValues = item.mapValues { (_, value) -> AttributeValue.S(value) }
10+
11+
DynamoDbClient { region = "us-west-2" }.use { dynamoDb ->
12+
dynamoDb.putItem {
13+
this.tableName = tableName
14+
this.item = itemValues
15+
}
16+
println("Item successfully added to table $tableName")
17+
}
18+
}
19+
20+
suspend fun main() {
21+
val tableName = "YourTableName"
22+
val item =
23+
mapOf(
24+
"id" to "1234",
25+
"name" to "John Doe",
26+
"email" to "[email protected]",
27+
)
28+
29+
putItem(tableName, item)
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
2+
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
3+
import aws.sdk.kotlin.services.dynamodb.putItem
4+
5+
suspend fun putItemConditional(
6+
tableName: String,
7+
item: Map<String, String>,
8+
) {
9+
val itemValues = item.mapValues { (_, value) -> AttributeValue.S(value) }
10+
11+
DynamoDbClient { region = "us-west-2" }.use { dynamoDb ->
12+
dynamoDb.putItem {
13+
this.tableName = tableName
14+
this.item = itemValues
15+
this.conditionExpression = "attribute_not_exists(id)"
16+
}
17+
println("Item successfully added to table $tableName")
18+
}
19+
}
20+
21+
suspend fun main() {
22+
val tableName = "YourTableName"
23+
val item =
24+
mapOf(
25+
"id" to "1234",
26+
"name" to "John Doe",
27+
"email" to "[email protected]",
28+
)
29+
30+
putItemConditional(tableName, item)
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
2+
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
3+
import aws.sdk.kotlin.services.dynamodb.model.Get
4+
import aws.sdk.kotlin.services.dynamodb.model.TransactGetItem
5+
import aws.sdk.kotlin.services.dynamodb.transactGetItems
6+
7+
suspend fun transactGetItem(requestsPerTable: Map<String, Map<String, AttributeValue.S>>) {
8+
DynamoDbClient { region = "us-west-2" }.use { ddb ->
9+
val response =
10+
ddb.transactGetItems {
11+
this.transactItems =
12+
requestsPerTable.map { (table, requests) ->
13+
TransactGetItem {
14+
this.get =
15+
Get {
16+
this.tableName = table
17+
this.key = requests
18+
}
19+
}
20+
}
21+
}
22+
23+
response.responses?.forEach {
24+
println("found item $it")
25+
}
26+
}
27+
}
28+
29+
suspend fun main() {
30+
val tableName1 = "YourTableName"
31+
val tableName2 = "YourTableName2"
32+
val requests =
33+
mapOf(
34+
tableName1 to mapOf("PrimaryKey1" to AttributeValue.S("YourPrimaryKeyValue1")),
35+
tableName2 to mapOf("PrimaryKey2" to AttributeValue.S("YourPrimaryKeyValue2")),
36+
)
37+
transactGetItem(requests)
38+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
2+
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
3+
import aws.sdk.kotlin.services.dynamodb.model.TransactWriteItem
4+
import aws.sdk.kotlin.services.dynamodb.transactWriteItems
5+
6+
suspend fun transactPutItem(requestsPerTable: Map<String, Map<String, String>>) {
7+
DynamoDbClient { region = "us-west-2" }.use { ddb ->
8+
ddb.transactWriteItems {
9+
this.transactItems =
10+
requestsPerTable.map { (table, requests) ->
11+
TransactWriteItem {
12+
this.put {
13+
this.tableName = table
14+
this.item = requests.mapValues { (_, value) -> AttributeValue.S(value) }
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}
21+
22+
suspend fun main() {
23+
val tableName1 = "YourTableName"
24+
val tableName2 = "YourTableName2"
25+
26+
val requests =
27+
mapOf(
28+
tableName1 to
29+
mapOf(
30+
"id" to "1234",
31+
"name" to "John Doe",
32+
"email" to "[email protected]",
33+
),
34+
tableName2 to
35+
mapOf(
36+
"order" to "5678",
37+
"customer" to "1234",
38+
),
39+
)
40+
transactPutItem(requests)
41+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
2+
import aws.sdk.kotlin.services.dynamodb.model.AttributeAction
3+
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
4+
import aws.sdk.kotlin.services.dynamodb.model.AttributeValueUpdate
5+
import aws.sdk.kotlin.services.dynamodb.model.UpdateItemRequest
6+
import aws.sdk.kotlin.services.dynamodb.updateItem
7+
8+
suspend fun updateItem(
9+
tableName: String,
10+
id: String,
11+
newEmail: String,
12+
) {
13+
DynamoDbClient { region = "us-west-2" }.use { dynamoDb ->
14+
dynamoDb.updateItem {
15+
UpdateItemRequest {
16+
this.tableName = tableName
17+
this.key = mapOf("PrimaryKey" to AttributeValue.S(id))
18+
this.attributeUpdates =
19+
mapOf(
20+
"email" to
21+
AttributeValueUpdate {
22+
this.action = AttributeAction.Put
23+
this.value = AttributeValue.S(newEmail)
24+
},
25+
)
26+
}
27+
}
28+
println("Item updated")
29+
}
30+
}
31+
32+
suspend fun main() {
33+
val tableName = "YourTableName"
34+
updateItem(tableName, id = "1234", newEmail = "[email protected]")
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
2+
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
3+
import aws.sdk.kotlin.services.dynamodb.model.UpdateItemRequest
4+
import aws.sdk.kotlin.services.dynamodb.updateItem
5+
6+
suspend fun updateItemConditional(
7+
tableName: String,
8+
id: String,
9+
newPrice: Long,
10+
) {
11+
DynamoDbClient { region = "us-west-2" }.use { dynamoDb ->
12+
val updateExpression = "SET #attr = :val"
13+
val conditionExpression = "#attr < :threshold"
14+
val expressionAttributeNames = mapOf("#attr" to "price")
15+
val expressionAttributeValues =
16+
mapOf(
17+
":val" to AttributeValue.N(newPrice.toString()),
18+
":threshold" to AttributeValue.N("5000"),
19+
)
20+
21+
dynamoDb.updateItem {
22+
UpdateItemRequest {
23+
this.tableName = tableName
24+
this.key = mapOf("PrimaryKey" to AttributeValue.S(id))
25+
this.conditionExpression = conditionExpression
26+
this.updateExpression = updateExpression
27+
this.expressionAttributeNames = expressionAttributeNames
28+
this.expressionAttributeValues = expressionAttributeValues
29+
}
30+
}
31+
println("Item updated")
32+
}
33+
}
34+
35+
suspend fun main() {
36+
val tableName = "YourTableName"
37+
updateItemConditional(tableName, id = "product_id_1", newPrice = 500)
38+
}

0 commit comments

Comments
 (0)