2012-02-13 46 views
5

我正在查询的实体有一个HashKey & a RangeKey(数字)。当我使用它batchGetItem,我得到以下错误:Java中batchGetItem API的错误

AWS Error Code: ValidationException, AWS Error Message: One or more parameter values were invalid: Mismatching attribute types between location and schema

模式:

Table: Daily

Hash Key: CustId (String)

Range Key: Dated (Number)

数据:

CustId : VisioNerdy

Dated : 1329071400000

代码:

List<Key> fkeys = new ArrayList<Key>(); //tableName="Daily", keys=["VisioNerdy"], ranges=[1329071400000] 
    Map<String, KeysAndAttributes> requestItems = new HashMap<String, KeysAndAttributes>(); 
    for(int i = 0; i < keys.size(); i++) 
    { 
     String key = keys.get(i); 
     if(ranges == null) 
      fkeys.add(new Key().withHashKeyElement(new AttributeValue().withS(key))); 
     else 
      fkeys.add(new Key().withHashKeyElement(new AttributeValue().withS(key)) 
        .withRangeKeyElement(new AttributeValue().withS(ranges.get(i).toString()))); 
    } 
    requestItems.put(tableName, new KeysAndAttributes().withKeys(fkeys)); 
    BatchGetItemRequest batchGetItemRequest = new BatchGetItemRequest().withRequestItems(requestItems); 
    BatchGetItemResult result = client.batchGetItem(batchGetItemRequest); 

任何线索?

+0

能否请您添加(最终凝结)架构和执行查询,以缓解代码段分析?谢谢! – 2012-02-13 09:29:43

+1

已编辑的问题包括他们。谢谢! – 2012-02-14 02:50:42

+0

你有“if(ranges == null)”,但如果一个Table有一个范围键,则需要一个值;你不能忽略它。 – 2012-02-14 04:49:15

回答

8

您已经定义了Hash and Range Type Primary Key的范围属性类型,但通过withS()准备自己的属性值类型字符串您的要求:

fkeys.add(new Key().withHashKeyElement(new AttributeValue().withS(key)) 
     .withRangeKeyElement(new AttributeValue().withS(ranges.get(i).toString()))); 

更改withS(String s)withN(String s)应该解决您的问题(混淆地,两种方法都需要字符串的参数):

fkeys.add(new Key().withHashKeyElement(new AttributeValue().withS(key)) 
     .withRangeKeyElement(new AttributeValue().withN(ranges.get(i).toString()))); 

诚然,基于字符串参数仅供并不完全缓解发展DynamoDB data types提交的隐含弱类型;)

+0

明白了!谢谢! – 2012-02-14 15:50:28

+0

@Mani:很高兴我能帮上忙 - 如果能够解决您的问题,您能否请求upvote并特别接受答案?这是在这里所期望的,既能让你的问题看不见,也能为其他人的问题腾出空间,并确保用户保持动力以互相帮助;)请参阅常见问题解答[什么是信誉?](http:// stackoverflow .com/faq#声望)了解详情 - 谢谢! – 2012-02-14 16:33:05

+1

完成!感谢您的指导! – 2012-02-14 16:48:49