Created
March 20, 2024 16:53
-
-
Save ThinaticSystem/ca12e14d3195d372a300af15fff64cd1 to your computer and use it in GitHub Desktop.
DynamoDBのバッチ実験
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
package com.thinaticsystem.mre | |
import aws.sdk.kotlin.runtime.auth.credentials.ProfileCredentialsProvider | |
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient | |
import aws.sdk.kotlin.services.dynamodb.batchExecuteStatement | |
import aws.sdk.kotlin.services.dynamodb.batchWriteItem | |
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue | |
import aws.sdk.kotlin.services.dynamodb.model.BatchStatementRequest | |
import aws.sdk.kotlin.services.dynamodb.model.WriteRequest | |
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider | |
import kotlinx.coroutines.runBlocking | |
private object MainAttrKeys { | |
const val pID_S = "p_id" | |
const val sCreatedAt_N = "s_created_at" | |
} | |
fun main(): Unit = runBlocking { | |
val requireClientConfigResult = requireClientConfig() | |
DynamoDbClient { | |
credentialsProvider = requireClientConfigResult.credentialsProvider | |
region = requireClientConfigResult.region | |
}.use { client -> | |
batchExecuteStatement(client) | |
} | |
} | |
/** Result of [requireClientConfig] */ | |
private data class RequireClientConfigResult( | |
val credentialsProvider: CredentialsProvider, | |
val region: String, | |
) | |
/** | |
* @return [RequireClientConfigResult] | |
*/ | |
private suspend fun requireClientConfig(): RequireClientConfigResult { | |
val credentialsProvider: CredentialsProvider = run { | |
val profileName = askUser("AWS-CLI profile name").getOrThrow() | |
ProfileCredentialsProvider(profileName) | |
} | |
val region = askUser("Region").getOrThrow() | |
return RequireClientConfigResult( | |
credentialsProvider = credentialsProvider, | |
region = region, | |
) | |
} | |
private suspend fun batchExecuteStatement(client: DynamoDbClient) { | |
client.batchExecuteStatement { | |
val commonStatement = listOf( | |
"INSERT", | |
"INTO", | |
"\"Main\"", | |
"value", | |
listOf( | |
"'${MainAttrKeys.pID_S}' : ?", // param0 | |
"'${MainAttrKeys.sCreatedAt_N}' : ?", // param1 | |
).joinToString(",", "{", "}"), | |
).joinToString(" ") | |
statements = listOf( | |
BatchStatementRequest { | |
statement = commonStatement | |
parameters = listOf( | |
/* param0 */ AttributeValue.S("1"), | |
/* param1 */ AttributeValue.N(1.toString()), | |
) | |
}, | |
BatchStatementRequest { | |
statement = commonStatement | |
parameters = listOf( | |
/* param0 */ AttributeValue.S("1"), | |
/* param1 */ AttributeValue.N(2.toString()), | |
) | |
}, | |
) | |
}.responses | |
?.also { responses -> | |
val errors = responses.mapNotNull { it.error } | |
if (errors.isNotEmpty()) { | |
throw RuntimeException( | |
""" | |
|Error Statements: | |
${errors.joinToString("\n", "|")} | |
""".trimMargin() | |
) | |
} | |
} | |
?: throw NullPointerException("nullなことある???") | |
} | |
/** 参考 */ | |
private suspend fun batchWriteItem(client: DynamoDbClient) { | |
client.batchWriteItem { | |
requestItems = mapOf( | |
// "Main" table | |
"Main" to listOf( | |
WriteRequest { | |
putRequest { | |
item = mapOf( | |
MainAttrKeys.pID_S to AttributeValue.S("2"), | |
MainAttrKeys.sCreatedAt_N to AttributeValue.N(1.toString()), | |
) | |
} | |
}, | |
WriteRequest { | |
putRequest { | |
item = mapOf( | |
MainAttrKeys.pID_S to AttributeValue.S("2"), | |
MainAttrKeys.sCreatedAt_N to AttributeValue.N(2.toString()), | |
) | |
} | |
}, | |
), | |
) | |
}.unprocessedItems | |
?.also { unprocessedItems -> | |
if (unprocessedItems.isNotEmpty()) { | |
throw RuntimeException( | |
""" | |
|Error items: | |
${unprocessedItems.map { it.toString() }.joinToString("\n", "|")} | |
""".trimMargin() | |
) | |
} | |
} | |
?: throw NullPointerException("nullなことある???") | |
} |
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
package com.thinaticsystem.mre | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.withContext | |
suspend fun askUser( | |
message: String, | |
validator: (input: String) -> Boolean = { true }, | |
): Result<String> { | |
if (message.length < 36) { | |
print("$message: ") | |
} else { | |
println(message) | |
print(": ") | |
} | |
val input = withContext(Dispatchers.IO) { | |
readlnOrNull() | |
} | |
// Validate | |
input ?: return Result.failure(IllegalArgumentException("EOF")) | |
if (!validator(input)) { | |
return Result.failure(IllegalArgumentException("Validation error")) | |
} | |
return Result.success(input) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PartiQLのINSERTのvalueの中のやつ、
?
のクォート入れたらまずいっぽい