使用ResourceNotFoundException的简单DynamoDB请求失败

时间:2014-10-11 11:57:19

标签: java amazon-web-services amazon-dynamodb aws-sdk

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

我在表格中为该项目发出了一个简单的Java请求,但它在ResourceNotFoundException失败了。我非常肯定我提供的表名是正确的,我用来查询该项的主哈希键的名称也是正确的。表状态在AWS控制台中列为Active,我也可以看到该项及其值。

这是我得到的错误:

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);

......但结果相同。
我对ResourceNotFoundException的理解是,它表示引用的表名或属性无效,但实际情况并非如此。如果表格在Creating状态中过早,但我的表格为Active,也会抛出它。

3 个答案:

答案 0 :(得分:36)

请求失败,因为我在发出请求之前没有为客户端设置区域。默认区域可能是US East,我的表格在EU West中设置。这修好了它:

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

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

答案 1 :(得分:0)

完整代码可能如下所示:

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"));
        }
    }
}

答案 2 :(得分:0)

如果您使用Spring Boot,那么您可以这样做:

@Configuration
@EnableDynamoDBRepositories(basePackages = "com.test.repository")
@EntityScan("com.test.entity")
public class DynamoDBConfig {

    @Value("${amazon.dynamodb.endpoint}")
    private String amazonDynamoDBEndpoint;

    @Value("${amazon.aws.accesskey}")
    private String amazonAWSAccessKey;

    @Value("${amazon.aws.secretkey}")
    private String amazonAWSSecretKey;

@Bean
public AmazonDynamoDB amazonDynamoDB() {
    AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
        amazonDynamoDBEndpoint, Regions.AP_SOUTHEAST_2.getName());

    AWSStaticCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(amazonAWSCredentials());

    AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
        endpointConfiguration).withCredentials(credentialsProvider).build();

    return amazonDynamoDB;
}

@Bean
public AWSCredentials amazonAWSCredentials() {
    return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
}}