2016-10-01 53 views
1

我想自動號碼dynamodb通過控制檯, 例如添加到表的鍵字段0,1,2,...dynamodb:如何自動遞增號添加到關鍵領域,同時增加記錄表

我有contact_us表,我想有一個與自動識別增量列。 請幫我這個。

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值爲空白。

+0

有幫助嗎? – notionquest

回答

3

請參考link。根據DynamoDB的最佳實踐,不建議使用以1爲增量生成表的散列鍵的整數。爲了使表根據預配置容量進行擴展,請求應該均勻分佈在密鑰空間中。

建議的方法是使用UUID。如果您正在使用AWS Java SDK,則可以使用@DynamoDBAutoGeneratedKey自動生成散列密鑰。散列鍵應該被定義爲字符串。密鑰將作爲標準UUID格式生成,這將有助於在整個密鑰空間中擴展和擴展數據。

樣品類@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; 

    } 

我有我的dynamoDBClient使用Spring配置。

@Autowired 
private AmazonDynamoDBClient dynamoDBClient; 

此代碼已成功通過測試。它應該工作,如果你有正確配置dynamoDBClient。

+0

我試圖將您的建議用於我的代碼,但仍然收到空白值。讓我用代碼編輯問題。 –

+0

更新了示例代碼。請嘗試。它應該工作。 – notionquest

+0

是否生成字符串如 33e31aa4-00bf-4179-90c7-672fff92366e –