Reading dynamo db data using java.

-1

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 : com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient@58359ebd {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(AWSCredentialsProviderChain.java:142) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.getCredentialsFromContext(AmazonHttpClient.java:1269) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.runBeforeRequestHandlers(AmazonHttpClient.java:845) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:794) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:781) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:755) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:715) at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:697) at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:561) at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:541) at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:6875) at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:6842) at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.executeGetItem(AmazonDynamoDBClient.java:3493) at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.getItem(AmazonDynamoDBClient.java:3457) at com.amazonaws.services.dynamodbv2.document.internal.GetItemImpl.doLoadItem(GetItemImpl.java:77) at com.amazonaws.services.dynamodbv2.document.internal.GetItemImpl.getItemOutcome(GetItemImpl.java:40) at com.amazonaws.services.dynamodbv2.document.internal.GetItemImpl.getItemOutcome(GetItemImpl.java:99) at com.amazonaws.services.dynamodbv2.document.internal.GetItemImpl.getItem(GetItemImpl.java:111) at com.amazonaws.services.dynamodbv2.document.Table.getItem(Table.java:624) at Get_DynamoDB_Data.main(Get_DynamoDB_Data.java:52)

1 Answer
0

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 : com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient@58359ebd {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
		.standard()
		.withRegion("AP_SOUTH_1")
		.build();

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(); 
	System.out.println(dynamoDBClient.listTables()); 

	// 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.

AWS
answered 8 months 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