简单的 DynamoDB 请求失败并出现 ResourceNotFoundException

Simple DynamoDB request failing with ResourceNotFoundException(简单的 DynamoDB 请求失败并出现 ResourceNotFoundException)
本文介绍了简单的 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));

这篇关于简单的 DynamoDB 请求失败并出现 ResourceNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)