问题描述
如何在不使用dynamodb中使用scan的情况下验证属性是否存在于表中?
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
:如果没有另一个具有相同<$的项目,则将该项目放入表中c $ c> 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", "[email protected]"));
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", "[email protected]"), 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", "[email protected]"), 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: {[email protected], customer_name=Jim, customer_id=ABCD} }
After Garret:
{ Item: {[email protected], customer_name=Garret, customer_id=EFGH} }
{ Item: {[email protected], customer_name=Jim, customer_id=ABCD} }
Conditional check failed!
After Bob:
{ Item: {[email protected], customer_name=Garret, customer_id=EFGH} }
{ Item: {[email protected], customer_name=Jim, customer_id=ABCD} }
这篇关于在不使用扫描的情况下验证DynamoDB中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!