Skip to content

DynamoDB with the AWS SDK for Java 2.x

Uses software.amazon.awssdk:dynamodb 2.57 with endpointOverride. The code is an ordinary application for the original database; nothing in it knows about MachNoSQL.

https://dynamodb.eu-central-1.amazonaws.com
https://machnosql.example.com:8000

The address comes from the environment variable DYNAMODB_ENDPOINT, so switching to MachNoSQL changes configuration only.

Terminal window
export DYNAMODB_ENDPOINT='https://machnosql.example.com:8000'
mvn -q compile exec:java

Credentials are the usual AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY (an access key created in MachNoSQL) and AWS_REGION. Instead of passing the endpoint in code, current AWS SDKs and the AWS CLI also read AWS_ENDPOINT_URL_DYNAMODB from the environment; then the application needs no change at all.

The program as it is kept and tested in the Mach Database NoSQL repository; highlighted lines read the connection setting.

src/main/java/example/App.java
// Orders table on DynamoDB, unchanged on MachNoSQL.
package example;
import java.net.URI;
import java.util.Map;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder;
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.BillingMode;
import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement;
import software.amazon.awssdk.services.dynamodb.model.KeyType;
import software.amazon.awssdk.services.dynamodb.model.ResourceInUseException;
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;
public final class App {
private static AttributeValue s(String value) {
return AttributeValue.fromS(value);
}
public static void main(String[] args) {
DynamoDbClientBuilder builder = DynamoDbClient.builder();
String endpoint = System.getenv("DYNAMODB_ENDPOINT");
if (endpoint != null) {
builder.endpointOverride(URI.create(endpoint));
}
try (DynamoDbClient ddb = builder.build()) {
try {
ddb.createTable(t -> t.tableName("Orders")
.keySchema(
KeySchemaElement.builder().attributeName("customer").keyType(KeyType.HASH).build(),
KeySchemaElement.builder().attributeName("orderId").keyType(KeyType.RANGE).build())
.attributeDefinitions(
AttributeDefinition.builder().attributeName("customer").attributeType(ScalarAttributeType.S).build(),
AttributeDefinition.builder().attributeName("orderId").attributeType(ScalarAttributeType.S).build())
.billingMode(BillingMode.PAY_PER_REQUEST));
ddb.waiter().waitUntilTableExists(r -> r.tableName("Orders"));
} catch (ResourceInUseException alreadyThere) {
// The table exists from an earlier run.
}
ddb.putItem(p -> p.tableName("Orders").item(Map.of(
"customer", s("ada"), "orderId", s("1001"), "item", s("keyboard"), "total", AttributeValue.fromN("49"))));
ddb.putItem(p -> p.tableName("Orders").item(Map.of(
"customer", s("ada"), "orderId", s("1002"), "item", s("mouse"), "total", AttributeValue.fromN("39"))));
System.out.println(ddb.getItem(g -> g.tableName("Orders")
.key(Map.of("customer", s("ada"), "orderId", s("1001")))).item());
System.out.println(ddb.query(q -> q.tableName("Orders")
.keyConditionExpression("customer = :c")
.expressionAttributeValues(Map.of(":c", s("ada")))).items());
ddb.updateItem(u -> u.tableName("Orders")
.key(Map.of("customer", s("ada"), "orderId", s("1002")))
.updateExpression("SET #s = :shipped")
.conditionExpression("attribute_not_exists(#s)")
.expressionAttributeNames(Map.of("#s", "status"))
.expressionAttributeValues(Map.of(":shipped", s("shipped"))));
}
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.machnosql</groupId>
<artifactId>dynamodb-example</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<exec.mainClass>example.App</exec.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
<version>2.57.7</version>
</dependency>
</dependencies>
</project>