在不使用扫描的情况下验证 DynamoDB 中的属性? [英] Verifying the attribute in DynamoDB without using scan?
问题描述
如何在不使用 dynamodb 扫描的情况下验证属性是否存在于表中?
How to verify an attribute whether it present in table or not without using scan in dynamodb?
在我的用例中,从客户端,客户请求使用他们的 Customer_id 来了解产品的价值.在服务器端,必须检查输入的 customer_id 是否已经存在于 DynamoDB 表中.如果没有,则必须进行新条目.
In my usecase, From client side, The customer request with their Customer_id for knowing the values of the product. In server side, have to check whether the entered customer_id already present in DynamoDB table or not. If not, have to make a new entry.
如何在不对表使用 SCAN 操作的情况下实现这种情况?
How can I implement this case without using SCAN operation to the table?
推荐答案
听起来你想在这个表上做一个条件PutItem
:如果没有,就把项目放入表中另一个具有相同 customer_id
的商品.这很容易做到,因为 customer_id
是表的哈希键.来自 PutItem
文档:
It sounds to me that you want to do a conditional PutItem
on this table: put the item into the table if there is not another item with the same customer_id
. This is easy enough to do because the customer_id
is the hash key of the table. From the PutItem
documentation:
注意
要防止新项目替换现有项目,请使用条件放置操作,并将 ComparisonOperator 设置为 NULL
主键属性或属性.
To prevent a new item from replacing an existing item, use a conditional put operation with ComparisonOperator set to NULL
for the
primary key attribute, or attributes.
这是我使用 Java SDK 中的 Dynamo DB 文档 API 编写并针对 DynamoDB Local 运行的一个快速示例:
Here is a quick example I coded up using the Dynamo DB document API in the Java SDK and running against DynamoDB Local:
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Expected;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import com.amazonaws.services.dynamodbv2.util.Tables;
public class StackOverflow {
private static final String EXAMPLE_TABLE_NAME = "example_table";
public static void main(String[] args) {
AmazonDynamoDB client = new AmazonDynamoDBClient(new BasicAWSCredentials("accessKey", "secretKey"));
client.setEndpoint("http://localhost:4000");
DynamoDB dynamoDB = new DynamoDB(client);
if (Tables.doesTableExist(client, "example_table")) client.deleteTable(EXAMPLE_TABLE_NAME);
// Create table with hash key 'customer_id'
CreateTableRequest createTableRequest = new CreateTableRequest();
createTableRequest.withTableName(EXAMPLE_TABLE_NAME);
createTableRequest.withKeySchema(new KeySchemaElement("customer_id", KeyType.HASH));
createTableRequest.withAttributeDefinitions(new AttributeDefinition("customer_id", ScalarAttributeType.S));
createTableRequest.withProvisionedThroughput(new ProvisionedThroughput(15l, 15l));
dynamoDB.createTable(createTableRequest);
Tables.waitForTableToBecomeActive(client, EXAMPLE_TABLE_NAME);
Table exampleTable = dynamoDB.getTable(EXAMPLE_TABLE_NAME);
exampleTable.putItem(new Item()
.withPrimaryKey("customer_id", "ABCD")
.withString("customer_name", "Jim")
.withString("customer_email", "jim@gmail.com"));
System.out.println("After Jim:");
exampleTable.scan()
.forEach(System.out::println);
System.out.println();
try {
exampleTable.putItem(new Item()
.withPrimaryKey("customer_id", "EFGH")
.withString("customer_name", "Garret")
.withString("customer_email", "garret@gmail.com"), new Expected("customer_id").notExist());
} catch (ConditionalCheckFailedException e) {
System.out.println("Conditional check failed!");
}
System.out.println("After Garret:");
exampleTable.scan()
.forEach(System.out::println);
System.out.println();
try {
exampleTable.putItem(new Item()
.withPrimaryKey("customer_id", "ABCD")
.withString("customer_name", "Bob")
.withString("customer_email", "bob@gmail.com"), new Expected("customer_id").notExist());
} catch (ConditionalCheckFailedException e) {
System.out.println("Conditional check failed!");
}
System.out.println("After Bob:");
exampleTable.scan()
.forEach(System.out::println);
}
}
输出:
After Jim:
{ Item: {customer_email=jim@gmail.com, customer_name=Jim, customer_id=ABCD} }
After Garret:
{ Item: {customer_email=garret@gmail.com, customer_name=Garret, customer_id=EFGH} }
{ Item: {customer_email=jim@gmail.com, customer_name=Jim, customer_id=ABCD} }
Conditional check failed!
After Bob:
{ Item: {customer_email=garret@gmail.com, customer_name=Garret, customer_id=EFGH} }
{ Item: {customer_email=jim@gmail.com, customer_name=Jim, customer_id=ABCD} }
这篇关于在不使用扫描的情况下验证 DynamoDB 中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!