Describes the basic program for accessing Amazon DynamoDB
@DynamoDBTable(tableName = "table name")
public class SampleTableEntity {
// HashKey
@DynamoDBHashKey
private String hashKey;
// RangeKey
@DynamoDBRangeKey
private String rangeKey;
//item
@DynamoDBAttribute
private String item;
}
//Client generation to access DynamoDB in the specified region
AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClientBuilder.standard()
.withRegion(Region name)
.build();
//Generate Mapper to table
DynamoDBMapper mapper = new DynamoDBMapper(amazonDynamoDB);
String hashKey="000001";
String rangeKey="000002";
SampleTableEntity entity = mapper.load(SampleTableEntity.class, hashKey, rangekey);
String hashKey="000001";
String rangeKey="000003";
Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
eav.put(":v1", new AttributeValue().withS(hashKey));
eav.put(":v2",new AttributeValue().withS(rangeKey));
DynamoDBQueryExpression<SampleTableEntity> queryExpression = new DynamoDBQueryExpression<SampleTableEntity>()
.withKeyConditionExpression("hashKey = :v1 and rangeKey <> :v2")
.withExpressionAttributeValues(eav);
List<SampleTableEntity > entityList = mapper.query(SampleTableEntity.class, queryExpression);
String item = "Item";
Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
eav.put(":v1", new AttributeValue().withS(item));
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()
.withFilterExpression("item = :v1")
.withExpressionAttributeValues(eav);
List<SampleTableEntity > entityList = mapper.scan(SampleTableEntity.class, scanExpression);
SampleTableEntity entity = new SampleTableEntity();
entity.setHashKey="0000001";
entity.setRangeKey="0000002";
entity.setItem="Item";
mapper.save(entity);
Recommended Posts