Created
May 8, 2019 11:19
-
-
Save gaplo917/a4298d755c076b1a295026ed9b3521fa to your computer and use it in GitHub Desktop.
Kotlin DynamoDB Object Mapping Sample
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
import com.amazonaws.auth.AWSStaticCredentialsProvider | |
import com.amazonaws.auth.BasicAWSCredentials | |
import com.amazonaws.client.builder.AwsClientBuilder | |
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder | |
import com.amazonaws.services.dynamodbv2.datamodeling.* | |
import com.amazonaws.services.dynamodbv2.model.BillingMode | |
import com.amazonaws.services.dynamodbv2.model.ResourceInUseException | |
import java.math.BigDecimal | |
import java.util.* | |
@DynamoDBTable(tableName = "kotlin_test_table") | |
data class DItem( | |
@DynamoDBHashKey(attributeName="id") | |
var id: String = "", | |
@DynamoDBAttribute(attributeName = "kotlin_test") | |
var kotlinTest: String? = null, | |
@DynamoDBAttribute(attributeName = "kotlin_test_obj") | |
var kotlinTestObj: KotlinTestObj? = null | |
) | |
@DynamoDBDocument | |
data class KotlinTestObj( | |
var testUUID: UUID = UUID.randomUUID(), | |
var testShort: Short = 99.toShort(), | |
var testByte: Byte = 127, | |
var testByteArray: ByteArray = ByteArray(16), | |
var testBool: Boolean = true, | |
var testInt: Int = 199, | |
var testStr: String = "199", | |
var testBigDecimal: BigDecimal = BigDecimal.TEN, | |
var testFloat: Double = 20.0, | |
var testDate: java.util.Date = java.util.Date(), | |
var testNumberList: List<Double> = listOf(1.0, 3.0, 5.0), | |
var testStringList: List<String> = listOf("abc", "def", "xyz"), | |
var testNumberSet: Set<Double> = setOf(5.0, 3.0, 1.0), | |
var testStringSet: Set<String> = setOf("abc", "def", "xyz"), | |
var testMap1: MutableMap<String, Int> = mutableMapOf("key" to 1), | |
var testMap2: MutableMap<String, String> = mutableMapOf("key" to "value"), | |
@DynamoDBTyped(DynamoDBMapperFieldModel.DynamoDBAttributeType.S) var testEnum: Direction = Direction.LEFT | |
) { | |
enum class Direction { | |
UP, DOWN, LEFT, RIGHT | |
} | |
} | |
fun main() { | |
val awsCreds = BasicAWSCredentials("access_key_id", "secret_key_id") | |
val endpoint = AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "local") | |
val ddb = AmazonDynamoDBClientBuilder.standard() | |
.withCredentials(AWSStaticCredentialsProvider(awsCreds)) | |
.withEndpointConfiguration(endpoint) | |
.build() | |
val mapper = DynamoDBMapper(ddb) | |
// create table | |
try { | |
ddb.createTable( | |
mapper.generateCreateTableRequest(DItem::class.java) | |
.withBillingMode(BillingMode.PAY_PER_REQUEST) | |
) | |
} catch (e: ResourceInUseException) { | |
println("table already created.") | |
} | |
mapper.save( | |
DItem( | |
id = "kotlin-test-abc", | |
kotlinTest = "kotlin-test-string", | |
kotlinTestObj = KotlinTestObj() | |
) | |
) | |
val record = mapper.load(DItem::class.java, "kotlin-test-abc") | |
println(record) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kotlin DynamoDB Example
Gradle Project Setup
Create a kotlin/jvm project in Intellij, add the following gradle dependency
Create DynamoDB Local
create a
docker-compose.yml
with the following configRun
docker-compose up
Points to Note
var
Map<String, *>