使用ResourceNotFoundException的简单DynamoDB请求失败 [英] Simple DynamoDB request failing with ResourceNotFoundException

查看:641
本文介绍了使用ResourceNotFoundException的简单DynamoDB请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm just getting up and running with DynamoDB using the Java SDK (v1.8). I've created a very simple table using the AWS console. My table has a primary hash key, which is a String (no range). I've put a single item into the table with 4 other attribute values (all Strings).

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

I'm making a simple Java request for that item in the table, but it's failing with ResourceNotFoundException. I'm absolutely positive that the table name I'm supplying is correct, as is the name of the primary hash key that I'm using to query the item. The table status is listed in the AWS console as Active and I can see the item and its values too.

这是我得到的错误:

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

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

I've tried the following (using the dynamodbv2 versions of the classes):

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

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

I've also tried using the older, deprecated versions of all these classes, like this:

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

...但结果相同。

我对<的理解code> ResourceNotFoundException 是指它引用的表名或属性无效,但实际情况并非如此。如果表在 Creating 状态中过早,也可以抛出它,但我的表是 Active

...but it's the same result.
My understanding of the ResourceNotFoundException is that it means the table name or attribute referenced is invalid, which is not the case. It can also be thrown if the table is too early in the Creating state, but my table is Active.

推荐答案

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

The request was failing because I wasn't setting the region for the client before making the request. The default region is probably US East and my table is setup in EU West. This fixed it:

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

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

这篇关于使用ResourceNotFoundException的简单DynamoDB请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆