简单的 DynamoDB 请求失败并出现 ResourceNotFoundException
我刚刚开始使用 Java SDK (v1.8) 运行 DynamoDB.我使用 AWS 控制台创建了一个非常简单的表.我的表有一个主哈希键,它是一个字符串(无范围).我已经将一个项目与其他 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 控制台中列为 Active
,我也可以看到该项目及其值.
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);
...但结果相同.
我对 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
.
推荐答案
请求失败,因为我在发出请求之前没有为客户端设置区域.默认区域可能是美国东部,而我的表设置在欧洲西部.这修复了它:
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));
相关文章