Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one use Amazon's DynamoDBMapper in Scala?

I'm having trouble using Amazon's DynamoDBMapper in Scala code. The main sticking point is getting the JVM to recognize @DynamoDBHashkey when it is used in a case class, like:

case class MyCoolCaseClass(@DynamoDBHashKey(attributeName = "my_id") myId: String) {}

Any pointers from someone who has integrated this client library into a Scala project? (I'm hoping to not simply fallback to the low-level API, though that may be a decent decision once exhausting my options with the Mapper).

like image 228
eldilibra Avatar asked Jun 12 '13 16:06

eldilibra


2 Answers

I had to do this:

import annotation.meta.beanGetter
import beans.BeanProperty

import com.amazonaws.services.dynamodbv2.datamodeling._

@DynamoDBTable(tableName="DEMOTAB")
case class DemoItem(  // it's a case class for the free stuff, but can be ordinary class
    @(DynamoDBHashKey @beanGetter)  // would not work without meta annotation
    @BeanProperty var id:String,    // must be var or mapper can't instantiate one

    @BeanProperty var number:Integer
) {
  def this() = this(null, null)     // needed by DynamoDB Mapper to instantiate
}
like image 92
qu1j0t3 Avatar answered Sep 20 '22 05:09

qu1j0t3


The DynamoDB mapper uses reflection to find the getters and setters. The SDK assumes Java-style conventions, i.e. that your getters and setters start with "get" or "is", and setters start with "set". You can see the reflection code on github.

I've been able to get it working, but it feels just like writing Java :(

@DynamoDBTable(tableName = "awesome_table")
class TheBestClass {
  private var id : Integer = _

  @DynamoDBHashKey
  def getId() = id
  def setId(_id: Integer) = id = _id
}
like image 23
MattE Avatar answered Sep 22 '22 05:09

MattE