Reputation: 6484
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
.
Upvotes: 14
Views: 24171
Reputation: 11234
If you use Spring Boot, then you can do like this:
@Configuration
@EnableDynamoDBRepositories(basePackages = "com.test.repository")
@EntityScan("com.test.entity")
public class DynamoDBConfig {
@Value("${amazon.dynamodb.endpoint}")
private String amazonDynamoDBEndpoint;
@Value("${amazon.aws.accesskey}")
private String amazonAWSAccessKey;
@Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey;
@Bean
public AmazonDynamoDB amazonDynamoDB() {
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
amazonDynamoDBEndpoint, Regions.AP_SOUTHEAST_2.getName());
AWSStaticCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(amazonAWSCredentials());
AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
endpointConfiguration).withCredentials(credentialsProvider).build();
return amazonDynamoDB;
}
@Bean
public AWSCredentials amazonAWSCredentials() {
return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
}}
Upvotes: 0
Reputation: 11184
the full code may looks like :
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.QueryRequest;
import com.amazonaws.services.dynamodbv2.model.QueryResult;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
public final class LogFetcher {
static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
static String tableName = "<TABLE_NAME>";
public static ArrayList<Object> findLogsForDeviceWithMacID(String macID) {
client.setRegion(Region.getRegion(Regions.EU_WEST_1));
Condition hashKeyCondition = new Condition()
.withComparisonOperator(ComparisonOperator.EQ)
.withAttributeValueList(new AttributeValue().withS(macID));
Map<String, Condition> keyConditions = new HashMap<String, Condition>();
keyConditions.put("parentKey", hashKeyCondition);
QueryRequest queryRequest = new QueryRequest()
.withTableName(tableName)
.withKeyConditions(keyConditions);
QueryResult result = client.query(queryRequest);
ArrayList<Object> data = new ArrayList<Object>();
for (Map<String, AttributeValue> item : result.getItems()) {
// printItem(item);
data.add(item);
}
return data;
}
private static void printItem(Map<String, AttributeValue> attributeList) {
for (Map.Entry<String, AttributeValue> item : attributeList.entrySet()) {
String attributeName = item.getKey();
AttributeValue value = item.getValue();
System.out.println(attributeName + " "
+ (value.getS() == null ? "" : "S=[" + value.getS() + "]")
+ (value.getN() == null ? "" : "N=[" + value.getN() + "]")
+ (value.getB() == null ? "" : "B=[" + value.getB() + "]")
+ (value.getSS() == null ? "" : "SS=[" + value.getSS() + "]")
+ (value.getNS() == null ? "" : "NS=[" + value.getNS() + "]")
+ (value.getBS() == null ? "" : "BS=[" + value.getBS() + "] \n"));
}
}
}
Upvotes: 0
Reputation: 6484
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));
Upvotes: 39