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).
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.
This is the error I'm getting:
Requested resource not found (Service: AmazonDynamoDB; Status Code: 400; Error Code: ResourceNotFoundException; Request ID: ...)
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);
...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));