Reading dynamo db data using java.


This is code i am using to read data : public static AmazonDynamoDB initializeDynamoDBClient() { // Set your AWS credentials String accessKey = "*******************"; String secretKey = "*****************/SUnL"; BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey); // Create a DynamoDB client AmazonDynamoDB client = AmazonDynamoDBClient.builder().withRegion(Regions.AP_SOUTH_1) // desired AWS region .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build(); return client; } public static void main(String[] args) { AmazonDynamoDB dynamoDB = initializeDynamoDBClient(); System.out.println(dynamoDB.listTables()); AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.standard().withRegion("AP_SOUTH_1") // .build();

	DynamoDB dynamo_DB = new DynamoDB(dynamoDBClient);
	// Replace "YourTableName" with the name of your DynamoDB table
	String tableName = "Automation";
	// Get the DynamoDB table
	Table table = dynamo_DB.getTable(tableName);
	// Specify the primary key value you want to retrieve
	String Pk = "MG2GFRP9411";
	// Retrieve the item using its primary key
	//Item item=dynamoDBClient.getItem(null)
	Item item = table.getItem("Automation007", Pk);	

	if (item != null) {
		// Do something with the retrieved item
		System.out.println("Item: " + item.toJSONPretty());
	} else {
		System.out.println("No matching item found in the table.");

this is output i am getting :

Client Name : {TableNames: [Automation],} Dynamo DB Table Name : {Automation: null} Exception in thread "main" com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain: [EnvironmentVariableCredentialsProvider: Unable to load AWS credentials from environment variables (AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY) and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY)), SystemPropertiesCredentialsProvider: Unable to load AWS credentials from Java system properties (aws.accessKeyId and aws.secretKey), WebIdentityTokenCredentialsProvider: To use assume role profiles the aws-java-sdk-sts module must be on the class path., com.amazonaws.auth.profile.ProfileCredentialsProvider@6d91790b: profile file cannot be null, com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper@4a1c0752: Failed to connect to service endpoint: ] at com.amazonaws.auth.AWSCredentialsProviderChain.getCredentials( at com.amazonaws.http.AmazonHttpClient$RequestExecutor.getCredentialsFromContext( at com.amazonaws.http.AmazonHttpClient$RequestExecutor.runBeforeRequestHandlers( at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute( at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer( at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute( at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500( at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute( at com.amazonaws.http.AmazonHttpClient.execute( at com.amazonaws.http.AmazonHttpClient.execute( at at at at at at at at at at Get_DynamoDB_Data.main(

1 Answer

Hi Mr R, From what I can understand of your code and output, I can see that you have successfully printed out the list of table names that you have as the output before the Exception shows:

Client Name : {TableNames: [Automation],} Dynamo DB Table Name : {Automation: null} 

This shows your initial DynamoDB client initialization has worked and you have connected to DynamoDB and returned something. Digging further I can see that under "main" you are then initializing another DynamoDB client:

AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder

This initialization is missing .withCredentials which I believe is causing your exception. You also already have a DynamoDB client initialized in "initializeDynamoDBClient()" so there is no need to initialize a new client. I haven't gone through the rest of your code to check if it works, but to make progress I recommend changing the start of your main function to:

public static void main(String[] args) {
	AmazonDynamoDB dynamoDBClient = initializeDynamoDBClient(); 

	// Delete unneccessary call to AmazonDynamoDBClientBuilder
	// Continue from here with your getItem query

and continue from there.

There's also good documentation on Querying tables using Java with the DynamoDB SDK that should help you get your code running successfully.

answered 9 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions