Skip to content

Commit

Permalink
Merge pull request #6 from guardian/put-all
Browse files Browse the repository at this point in the history
Add support for putting multiple items at at time
  • Loading branch information
philwills committed Mar 23, 2016
2 parents a122bb1 + 506c712 commit 2da534e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/scala/com/gu/scanamo/Scanamo.scala
Expand Up @@ -33,6 +33,23 @@ object Scanamo {
def put[T](client: AmazonDynamoDB)(tableName: String)(item: T)(implicit f: DynamoFormat[T]): PutItemResult =
client.putItem(putRequest(tableName)(item))

/**
* {{{
* >>> val client = LocalDynamoDB.client()
* >>> case class Rabbit(name: String)
* >>> import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType._
* >>> val createTableResult = LocalDynamoDB.createTable(client)("rabbits")('name -> S)
* >>> val multiPut = Scanamo.putAll(client)("rabbits")((
* ... for { _ <- 0 until 100 } yield Rabbit(util.Random.nextString(500))).toList).toList
* >>> Scanamo.scan[Rabbit](client)("rabbits").toList.size
* 100
* }}}
*/
def putAll[T](client: AmazonDynamoDB)(tableName: String)(items: List[T])(implicit f: DynamoFormat[T]): Streaming[BatchWriteItemResult] =
Streaming.fromIteratorUnsafe(for {
batch <- items.grouped(25)
} yield client.batchWriteItem(batchPutRequest(tableName)(batch)))

/**
* {{{
* >>> val client = LocalDynamoDB.client()
Expand Down
4 changes: 4 additions & 0 deletions src/main/scala/com/gu/scanamo/ScanamoRequest.scala
Expand Up @@ -20,6 +20,10 @@ object ScanamoRequest {
def putRequest[T](tableName: String)(item: T)(implicit f: DynamoFormat[T]): PutItemRequest =
new PutItemRequest().withTableName(tableName).withItem(f.write(item).getM)

def batchPutRequest[T](tableName: String)(items: List[T])(implicit f: DynamoFormat[T]): BatchWriteItemRequest =
new BatchWriteItemRequest().withRequestItems(Map(tableName -> items.map(i =>
new WriteRequest().withPutRequest(new PutRequest().withItem(f.write(i).getM))
).asJava).asJava)

/**
* {{{
Expand Down

0 comments on commit 2da534e

Please sign in to comment.