게시. 희망이 사람을 도움이됩니다.
1 단계 : 원하는 구조를 반영하는 복잡한 Java 객체를 만듭니다.
List<HashMap<String, Integer>> cartItems = new ArrayList<HashMap<String, Integer>>();
HashMap<String, Integer> item1 = new HashMap<String, Integer>();
item1.put("ProductId", 100);
item1.put("Quantity", 50);
cartItems.add(item1);
HashMap<String, Integer> item2 = new HashMap<String, Integer>();
item2.put("ProductId", 121);
item2.put("Quantity", 51);
cartItems.add(item2);
2 단계 : DynamoDB 항목을 복합 개체로 업데이트합니다.따라서 전화 후
private void updateAttribute(int id, String newAttribute, Object newValue){
Map<String, Object> newValues = new HashMap<String, Object>();
newValues.put(":value", newValue);
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey("id", id)
.withUpdateExpression("set " + newAttribute + " = :value")
.withValueMap(newValues);
siteTable.updateItem(updateItemSpec);
}
과 :
"CartItems": [
{
"ProductId": 100,
"Quantity": 50
},
{
"ProductId": 121,
"Quantity": 51
}
],
내가하지 않은 :
updateAttribute(123, "CartItems", cartItems);
새로 추가 된 카트 항목처럼 DynamoDB의에 표시 속성
나는 헬퍼 방법을 사용 upsert 시나리오를 테스트했습니다. 과거에는 upsert 기능이있을 것 같지 않았다 관해서는 https://forums.aws.amazon.com/thread.jspa?threadID=162907
깊이 중첩 된 항목으로 읽 http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.AccessingItemAttributes.html#DocumentPaths
안녕하세요, 고맙습니다, 제 제안이 내 문제를 해결했습니다 :). 지금 당신이 지정한 형식을 사용하고 있습니다. 또한, 나는 HashKey가 유일해야한다는 인상하에있었습니다. 명확히 해 주셔서 감사합니다. – Amit
해시 키는 전체 데이터 집합에 대해 고유해야합니다. 5 명의 사용자 만 있지만 수백만 개의 행이있는 경우 성능이 저하됩니다. 이 특별한 사용 사례의 경우 기본 해시 키가 고유하지 않은 것이 좋습니다. Amazon DynamoDB가 관심있는 경우 성능을 위해 데이터를 분할하는 방법에 대해 자세히 읽을 수 있습니다. – kurtzbot
이 답변은 잘못되었습니다. DynamoDB 테이블은 단순하지 않은 (파티션 키 만) 또는 복합 (파티션 및 정렬 키) 일 수있는 고유하지 않은 기본 키가있는 항목을 가질 수 없습니다. * 파티션 키 = 해시 키. 정렬 키 = 범위 키 – golem