在不使用扫描的情况下验证 DynamoDB 中的属性?

Verifying the attribute in DynamoDB without using scan?

如何在不使用 dynamodb 扫描的情况下验证属性是否存在于 table 中?

在我的用例中,从客户端,客户请求 Customer_id 了解产品的价值。在服务器端,必须检查输入的 customer_id 是否已经存在于 DynamoDB table 中。如果没有,则必须创建一个新条目。

如何在不对 table 使用 SCAN 操作的情况下实现这种情况?

在我看来,您想对这个 table 做一个有条件的 PutItem:如果没有另一个具有相同 [=] 的项目,则将项目放入 table 13=]。这很容易做到,因为 customer_id 是 table 的哈希键。来自 PutItem documentation:

Note

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 运行 编写的一个简单示例:

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 Whosebug {

    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} }