2019
Jul
09




Create a Table with global secondary index on local dynamodb

Create Table
  1. AmazonDynamoDB ddb = DynamoDBEmbedded.create().amazonDynamoDB();
  2.  
  3. String tableName = "users";
  4. String hashKeyName = "id";
  5. String gsiIndexName = "usersIndex";
  6. String gsiIndexHashKeyName = "id2";
  7. CreateTableResult res = createTable(ddb, tableName, hashKeyName);
  8. TableDescription tableDesc = res.getTableDescription();
  9. List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
  10. attributeDefinitions.add(new AttributeDefinition(hashKeyName, ScalarAttributeType.S));
  11. attributeDefinitions.add(new AttributeDefinition(gsiIndexHashKeyName, ScalarAttributeType.S));
  12. List<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
  13. ks.add(new KeySchemaElement(hashKeyName, KeyType.HASH));
  14. ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput(1000L, 1000L);
  15.  
  16.  
  17. # add global secondary index
  18. List<GlobalSecondaryIndex> gsi = new ArrayList<GlobalSecondaryIndex>();
  19. gsi.add(new GlobalSecondaryIndex()
  20. .withIndexName(gsiIndexName)
  21. .withKeySchema(new KeySchemaElement(gsiIndexHashKeyName, KeyType.HASH))
  22. .withProjection(new Projection().withProjectionType(ProjectionType.ALL))
  23. .withProvisionedThroughput(provisionedthroughput)
  24. );
  25.  
  26. CreateTableRequest request =
  27. new CreateTableRequest()
  28. .withTableName(tableName)
  29. .withAttributeDefinitions(attributeDefinitions)
  30. .withKeySchema(ks)
  31. .withProvisionedThroughput(provisionedthroughput);
  32. request.setGlobalSecondaryIndexes(gsi);
  33. ddb.createTable(request);

Add local secondary index

local secondary index
  1. List<LocalSecondaryIndex> lsi = new ArrayList<LocalSecondaryIndex>();
  2.  
  3. lsi.add(new LocalSecondaryIndex()
  4. .withIndexName(lsiIndexName)
  5. .withKeySchema(new KeySchemaElement(lsiIndexHashKeyName, KeyType.HASH, lsiRangeKeyName, KeyType.RANGE))
  6. .withProjection(new Projection().withProjectionType(ProjectionType.ALL));
  7.  
  8. createTableRequest.setLocalSecondaryIndexes(lsi);

Insert new data into local dynamodb

Insert
  1. DynamoDB dynamoDB = new DynamoDB(ddb);
  2. Table table = dynamoDB.getTable(tableName);
  3. String userId = "user0001";
  4. String userName = "puritys";
  5. UpdateItemSpec updateItemSpec = new UpdateItemSpec()
  6. .withPrimaryKey("id", userId);
  7.  
  8. StringBuilder updateStr = new StringBuilder("set #userName = :userName");
  9. ValueMap value = new ValueMap();
  10. NameMap name = new NameMap();
  11. name.with("#userName", "userName");
  12. value.withString(":userName", userName);
  13.  
  14.  
  15. updateItemSpec.withUpdateExpression(updateStr.toString())
  16. .withNameMap(name)
  17. .withValueMap(value)
  18. .withReturnValues(ReturnValue.UPDATED_NEW);

Package version use gradle

build.gradle
  1. repositories {
  2. mavenLocal()
  3. mavenCentral()
  4. maven {
  5. url "https://s3-us-west-2.amazonaws.com/dynamodb-local/release"
  6. content {
  7. includeModule "com.amazonaws", "DynamoDBLocal"
  8. }
  9. }
  10.  
  11. }
  12.  
  13. dependencies {
  14. compile (
  15. "org.slf4j:slf4j-api:1.7.25",
  16. )
  17.  
  18. testCompile (
  19. "junit:junit:4.8.1",
  20. "org.mockito:mockito-all:1.9.5",
  21. "com.almworks.sqlite4java:sqlite4java:1.0.392",
  22. "com.almworks.sqlite4java:libsqlite4java-linux-amd64:1.0.392",
  23. "com.amazonaws:DynamoDBLocal:1.11.119"
  24. )
  25.  
  26. }

import class

import
  1. import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
  2. import com.amazonaws.services.dynamodbv2.document.DynamoDB;
  3. import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;
  4. import com.amazonaws.services.dynamodbv2.document.Table;
  5. import com.amazonaws.services.dynamodbv2.document.utils.NameMap;
  6. import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
  7. import com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded;
  8. import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
  9. import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
  10. import com.amazonaws.services.dynamodbv2.model.CreateTableResult;
  11. import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
  12. import com.amazonaws.services.dynamodbv2.model.KeyType;
  13. import com.amazonaws.services.dynamodbv2.model.ListTablesResult;
  14. import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
  15. import com.amazonaws.services.dynamodbv2.model.ReturnValue;
  16. import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
  17. import com.amazonaws.services.dynamodbv2.model.TableDescription;
  18. import com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex;
  19. import com.amazonaws.services.dynamodbv2.model.Projection;
  20. import com.amazonaws.services.dynamodbv2.model.ProjectionType;

回應 (Leave a comment)