2019
Jul
09
Create a Table with global secondary index on local dynamodb
Unit test Create Table
- AmazonDynamoDB ddb = DynamoDBEmbedded.create().amazonDynamoDB();
- String tableName = "users";
- String hashKeyName = "id";
- String gsiIndexName = "usersIndex";
- String gsiIndexHashKeyName = "id2";
- CreateTableResult res = createTable(ddb, tableName, hashKeyName);
- public CreateTableResult createTable(AmazonDynamoDB ddb, String tableName, String hashKeyName, String rangeKeyName) {
- TableDescription tableDesc = res.getTableDescription();
- List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
- attributeDefinitions.add(new AttributeDefinition(hashKeyName, ScalarAttributeType.S));
- attributeDefinitions.add(new AttributeDefinition(gsiIndexHashKeyName, ScalarAttributeType.S));
- List<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
- ks.add(new KeySchemaElement(hashKeyName, KeyType.HASH));
- ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput(1000L, 1000L);
- # add global secondary index
- List<GlobalSecondaryIndex> gsi = new ArrayList<GlobalSecondaryIndex>();
- gsi.add(new GlobalSecondaryIndex()
- .withIndexName(gsiIndexName)
- .withKeySchema(new KeySchemaElement(gsiIndexHashKeyName, KeyType.HASH))
- .withProjection(new Projection().withProjectionType(ProjectionType.ALL))
- .withProvisionedThroughput(provisionedthroughput)
- );
- CreateTableRequest request =
- new CreateTableRequest()
- .withTableName(tableName)
- .withAttributeDefinitions(attributeDefinitions)
- .withKeySchema(ks)
- .withProvisionedThroughput(provisionedthroughput);
- request.setGlobalSecondaryIndexes(gsi);
- ddb.createTable(request);
- }
Add local secondary index
local secondary index
- List<LocalSecondaryIndex> lsi = new ArrayList<LocalSecondaryIndex>();
- lsi.add(new LocalSecondaryIndex()
- .withIndexName(lsiIndexName)
- .withKeySchema(new KeySchemaElement(lsiIndexHashKeyName, KeyType.HASH, lsiRangeKeyName, KeyType.RANGE))
- .withProjection(new Projection().withProjectionType(ProjectionType.ALL));
- createTableRequest.setLocalSecondaryIndexes(lsi);
Insert new data into local dynamodb
Insert
- DynamoDB dynamoDB = new DynamoDB(ddb);
- Table table = dynamoDB.getTable(tableName);
- String userId = "user0001";
- String userName = "puritys";
- UpdateItemSpec updateItemSpec = new UpdateItemSpec()
- .withPrimaryKey("id", userId);
- StringBuilder updateStr = new StringBuilder("set #userName = :userName");
- ValueMap value = new ValueMap();
- NameMap name = new NameMap();
- name.with("#userName", "userName");
- value.withString(":userName", userName);
- updateItemSpec.withUpdateExpression(updateStr.toString())
- .withNameMap(name)
- .withValueMap(value)
- .withReturnValues(ReturnValue.UPDATED_NEW);
Package version use gradle
build.gradle
- repositories {
- mavenLocal()
- mavenCentral()
- maven {
- url "https://s3-us-west-2.amazonaws.com/dynamodb-local/release"
- content {
- includeModule "com.amazonaws", "DynamoDBLocal"
- }
- }
- }
- dependencies {
- compile (
- "org.slf4j:slf4j-api:1.7.25",
- )
- testCompile (
- "junit:junit:4.8.1",
- "org.mockito:mockito-all:1.9.5",
- "com.almworks.sqlite4java:sqlite4java:1.0.392",
- "com.almworks.sqlite4java:libsqlite4java-linux-amd64:1.0.392",
- "com.amazonaws:DynamoDBLocal:1.11.119"
- )
- }
import class
import
- import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
- import com.amazonaws.services.dynamodbv2.document.DynamoDB;
- import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;
- import com.amazonaws.services.dynamodbv2.document.Table;
- import com.amazonaws.services.dynamodbv2.document.utils.NameMap;
- import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
- import com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded;
- import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
- import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
- import com.amazonaws.services.dynamodbv2.model.CreateTableResult;
- import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
- import com.amazonaws.services.dynamodbv2.model.KeyType;
- import com.amazonaws.services.dynamodbv2.model.ListTablesResult;
- import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
- import com.amazonaws.services.dynamodbv2.model.ReturnValue;
- import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
- import com.amazonaws.services.dynamodbv2.model.TableDescription;
- import com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex;
- import com.amazonaws.services.dynamodbv2.model.Projection;
- import com.amazonaws.services.dynamodbv2.model.ProjectionType;
- System.setProperty("sqlite4java.library.path", "native-libs");
pom.xml
pom
- <dependency>
- <groupId>com.amazonaws</groupId>
- <artifactId>DynamoDBLocal</artifactId>
- <version>1.11.86</version>
- <scope>test</scope>
- <exclusions>
- <exclusion>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-util</artifactId>
- </exclusion>
- <exclusion>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-server</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>com.almworks.sqlite4java</groupId>
- <artifactId>sqlite4java</artifactId>
- <version>1.0.392</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.almworks.sqlite4java</groupId>
- <artifactId>libsqlite4java-linux-amd64</artifactId>
- <version>1.0.392</version>
- <type>so</type>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.almworks.sqlite4java</groupId>
- <artifactId>libsqlite4java-osx</artifactId>
- <version>1.0.392</version>
- <type>dylib</type>
- <scope>test</scope>
- </dependency>
- <repositories>
- <repository>
- <id>dynamodb-local-oregon</id>
- <name>DynamoDB Local Release Repository</name>
- <url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</url>
- </repository>
- </repositories>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
- <version>2.10</version>
- <executions>
- <execution>
- <id>copy</id>
- <phase>test-compile</phase>
- <goals>
- <goal>copy-dependencies</goal>
- </goals>
- <configuration>
- <!-- copy sqlite binary to correct dir for unit tests -->
- <includeScope>test</includeScope>
- <includeTypes>so,dll,dylib</includeTypes>
- <outputDirectory>${project.basedir}/native-libs</outputDirectory>
- </configuration>
- </execution>
- </executions>
- </plugin>
目前回應 Comments(1 comments)
sanjay 2021/05/05
The heading shows unit test, but code looks like its insert and create table functionalities, where the unit test cases? I am looking for JUnit test cases for DynamoDB, something you can give examples. why do i need to connect local server for JUnit Test?
ReplyAdmin