|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | +package aws.sdk.kotlin.example |
| 6 | + |
| 7 | +import aws.sdk.kotlin.runtime.AwsServiceException |
| 8 | +import aws.sdk.kotlin.services.dynamodb.DynamodbClient |
| 9 | +import aws.sdk.kotlin.services.dynamodb.model.* |
| 10 | +import com.google.gson.JsonElement |
| 11 | +import com.google.gson.JsonParser |
| 12 | +import kotlinx.coroutines.delay |
| 13 | +import kotlinx.coroutines.runBlocking |
| 14 | +import java.lang.IllegalStateException |
| 15 | + |
| 16 | +/** |
| 17 | + * Partial implementation of: https://docs.amazonaws.cn/en_us/amazondynamodb/latest/developerguide/GettingStarted.Java.html |
| 18 | + */ |
| 19 | +fun main() = runBlocking { |
| 20 | + val client = DynamodbClient { region = "us-east-2" } |
| 21 | + |
| 22 | + val tableName = "dynamo-movies-example" |
| 23 | + |
| 24 | + try { |
| 25 | + createMoviesTable(client, tableName) |
| 26 | + |
| 27 | + client.waitForTableReady(tableName) |
| 28 | + |
| 29 | + loadMoviesTable(client, tableName) |
| 30 | + |
| 31 | + val films2222 = client.moviesInYear(tableName, 2222) |
| 32 | + check(films2222.count == 0) |
| 33 | + |
| 34 | + val films2013 = client.moviesInYear(tableName, 2013) |
| 35 | + check(films2013.count == 2) |
| 36 | + |
| 37 | + val titles = films2013.items?.mapNotNull { (it["title"] as? AttributeValue.S)?.value } |
| 38 | + println("2013 film titles:") |
| 39 | + println(titles) |
| 40 | + } catch (ex: AwsServiceException) { |
| 41 | + println(ex) |
| 42 | + } |
| 43 | + |
| 44 | + client.close() |
| 45 | +} |
| 46 | + |
| 47 | +suspend fun createMoviesTable(client: DynamodbClient, name: String) { |
| 48 | + val tableExists = client.listTables(ListTablesRequest {}).tableNames?.contains(name) ?: false |
| 49 | + if (tableExists) return |
| 50 | + |
| 51 | + val req = CreateTableRequest { |
| 52 | + tableName = name |
| 53 | + keySchema = listOf( |
| 54 | + KeySchemaElement { |
| 55 | + attributeName = "year" |
| 56 | + keyType = KeyType.Hash |
| 57 | + }, |
| 58 | + KeySchemaElement { |
| 59 | + attributeName = "title" |
| 60 | + keyType = KeyType.Range |
| 61 | + } |
| 62 | + ) |
| 63 | + |
| 64 | + attributeDefinitions = listOf( |
| 65 | + AttributeDefinition { |
| 66 | + attributeName = "year" |
| 67 | + attributeType = ScalarAttributeType.N |
| 68 | + }, |
| 69 | + AttributeDefinition { |
| 70 | + attributeName = "title" |
| 71 | + attributeType = ScalarAttributeType.S |
| 72 | + } |
| 73 | + ) |
| 74 | + provisionedThroughput { |
| 75 | + readCapacityUnits = 10 |
| 76 | + writeCapacityUnits = 10 |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + val resp = client.createTable(req) |
| 81 | + println("created table: ${resp.tableDescription?.tableArn}") |
| 82 | +} |
| 83 | + |
| 84 | +// no waiters support (yet) |
| 85 | +suspend fun DynamodbClient.waitForTableReady(name: String) { |
| 86 | + while (true) { |
| 87 | + try { |
| 88 | + val req = DescribeTableRequest { tableName = name } |
| 89 | + if (describeTable(req).table?.tableStatus != TableStatus.Creating) { |
| 90 | + println("table ready") |
| 91 | + return |
| 92 | + } |
| 93 | + } catch (ex: AwsServiceException) { |
| 94 | + if (!ex.isRetryable) throw ex |
| 95 | + } |
| 96 | + println("waiting for table to be ready...") |
| 97 | + delay(1000) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +suspend fun loadMoviesTable(client: DynamodbClient, name: String) { |
| 102 | + // load items into table |
| 103 | + val data = getResourceAsText("data.json") |
| 104 | + val elements = JsonParser.parseString(data).asJsonArray |
| 105 | + elements.forEach { |
| 106 | + // map the json element -> AttributeValue |
| 107 | + val attrValue = jsonElementToAttributeValue(it) as? AttributeValue.M ?: throw IllegalStateException("expected a top level object value") |
| 108 | + val req = PutItemRequest { |
| 109 | + tableName = name |
| 110 | + item = attrValue.value |
| 111 | + } |
| 112 | + |
| 113 | + client.putItem(req) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +suspend fun DynamodbClient.moviesInYear(name: String, year: Int): QueryResponse { |
| 118 | + val req = QueryRequest { |
| 119 | + tableName = name |
| 120 | + keyConditionExpression = "#yr = :yyyy" |
| 121 | + expressionAttributeNames = mapOf( |
| 122 | + "#yr" to "year" |
| 123 | + ) |
| 124 | + expressionAttributeValues = mapOf( |
| 125 | + ":yyyy" to AttributeValue.N(year.toString()) |
| 126 | + ) |
| 127 | + } |
| 128 | + return query(req) |
| 129 | +} |
| 130 | + |
| 131 | +// utility/support functions |
| 132 | + |
| 133 | +fun getResourceAsText(path: String): String = |
| 134 | + object {}.javaClass.getResource(path).readText() |
| 135 | + |
| 136 | +// map json to attribute values |
| 137 | +fun jsonElementToAttributeValue(element: JsonElement): AttributeValue = when { |
| 138 | + element.isJsonNull -> AttributeValue.NULL(true) |
| 139 | + element.isJsonPrimitive -> { |
| 140 | + val primitive = element.asJsonPrimitive |
| 141 | + when { |
| 142 | + primitive.isBoolean -> AttributeValue.BOOL(primitive.asBoolean) |
| 143 | + primitive.isString -> AttributeValue.S(primitive.asString) |
| 144 | + else -> { |
| 145 | + check(primitive.isNumber) { "expected number" } |
| 146 | + AttributeValue.N(primitive.asString) |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + element.isJsonArray -> AttributeValue.L(element.asJsonArray.map(::jsonElementToAttributeValue)) |
| 151 | + element.isJsonObject -> { |
| 152 | + AttributeValue.M( |
| 153 | + element.asJsonObject.entrySet().associate { |
| 154 | + it.key to jsonElementToAttributeValue(it.value) |
| 155 | + } |
| 156 | + ) |
| 157 | + } |
| 158 | + else -> throw IllegalStateException("unknown json element type: $element") |
| 159 | +} |
0 commit comments