In this post, you will learn about Java code example related to creating an entry in the DynamoDB table using PutItem API. The following are some of the points considered later in this article:
Make sure you have configured your dev environment with appropriate Access Key ID and secret access key. You could do that by executing the command such as following:
aws configure
If you have configured it correctly, you should be able to find your key details in credentials which can be found at the following path: ~/.aws/credentials
In case, you are working with a Java Maven project, put following in the pom.xml file for including DynamoDB library.
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-dynamodb --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-dynamodb</artifactId> <version>1.11.289</version> </dependency>
Before looking at the code, make sure you have created a table by using command or AWS DynamoDB console. In the following code example, an item is created in a table named as “candidate” which represents a candidate scheduled for one or more interviews.
The following is the table schema:
public class DynamoDBPutItemExample { private AmazonDynamoDB dynamoDB; public DynamoDBPutItemExample() { this.dynamoDB = AmazonDynamoDBClientBuilder.standard().build() } /** * Create new interview * @param recruiter_id - email address * @param candidate_id - email address * @param company * @param role */ public void createInterview(String recruiter_id, String candidate_id, String company, String role) { // // Create a map of key-value where key represents attribute name and // value represents attribute value // Map<String, AttributeValue> values = new HashMap<String, AttributeValue>(); values.put("recruiter_id", new AttributeValue(recruiter_id)); values.put("candidate_id", new AttributeValue(candidate_id)); values.put("company", new AttributeValue(company)); values.put("position", new AttributeValue(role)); // // Create ISO 8601 compliant date // Date date = new Date(); TimeZone tz = TimeZone.getTimeZone("UTC"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); df.setTimeZone(tz); String date_time = df.format(date); values.put("date_time", new AttributeValue(date_time)); // // Create an entry in the DynamoDB table // PutItemResult response = this.dynamoDB.putItem("candidate", values); System.out.println(response.toString()); } public static void main(String[] args) { DynamoDBPutItemExample theExample = new DynamoDBPutItemExample(); theExample.createInterview("abc@gmail.com", "xyz@gmail.com", "facebook", "software engineer"); } }
In case, you are working with Spring Boot, the instantiation code could look like following:
@Component public class DynamoDBInterviewRepository implements InterviewRepository { public static final String TABLE_CANDIDATE = "candidate"; private AmazonDynamoDB dynamoDB; public DynamoDBInterviewRepository(@Autowired AmazonDynamoDB amazonDynamoDB) { this.dynamoDB = amazonDynamoDB; } @Override public void createInterview(String recruiter_id, String candidate_id, String company, String role) { Map<String, AttributeValue> values = new HashMap<String, AttributeValue>(); values.put("recruiter_id", new AttributeValue(recruiter_id)); values.put("candidate_id", new AttributeValue(candidate_id)); values.put("company", new AttributeValue(company)); values.put("position", new AttributeValue(role)); // // Create ISO 8601 compliant date // Date date = new Date(); TimeZone tz = TimeZone.getTimeZone("UTC"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); df.setTimeZone(tz); String date_time = df.format(date); values.put("date_time", new AttributeValue(date_time)); // // Create an entry in the DynamoDB table // PutItemResult response = this.dynamoDB.putItem(TABLE_CANDIDATE, values); System.out.println(response.toString()); } }
AmazonDynamoDB instance could be created as configuration bean. The following is the sample code:
@Configuration public class AWSAppConfig { @Bean(name = "amazonDynamoDB") public AmazonDynamoDB amazonDynamoDB() { return AmazonDynamoDBClientBuilder.standard().build(); } }
You can also use DynamoDBMapper and related techniques to create an entry in the table. This would help you to map a class to DynamoDB table and perform operations using appropriate APIs. The following needs to be done:
package com.vflux.rbot.services; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey; import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable; @DynamoDBTable(tableName="candidate") public class Candidate { private String recruiterId; private String candidateId; private String company; private String role; private String jobDescr; private String dateTime; @DynamoDBHashKey(attributeName="recruiter_id") public String getRecruiterId() { return recruiterId; } public void setRecruiterId(String recruiterId) { this.recruiterId = recruiterId; } @DynamoDBAttribute(attributeName="candidate_id") public String getCandidateId() { return candidateId; } public void setCandidateId(String candidateId) { this.candidateId = candidateId; } @DynamoDBAttribute(attributeName="company") public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } @DynamoDBAttribute(attributeName="role") public String getRole() { return role; } public void setRole(String role) { this.role = role; } @DynamoDBAttribute(attributeName="job_descr") public String getJobDescr() { return jobDescr; } public void setJobDescr(String jobDescr) { this.jobDescr = jobDescr; } @DynamoDBRangeKey(attributeName="date_time") public String getDateTime() { return dateTime; } public void setDateTime(String datetime) { this.dateTime = datetime; } }
public class DynamoDBPutItemExample { private DynamoDBMapper dynamoDBMapper; public DynamoDBPutItemExample() { AmazonDynamoDB dynamoDB = AmazonDynamoDBClientBuilder.standard().build() this.dynamoDBMapper = new DynamoDBMapper(dynamoDB); } /** * Create new Candidate * @param recruiter_id - email address * @param candidate_id - email address * @param company * @param role */ public void createInterview(String recruiter_id, String candidate_id, String company, String role) { // // Create a Candidate object where key represents attribute name and // value represents attribute value // Candidate candidate = new Candidate(); candidate.setRecruiterId(recruiter_id); candidate.setCandidateId(candidate_id); candidate.setCompany(company); candidate.setRole(role); // // Create ISO 8601 compliant date // Date date = new Date(); TimeZone tz = TimeZone.getTimeZone("UTC"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); df.setTimeZone(tz); String date_time = df.format(date); candidate.setDateTime(date_time); // // Create an entry in the DynamoDB table // this.dynamoDBMapper.save(candidiate); } public static void main(String[] args) { DynamoDBPutItemExample theExample = new DynamoDBPutItemExample(); theExample.createInterview("abc@gmail.com", "xyz@gmail.com", "facebook", "software engineer"); } }
In this post, you learned about using DynamoDB PutItem Java API to create an entry in the table.
Did you find this article useful? Do you have any questions or suggestions about this article in relation to using DynamoDB PutItem Java API for creating an entry in DynamoDB table? Leave a comment and ask your questions and I shall do my best to address your queries.
In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…
Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…
With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…
Anxiety is a common mental health condition that affects millions of people around the world.…
In machine learning, confounder features or variables can significantly affect the accuracy and validity of…
Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…