Simple DynamoDB 请求因 ResourceNotFoundException 而失败

我刚刚使用 Java 开发工具包 (v1.8) 启动并运行 DynamoDB。我使用 AWS 控制台创建了一个非常简单的表。我的表有一个主哈希键,它是一个字符串(无范围)。我已将单个项目放入具有 4 个其他属性值(所有字符串)的表中。

我正在对表中的该项目进行简单的Java请求,但是它失败了。我绝对肯定我提供的表名称是正确的,就像我用于查询项目的主哈希键的名称一样。表状态在 AWS 控制台中列为,我也可以看到该项目及其值。ResourceNotFoundExceptionActive

这是我得到的错误:

Requested resource not found (Service: AmazonDynamoDB; Status Code: 400; Error Code: ResourceNotFoundException; Request ID: ...)

我尝试了以下方法(使用类的版本):dynamodbv2

Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put(PRIMARY_KEY, new AttributeValue().withS(value));

GetItemRequest request = new GetItemRequest()
    .withTableName(TABLE_NAME)
    .withKey(key);

GetItemResult result = client.getItem(request);

我还尝试使用所有这些类的较旧的,已弃用的版本,如下所示:

GetItemRequest request = new GetItemRequest()
        .withTableName(TABLE_NAME)
        .withKey(new Key().withHashKeyElement(new AttributeValue().withS(value)));
GetItemResult result = client.getItem(request);

...但这是相同的结果。
我的理解是,这意味着引用的表名或属性无效,但事实并非如此。如果表处于状态太早,也可以将其抛出,但我的表是 .ResourceNotFoundExceptionCreatingActive


答案 1

请求失败,因为我在发出请求之前没有为客户端设置区域。默认区域可能是美国东部,我的表是在欧洲西部设置的。这修复了它:

import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;

client.setRegion(Region.getRegion(Regions.EU_WEST_1));

答案 2

完整代码可能如下所示:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.QueryRequest;
import com.amazonaws.services.dynamodbv2.model.QueryResult;

import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;

public final class LogFetcher {

    static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
    static String tableName = "<TABLE_NAME>";

    public static ArrayList<Object> findLogsForDeviceWithMacID(String macID) {

        client.setRegion(Region.getRegion(Regions.EU_WEST_1));

        Condition hashKeyCondition = new Condition()
                .withComparisonOperator(ComparisonOperator.EQ)
                .withAttributeValueList(new AttributeValue().withS(macID));
        Map<String, Condition> keyConditions = new HashMap<String, Condition>();
        keyConditions.put("parentKey", hashKeyCondition);

        QueryRequest queryRequest = new QueryRequest()
                .withTableName(tableName)
                .withKeyConditions(keyConditions);
        QueryResult result = client.query(queryRequest);

        ArrayList<Object> data = new ArrayList<Object>();

        for (Map<String, AttributeValue> item : result.getItems()) {
           // printItem(item);
            data.add(item);
        }
        return data;
    }

    private static void printItem(Map<String, AttributeValue> attributeList) {
        for (Map.Entry<String, AttributeValue> item : attributeList.entrySet()) {
            String attributeName = item.getKey();
            AttributeValue value = item.getValue();
            System.out.println(attributeName + " "
                    + (value.getS() == null ? "" : "S=[" + value.getS() + "]")
                    + (value.getN() == null ? "" : "N=[" + value.getN() + "]")
                    + (value.getB() == null ? "" : "B=[" + value.getB() + "]")
                    + (value.getSS() == null ? "" : "SS=[" + value.getSS() + "]")
                    + (value.getNS() == null ? "" : "NS=[" + value.getNS() + "]")
                    + (value.getBS() == null ? "" : "BS=[" + value.getBS() + "] \n"));
        }
    }
}

推荐