dynamodb:如何在向表中添加记录时向关键字段添加自动递增数字 [英] dynamodb: How to add auto increment numbers to key field while adding record to table

查看:32
本文介绍了dynamodb:如何在向表中添加记录时向关键字段添加自动递增数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过控制台将自动编号添加到 dynamodb 中表的关键字段,例如0,1,2,...

I would like to add auto numbers to key field of table in dynamodb via console, e.g. 0,1,2,...

我有 contact_us 表,我希望该表具有自动 id 增量的列.请帮我解决这个问题.

I have contact_us table that I would like to have column with auto id increment. Please help me with this.

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;

public class ContactUs extends HttpServlet {

    String con_id;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
           // ...
           // ...

           Map<String, AttributeValue> item = newRecord();
           //System.out.println(item);

           // ...

           dbObject.addRecord(item, "contact_us");

        }catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
        }
    }

    private Map<String, AttributeValue> newRecord() {
        Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
        con_id = getConID();
        dateTimeTimestamp = new SysDateTimeTimestamp();
        //Need to add con_id which will be auto id.
        item.put("con_id", new AttributeValue(con_id));
        // ...
        // ...

        return item;
    }

    @DynamoDBHashKey(attributeName = "con_id")
    @DynamoDBAutoGeneratedKey
    public String getConID(){
       return con_id;
    }

    public void setConID(String con_id){
       this.con_id = con_id;
    }
}

这里我收到的 con_id 值为空.

Here I am receiving con_id value blank.

推荐答案

请参考此链接.根据 DynamoDB 中的最佳实践,不建议使用递增 1 的整数来生成表的哈希键.为了让表根据配置的容量进行扩展,请求应均匀分布在键空间中.

Please refer this link. As per the best practices in DynamoDB, it is not recommended to have an integer which increments by 1 to generate hash key of the table. In order for the table to scale according to the provisioned capacity, requests should spread evenly across the key space.

推荐的方法是使用UUID.如果您使用的是 AWS Java SDK,则可以使用 @DynamoDBAutoGeneratedKey 自动生成哈希键.散列键应定义为字符串.密钥将以标准 UUID 格式生成,这将有助于在密钥空间中扩展和传播数据.

The recommended approach is to use UUID. If you are using AWS Java SDK, you can use the @DynamoDBAutoGeneratedKey to generate the hash key automatically. The hash key should be defined as string. The key will be generated as standard UUID format which will help to scale and spread the data across the key space.

带有@DynamoDBAutoGeneratedKey 的示例类:-

import java.io.Serializable;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

@DynamoDBTable(tableName = "Order")
public class Order implements Serializable {

    private static final long serialVersionUID = -3534650012619938612L;

    private String orderId;

    private String productName;

    @DynamoDBHashKey(attributeName = "orderId")
    @DynamoDBAutoGeneratedKey
    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    @DynamoDBAttribute(attributeName = "productName")
    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

}

创建订单方法:-

public Boolean createOrder(String productName) {

        DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(dynamoDBClient);

        Order order = new Order();
        order.setProductName(productName);

        dynamoDBMapper.save(order);


        return true;

    }

我使用 Spring 配置了我的 dynamoDBClient.

I have my dynamoDBClient configured using Spring.

@Autowired
private AmazonDynamoDBClient dynamoDBClient;

此代码已成功测试.如果您正确配置了 dynamoDBClient,它应该可以工作.

This code has been tested successfully. It should work if you have the dynamoDBClient configured correctly.

这篇关于dynamodb:如何在向表中添加记录时向关键字段添加自动递增数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆