-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adding Kotlin examples for Working with Items #154
Open
plastic-karma
wants to merge
1
commit into
aws-samples:master
Choose a base branch
from
plastic-karma:kotlin_153
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
16 changes: 16 additions & 0 deletions
16
examples/SDK/kotlin/data_plane/WorkingWithItems/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
47 changes: 47 additions & 0 deletions
47
examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchGetItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Map<String, AttributeValue.S>>, | ||
) { | ||
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) | ||
} |
58 changes: 58 additions & 0 deletions
58
examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/BatchWriteItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Map<String, String>>, | ||
) { | ||
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 "[email protected]", | ||
), | ||
mapOf( | ||
"id" to "1235", | ||
"name" to "Jane Doe", | ||
"email" to "[email protected]", | ||
), | ||
) | ||
|
||
batchWriteItem(tableName, items) | ||
} |
23 changes: 23 additions & 0 deletions
23
examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/DeleteItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, AttributeValue>, | ||
) { | ||
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")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment than the |
||
|
||
deleteItem(tableName, key) | ||
} |
27 changes: 27 additions & 0 deletions
27
examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/GetItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, AttributeValue>, | ||
) { | ||
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) | ||
} |
30 changes: 30 additions & 0 deletions
30
examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/PutItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String>, | ||
) { | ||
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 "[email protected]", | ||
) | ||
|
||
putItem(tableName, item) | ||
} |
31 changes: 31 additions & 0 deletions
31
examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/PutItemConditional.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String>, | ||
) { | ||
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 "[email protected]", | ||
) | ||
|
||
putItemConditional(tableName, item) | ||
} |
38 changes: 38 additions & 0 deletions
38
examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactGetItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Map<String, AttributeValue.S>>) { | ||
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) | ||
} |
41 changes: 41 additions & 0 deletions
41
examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/TransactPutItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Map<String, String>>) { | ||
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 "[email protected]", | ||
), | ||
tableName2 to | ||
mapOf( | ||
"order" to "5678", | ||
"customer" to "1234", | ||
), | ||
) | ||
transactPutItem(requests) | ||
} |
35 changes: 35 additions & 0 deletions
35
examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/UpdateItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = "[email protected]") | ||
} |
38 changes: 38 additions & 0 deletions
38
examples/SDK/kotlin/data_plane/WorkingWithItems/src/main/kotlin/UpdateItemConditional.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is nice if you match the
PrimaryKeyValue1
andPrimaryKeyValue2
to the examples1234
and1235
that you created with the Write operations, just for uniformity.For a new user it will also help on the understanding.