您好我正在尝试创建一个java程序来检查亚马逊dynamodb中存在的值。 好吧,实际上我正在尝试为零售商店编写一个java程序,程序检查dynamodb中的某个产品(值)并返回商店中存在的数量。我已经在dynamodb中创建了一个表,并且还使用了扫描api来检索items.But现在我想搜索某个值是否存在,并返回当前数量。 如果有人能用一个片段帮助我,那将是非常有帮助的。 ps:我是java的新手
由于
答案 0 :(得分:0)
为表ConsumerTable
提供以下数据建模,并将leaseKey
作为索引字段;
{
"checkpoint": "49572533067729099438069946639296561054199125138133221378",
"checkpointSubSequenceNumber": 0,
"leaseCounter": 1372794,
"leaseKey": "shardId-000000000000",
"leaseOwner": "pout_172.21.63.243",
"ownerSwitchesSinceCheckpoint": 0
}
通过索引字段查询Amazon DynamoDB的Java API将是
import com.amazonaws.ClientConfiguration;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.retry.v2.RetryPolicy;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
public AmazonDynamoDB getDbConnection() {
AmazonDynamoDBClient dynamoDB = new AmazonDynamoDBClient( new ProfileCredentialsProvider("your-aws-auth-profile_in_~/.aws/credentials"));
dynamoDB.setRegion(Region.getRegion(Regions.US_WEST_2))
return dynamoDB;
}
public void getData() {
DynamoDB dynamoDB = new DynamoDB(getDbConnection());
Table consumerOffsetTable = dynamoDB.getTable("ConsumerTable");
Optional.ofNullable(consumerOffsetTable.getItem("leaseKey", "shardId-000000000000")).ifPresent(item -> {
Map<String, Object> itemMap = item.asMap();
System.out.println(itemMap.get("leaseKey").toString() + " -> " + itemMap.get("checkpoint").toString());
});
}
Maven依赖将是
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.115</version>
</dependency>
在product
,quantity
示例中,索引product
属性和查询应与上述示例类似。
使用SDK进行AWS访问 - http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html
对于Dynamodb apis
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ScanJavaDocumentAPI.html
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html