File tree Expand file tree Collapse file tree 12 files changed +384
-0
lines changed
examples/SDK/kotlin/data_plane/WorkingWithItems Expand file tree Collapse file tree 12 files changed +384
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 (" id" to AttributeValue .S (" 12345" )),
43+ mapOf (" id" to AttributeValue .S (" 1234" )),
44+ )
45+
46+ batchGetItem(tableName, keys)
47+ }
Original file line number Diff line number Diff line change 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 " john.doe@example.com" ,
49+ ),
50+ mapOf (
51+ " id" to " 1235" ,
52+ " name" to " Jane Doe" ,
53+ " email" to " jane.doe@example.com" ,
54+ ),
55+ )
56+
57+ batchWriteItem(tableName, items)
58+ }
Original file line number Diff line number Diff line change 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 (" id" to AttributeValue .S (" 1234" ))
21+
22+ deleteItem(tableName, key)
23+ }
Original file line number Diff line number Diff line change 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 (" id" to AttributeValue .S (" 1234" ))
25+
26+ getItem(tableName, key)
27+ }
Original file line number Diff line number Diff line change 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 " john.doe@example.com" ,
27+ )
28+
29+ putItem(tableName, item)
30+ }
Original file line number Diff line number Diff line change 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 " john.doe@example.com" ,
28+ )
29+
30+ putItemConditional(tableName, item)
31+ }
Original file line number Diff line number Diff line change 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 (" id" to AttributeValue .S (" 12345" )),
35+ tableName2 to mapOf (" id" to AttributeValue .S (" 1234" )),
36+ )
37+ transactGetItem(requests)
38+ }
Original file line number Diff line number Diff line change 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 " john.doe@example.com" ,
33+ ),
34+ tableName2 to
35+ mapOf (
36+ " order" to " 5678" ,
37+ " customer" to " 1234" ,
38+ ),
39+ )
40+ transactPutItem(requests)
41+ }
You can’t perform that action at this time.
0 commit comments