-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,749 additions
and
1,625 deletions.
There are no files selected for viewing
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
175 changes: 91 additions & 84 deletions
175
kotlin/services/dynamodb/src/main/kotlin/com/kotlin/dynamodb/CreateTable.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 |
---|---|---|
@@ -1,84 +1,91 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.kotlin.dynamodb | ||
|
||
// snippet-start:[dynamodb.kotlin.create_table.import] | ||
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient | ||
import aws.sdk.kotlin.services.dynamodb.model.AttributeDefinition | ||
import aws.sdk.kotlin.services.dynamodb.model.CreateTableRequest | ||
import aws.sdk.kotlin.services.dynamodb.model.KeySchemaElement | ||
import aws.sdk.kotlin.services.dynamodb.model.KeyType | ||
import aws.sdk.kotlin.services.dynamodb.model.ProvisionedThroughput | ||
import aws.sdk.kotlin.services.dynamodb.model.ScalarAttributeType | ||
import aws.sdk.kotlin.services.dynamodb.waiters.waitUntilTableExists | ||
import kotlin.system.exitProcess | ||
// snippet-end:[dynamodb.kotlin.create_table.import] | ||
|
||
/** | ||
Before running this Kotlin code example, set up your development environment, | ||
including your credentials. | ||
For more information, see the following documentation topic: | ||
https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html | ||
*/ | ||
|
||
suspend fun main(args: Array<String>) { | ||
val usage = """ | ||
Usage: | ||
<tableName> <key> | ||
Where: | ||
tableName - The Amazon DynamoDB table to create (for example, Music3). | ||
key - The key for the Amazon DynamoDB table (for example, Artist). | ||
""" | ||
|
||
if (args.size != 2) { | ||
println(usage) | ||
exitProcess(0) | ||
} | ||
|
||
val tableName = args[0] | ||
val key = args[1] | ||
println("Creating an Amazon DynamoDB table named $tableName with a key named $key") | ||
val tableArn = createNewTable(tableName, key) | ||
println("The new table ARN is $tableArn") | ||
} | ||
|
||
// snippet-start:[dynamodb.kotlin.create_table.main] | ||
suspend fun createNewTable(tableNameVal: String, key: String): String? { | ||
val attDef = AttributeDefinition { | ||
attributeName = key | ||
attributeType = ScalarAttributeType.S | ||
} | ||
|
||
val keySchemaVal = KeySchemaElement { | ||
attributeName = key | ||
keyType = KeyType.Hash | ||
} | ||
|
||
val provisionedVal = ProvisionedThroughput { | ||
readCapacityUnits = 10 | ||
writeCapacityUnits = 10 | ||
} | ||
|
||
val request = CreateTableRequest { | ||
attributeDefinitions = listOf(attDef) | ||
keySchema = listOf(keySchemaVal) | ||
provisionedThroughput = provisionedVal | ||
tableName = tableNameVal | ||
} | ||
|
||
DynamoDbClient { region = "us-east-1" }.use { ddb -> | ||
|
||
var tableArn: String | ||
val response = ddb.createTable(request) | ||
ddb.waitUntilTableExists { // suspend call | ||
tableName = tableNameVal | ||
} | ||
tableArn = response.tableDescription!!.tableArn.toString() | ||
println("Table $tableArn is ready") | ||
return tableArn | ||
} | ||
} | ||
// snippet-end:[dynamodb.kotlin.create_table.main] | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.kotlin.dynamodb | ||
|
||
// snippet-start:[dynamodb.kotlin.create_table.import] | ||
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient | ||
import aws.sdk.kotlin.services.dynamodb.model.AttributeDefinition | ||
import aws.sdk.kotlin.services.dynamodb.model.CreateTableRequest | ||
import aws.sdk.kotlin.services.dynamodb.model.KeySchemaElement | ||
import aws.sdk.kotlin.services.dynamodb.model.KeyType | ||
import aws.sdk.kotlin.services.dynamodb.model.ProvisionedThroughput | ||
import aws.sdk.kotlin.services.dynamodb.model.ScalarAttributeType | ||
import aws.sdk.kotlin.services.dynamodb.waiters.waitUntilTableExists | ||
import kotlin.system.exitProcess | ||
// snippet-end:[dynamodb.kotlin.create_table.import] | ||
|
||
/** | ||
Before running this Kotlin code example, set up your development environment, | ||
including your credentials. | ||
For more information, see the following documentation topic: | ||
https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html | ||
*/ | ||
|
||
suspend fun main(args: Array<String>) { | ||
val usage = """ | ||
Usage: | ||
<tableName> <key> | ||
Where: | ||
tableName - The Amazon DynamoDB table to create (for example, Music3). | ||
key - The key for the Amazon DynamoDB table (for example, Artist). | ||
""" | ||
|
||
if (args.size != 2) { | ||
println(usage) | ||
exitProcess(0) | ||
} | ||
|
||
val tableName = args[0] | ||
val key = args[1] | ||
println("Creating an Amazon DynamoDB table named $tableName with a key named $key") | ||
val tableArn = createNewTable(tableName, key) | ||
println("The new table ARN is $tableArn") | ||
} | ||
|
||
// snippet-start:[dynamodb.kotlin.create_table.main] | ||
suspend fun createNewTable( | ||
tableNameVal: String, | ||
key: String | ||
): String? { | ||
val attDef = | ||
AttributeDefinition { | ||
attributeName = key | ||
attributeType = ScalarAttributeType.S | ||
} | ||
|
||
val keySchemaVal = | ||
KeySchemaElement { | ||
attributeName = key | ||
keyType = KeyType.Hash | ||
} | ||
|
||
val provisionedVal = | ||
ProvisionedThroughput { | ||
readCapacityUnits = 10 | ||
writeCapacityUnits = 10 | ||
} | ||
|
||
val request = | ||
CreateTableRequest { | ||
attributeDefinitions = listOf(attDef) | ||
keySchema = listOf(keySchemaVal) | ||
provisionedThroughput = provisionedVal | ||
tableName = tableNameVal | ||
} | ||
|
||
DynamoDbClient { region = "us-east-1" }.use { ddb -> | ||
var tableArn: String | ||
val response = ddb.createTable(request) | ||
ddb.waitUntilTableExists { | ||
// suspend call | ||
tableName = tableNameVal | ||
} | ||
tableArn = response.tableDescription!!.tableArn.toString() | ||
println("Table $tableArn is ready") | ||
return tableArn | ||
} | ||
} | ||
// snippet-end:[dynamodb.kotlin.create_table.main] |
121 changes: 63 additions & 58 deletions
121
kotlin/services/dynamodb/src/main/kotlin/com/kotlin/dynamodb/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 |
---|---|---|
@@ -1,58 +1,63 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.kotlin.dynamodb | ||
|
||
// snippet-start:[dynamodb.kotlin.delete_item.import] | ||
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient | ||
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue | ||
import aws.sdk.kotlin.services.dynamodb.model.DeleteItemRequest | ||
import kotlin.system.exitProcess | ||
// snippet-end:[dynamodb.kotlin.delete_item.import] | ||
|
||
/** | ||
Before running this Kotlin code example, set up your development environment, | ||
including your credentials. | ||
For more information, see the following documentation topic: | ||
https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html | ||
*/ | ||
|
||
suspend fun main(args: Array<String>) { | ||
val usage = """ | ||
Usage: | ||
<tableName> <key> <keyval> | ||
Where: | ||
tableName - The Amazon DynamoDB table to delete the item from (for example, Music3). | ||
key - The key used in the Amazon DynamoDB table (for example, Artist). | ||
keyval - The key value that represents the item to delete (for example, Famous Band). | ||
""" | ||
|
||
if (args.size != 3) { | ||
println(usage) | ||
exitProcess(0) | ||
} | ||
|
||
val tableName = args[0] | ||
val key = args[1] | ||
val keyVal = args[2] | ||
deleteDynamoDBItem(tableName, key, keyVal) | ||
} | ||
|
||
// snippet-start:[dynamodb.kotlin.delete_item.main] | ||
suspend fun deleteDynamoDBItem(tableNameVal: String, keyName: String, keyVal: String) { | ||
val keyToGet = mutableMapOf<String, AttributeValue>() | ||
keyToGet[keyName] = AttributeValue.S(keyVal) | ||
|
||
val request = DeleteItemRequest { | ||
tableName = tableNameVal | ||
key = keyToGet | ||
} | ||
|
||
DynamoDbClient { region = "us-east-1" }.use { ddb -> | ||
ddb.deleteItem(request) | ||
println("Item with key matching $keyVal was deleted") | ||
} | ||
} | ||
// snippet-end:[dynamodb.kotlin.delete_item.main] | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.kotlin.dynamodb | ||
|
||
// snippet-start:[dynamodb.kotlin.delete_item.import] | ||
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient | ||
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue | ||
import aws.sdk.kotlin.services.dynamodb.model.DeleteItemRequest | ||
import kotlin.system.exitProcess | ||
// snippet-end:[dynamodb.kotlin.delete_item.import] | ||
|
||
/** | ||
Before running this Kotlin code example, set up your development environment, | ||
including your credentials. | ||
For more information, see the following documentation topic: | ||
https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html | ||
*/ | ||
|
||
suspend fun main(args: Array<String>) { | ||
val usage = """ | ||
Usage: | ||
<tableName> <key> <keyval> | ||
Where: | ||
tableName - The Amazon DynamoDB table to delete the item from (for example, Music3). | ||
key - The key used in the Amazon DynamoDB table (for example, Artist). | ||
keyval - The key value that represents the item to delete (for example, Famous Band). | ||
""" | ||
|
||
if (args.size != 3) { | ||
println(usage) | ||
exitProcess(0) | ||
} | ||
|
||
val tableName = args[0] | ||
val key = args[1] | ||
val keyVal = args[2] | ||
deleteDynamoDBItem(tableName, key, keyVal) | ||
} | ||
|
||
// snippet-start:[dynamodb.kotlin.delete_item.main] | ||
suspend fun deleteDynamoDBItem( | ||
tableNameVal: String, | ||
keyName: String, | ||
keyVal: String | ||
) { | ||
val keyToGet = mutableMapOf<String, AttributeValue>() | ||
keyToGet[keyName] = AttributeValue.S(keyVal) | ||
|
||
val request = | ||
DeleteItemRequest { | ||
tableName = tableNameVal | ||
key = keyToGet | ||
} | ||
|
||
DynamoDbClient { region = "us-east-1" }.use { ddb -> | ||
ddb.deleteItem(request) | ||
println("Item with key matching $keyVal was deleted") | ||
} | ||
} | ||
// snippet-end:[dynamodb.kotlin.delete_item.main] |
Oops, something went wrong.