Skip to content

Instantly share code, notes, and snippets.

@gaplo917
Created May 8, 2019 11:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaplo917/a4298d755c076b1a295026ed9b3521fa to your computer and use it in GitHub Desktop.
Save gaplo917/a4298d755c076b1a295026ed9b3521fa to your computer and use it in GitHub Desktop.
Kotlin DynamoDB Object Mapping Sample
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)
}
@gaplo917
Copy link
Author

gaplo917 commented May 8, 2019

Kotlin DynamoDB Example

Gradle Project Setup

Create a kotlin/jvm project in Intellij, add the following gradle dependency

compile 'com.amazonaws:aws-java-sdk-dynamodb:1.11.548'

Create DynamoDB Local

create a docker-compose.yml with the following config

version: '3'
services:
  dynamodb-local:
    image: amazon/dynamodb-local
    hostname: dynamodb
    restart: always
    command: -jar DynamoDBLocal.jar -sharedDb -dbPath /home/dynamodblocal/data/
    volumes:
      - /data/dynamodb:/home/dynamodblocal/data
    ports:
      - "8000:8000"

Run docker-compose up

Points to Note

  • Every entity need empty constructor
  • All fields must be var
  • Only Supported Map<String, *>
  • Enum needed special handling as shown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment