問題描述
我剛剛開始使用 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!