在Java中batchGetItem API错误 [英] Error in batchGetItem API in java
问题描述
我查询实体有一个 HashKey
&放大器;一个 RangeKey
(数字)。当我使用 batchGetItem
就可以了,我得到了以下错误:
AWS错误code:ValidationException,AWS错误消息:一个或多个参数值无效:位置和模式之间不匹配的属性类型
模式:
表:每日
散列关键字:CUSTID(字符串)
RANGE键:年月日(数字)
数据:
客户ID:VisioNerdy
年月日:1329071400000
code:
名单,其中,重点> fkeys =新的ArrayList<关键>(); // tableName值=人民日报,键= [VisioNerdy],范围= [1329071400000] 地图<字符串,KeysAndAttributes> requestItems =新的HashMap<字符串,KeysAndAttributes>(); 的for(int i = 0; I< keys.size();我++) { 字符串键= keys.get(我); 如果(范围== NULL) fkeys.add(。新的密钥()withHashKeyElement(新的AttributeValue()withS(键))); 其他 fkeys.add(新键()。withHashKeyElement(新的AttributeValue()。withS(键)) .withRangeKeyElement(新的AttributeValue()withS(ranges.get(ⅰ)的ToString())。)); } requestItems.put(tableName值,新KeysAndAttributes()withKeys(fkeys)。); BatchGetItemRequest batchGetItemRequest =新BatchGetItemRequest()withRequestItems(requestItems)。 BatchGetItemResult结果= client.batchGetItem(batchGetItemRequest);
任何线索?
您已经定义的范围属性的<一个href="http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/DataModel.html#DataModelPrimaryKey">Hash和范围类型主键类型的号的,但$ P ppare其属性值$通过 withS()
类型的字符串的为您的要求:
fkeys.add(新键()。withHashKeyElement(新的AttributeValue()。withS(键))
.withRangeKeyElement(新的AttributeValue()withS(ranges.get(ⅰ)的ToString())。));
修改<一个href="http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodb/model/AttributeValue.html#withS%28java.lang.String%29">withS(String小号的)以<一href="http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodb/model/AttributeValue.html#withN%28java.lang.String%29">withN(String小号的)应该相应地解决你的问题(容易混淆这两种方法都需要类型的参数的字符串的):
fkeys.add(新键()。withHashKeyElement(新的AttributeValue()。withS(键))
.withRangeKeyElement(新的AttributeValue()withN(ranges.get(ⅰ)的ToString())。));
诚然,对隐弱类型<一href="http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/DataModel.html#DataModelDataTypes">DynamoDB 提交基于数据类型的字符串的参数只能并不完全缓解发展;)
The entity i am querying has a HashKey
& a RangeKey
(Number). When i use batchGetItem
on it, i get the following error:
AWS Error Code: ValidationException, AWS Error Message: One or more parameter values were invalid: Mismatching attribute types between location and schema
Schema:
Table: Daily
Hash Key: CustId (String)
Range Key: Dated (Number)
Data:
CustId : VisioNerdy
Dated : 1329071400000
Code:
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);
Any clues?
You have defined the range attribute of your Hash and Range Type Primary Key as type Number, yet prepare its attribute value via withS()
as type String for your request:
fkeys.add(new Key().withHashKeyElement(new AttributeValue().withS(key))
.withRangeKeyElement(new AttributeValue().withS(ranges.get(i).toString())));
Changing withS(String s) to withN(String s) should remedy your problem accordingly (confusingly both methods require a parameter of type String):
fkeys.add(new Key().withHashKeyElement(new AttributeValue().withS(key))
.withRangeKeyElement(new AttributeValue().withN(ranges.get(i).toString())));
Admittedly, the implicit weak typing of the DynamoDB data types submission based on String parameters only doesn't exactly ease developing ;)
这篇关于在Java中batchGetItem API错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!