在Java中batchGetItem API错误错误、Java、batchGetItem、API

2023-09-11 11:32:19 作者:烟勤话少脾气暴

我查询实体有一个 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);
 

任何线索?

解决方案

您已经定义的范围属性的Hash和范围类型主键类型的号的,但$ P ppare其属性值$通过 withS()类型的字符串的为您的要求:

  fkeys.add(新键()。withHashKeyElement(新的AttributeValue()。withS(键))
        .withRangeKeyElement(新的AttributeValue()withS(ranges.get(ⅰ)的ToString())。));
 
Java开发中 API接口不用写 Controller也可以

修改withS(String小号的)以withN(String小号的)应该相应地解决你的问题(容易混淆这两种方法都需要类型的参数的字符串的):

  fkeys.add(新键()。withHashKeyElement(新的AttributeValue()。withS(键))
        .withRangeKeyElement(新的AttributeValue()withN(ranges.get(ⅰ)的ToString())。));
 

诚然,对隐弱类型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 ;)

 
精彩推荐
图片推荐