gpt4 book ai didi

java - 动态模块 : How to add auto increment numbers to key field while adding record to table

转载 作者:行者123 更新时间:2023-11-29 07:32:46 24 4
gpt4 key购买 nike

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

我有一个 contact_us 表,我希望其中的列具有自动 id 增量。请帮我解决这个问题。

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 值。

最佳答案

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

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

带有@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

@Autowired
private AmazonDynamoDBClient dynamoDBClient;

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

关于java - 动态模块 : How to add auto increment numbers to key field while adding record to table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39808315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com