AWS DynamoDB Connector
The AWS DynamoDB connector provides a flow for streaming DynamoDB requests. For more information about DynamoDB please visit the official documentation.
Artifacts
- sbt
libraryDependencies += "com.lightbend.akka" %% "akka-stream-alpakka-dynamodb" % "0.15"
- Maven
<dependency> <groupId>com.lightbend.akka</groupId> <artifactId>akka-stream-alpakka-dynamodb_2.12</artifactId> <version>0.15</version> </dependency>
- Gradle
dependencies { compile group: 'com.lightbend.akka', name: 'akka-stream-alpakka-dynamodb_2.12', version: '0.15' }
Usage
This connector uses the default credential provider chain provided by the DynamoDB Java SDK to retrieve credentials.
Before you can construct the client, you need an ActorSystem, ActorMaterializer, and ExecutionContext.
- Scala
-
implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() implicit val ec = system.dispatcher
- Java
-
final ActorSystem system = ActorSystem.create(); final ActorMaterializer materializer = ActorMaterializer.create(system);
You can then create the client with a settings object.
- Scala
-
val settings = DynamoSettings(system) val client = DynamoClient(settings)
- Java
-
final DynamoSettings settings = DynamoSettings.apply(system); final DynamoClient client = DynamoClient.create(settings, system, materializer);
We can now send requests to DynamoDB across the connection.
- Scala
-
import DynamoImplicits._ val listTablesResult: Future[ListTablesResult] = client.single(new ListTablesRequest())
- Java
-
final Future<ListTablesResult> listTablesResultFuture = client.listTables(new ListTablesRequest());
You can also use a Flow to execute your Dynamodb call:
- Scala
-
Source .single(new CreateTableRequest().withTableName("testTable").toOp) .via(client.flow) .map(_.getTableDescription.getTableArn)
- Java
-
Source<String, NotUsed> tableArnSource = Source .single(new CreateTable(new CreateTableRequest().withTableName("testTable"))) .via(client.flow()) .map(result -> (CreateTableResult) result) .map(result -> result.getTableDescription().getTableArn());
Running the example code
The code in this guide is part of runnable tests of this project. You are welcome to edit the code and run it in sbt.
Test code requires DynamoDB server running in the background. You can start one quickly using docker:
docker run --rm -p 8001:8000 deangiberson/aws-dynamodb-local
- Scala
-
sbt > dynamodb/testOnly *Spec
- Java
-
sbt > dynamodb/testOnly *Test