Skip to content

Commit 672be6c

Browse files
committed
BAEL-650: Lambda and DynamoDB
1 parent a09e314 commit 672be6c

4 files changed

Lines changed: 176 additions & 1 deletion

File tree

‎aws/pom.xml‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,31 @@
1414
<version>1.1.0</version>
1515
</dependency>
1616

17+
<dependency>
18+
<groupId>com.amazonaws</groupId>
19+
<artifactId>aws-lambda-java-events</artifactId>
20+
<version>1.3.0</version>
21+
</dependency>
22+
1723
<dependency>
1824
<groupId>commons-io</groupId>
1925
<artifactId>commons-io</artifactId>
2026
<version>2.5</version>
2127
</dependency>
28+
29+
<dependency>
30+
<groupId>com.google.code.gson</groupId>
31+
<artifactId>gson</artifactId>
32+
<version>2.8.0</version>
33+
</dependency>
2234
</dependencies>
2335

2436
<build>
2537
<plugins>
2638
<plugin>
2739
<groupId>org.apache.maven.plugins</groupId>
2840
<artifactId>maven-shade-plugin</artifactId>
29-
<version>2.3</version>
41+
<version>3.0.0</version>
3042
<configuration>
3143
<createDependencyReducedPom>false</createDependencyReducedPom>
3244
</configuration>
@@ -41,4 +53,14 @@
4153
</plugin>
4254
</plugins>
4355
</build>
56+
<repositories>
57+
<repository>
58+
<id>artifactory-isg-release</id>
59+
<url>https://repository.deere.com/artifactory/isg-release</url>
60+
</repository>
61+
<repository>
62+
<id>axiom-nexus</id>
63+
<url>http://isgnexus.deere.com/nexus/content/groups/public</url>
64+
</repository>
65+
</repositories>
4466
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE &
3+
* COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE &
4+
* COMPANY IS PROHIBITED."
5+
*/
6+
package com.baeldung.lambda.dynamodb;
7+
8+
import com.amazonaws.regions.Region;
9+
import com.amazonaws.regions.Regions;
10+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
11+
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
12+
import com.amazonaws.services.dynamodbv2.document.Item;
13+
import com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
14+
import com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec;
15+
import com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException;
16+
import com.amazonaws.services.lambda.runtime.Context;
17+
import com.amazonaws.services.lambda.runtime.RequestHandler;
18+
import com.baeldung.lambda.dynamodb.bean.PersonRequest;
19+
import com.baeldung.lambda.dynamodb.bean.PersonResponse;
20+
21+
public class SavePersonHandler implements RequestHandler<PersonRequest, PersonResponse> {
22+
23+
private DynamoDB dynamoDb;
24+
25+
private final String DYNAMODB_TABLE_NAME = "Person";
26+
private final Regions REGION = Regions.US_WEST_2;
27+
28+
public PersonResponse handleRequest(PersonRequest personRequest, Context context) {
29+
context.getLogger().log("personRequest: " + personRequest);
30+
this.initDynamoDbClient();
31+
32+
persistData(personRequest);
33+
34+
PersonResponse personResponse = new PersonResponse();
35+
personResponse.setMessage("Saved Successfully!!!");
36+
context.getLogger().log("personResponse: " + personResponse);
37+
return personResponse;
38+
}
39+
40+
private PutItemOutcome persistData(PersonRequest personRequest) throws ConditionalCheckFailedException {
41+
return this.dynamoDb.getTable(DYNAMODB_TABLE_NAME)
42+
.putItem(
43+
new PutItemSpec().withItem(new Item()
44+
.withNumber("id", personRequest.getId())
45+
.withString("firstName", personRequest.getFirstName())
46+
.withString("lastName", personRequest.getLastName())
47+
.withNumber("age", personRequest.getAge())
48+
.withString("address", personRequest.getAddress())
49+
));
50+
}
51+
52+
private void initDynamoDbClient() {
53+
final AmazonDynamoDBClient client = new AmazonDynamoDBClient();
54+
client.setRegion(Region.getRegion(REGION));
55+
this.dynamoDb = new DynamoDB(client);
56+
}
57+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE &
3+
* COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE &
4+
* COMPANY IS PROHIBITED."
5+
*/
6+
package com.baeldung.lambda.dynamodb.bean;
7+
8+
import com.google.gson.Gson;
9+
10+
public class PersonRequest {
11+
private int id;
12+
private String firstName;
13+
private String lastName;
14+
private int age;
15+
private String address;
16+
17+
public static void main(String[] args) {
18+
PersonRequest personRequest = new PersonRequest();
19+
personRequest.setId(1);
20+
personRequest.setFirstName("John");
21+
personRequest.setLastName("Doe");
22+
personRequest.setAge(30);
23+
personRequest.setAddress("United States");
24+
System.out.println(personRequest);
25+
}
26+
27+
public String toString() {
28+
final Gson gson = new Gson();
29+
return gson.toJson(this);
30+
}
31+
32+
public int getId() {
33+
return id;
34+
}
35+
36+
public void setId(int id) {
37+
this.id = id;
38+
}
39+
40+
public String getFirstName() {
41+
return firstName;
42+
}
43+
44+
public void setFirstName(String firstName) {
45+
this.firstName = firstName;
46+
}
47+
48+
public String getLastName() {
49+
return lastName;
50+
}
51+
52+
public void setLastName(String lastName) {
53+
this.lastName = lastName;
54+
}
55+
56+
public int getAge() {
57+
return age;
58+
}
59+
60+
public void setAge(int age) {
61+
this.age = age;
62+
}
63+
64+
public String getAddress() {
65+
return address;
66+
}
67+
68+
public void setAddress(String address) {
69+
this.address = address;
70+
}
71+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE &
3+
* COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE &
4+
* COMPANY IS PROHIBITED."
5+
*/
6+
package com.baeldung.lambda.dynamodb.bean;
7+
8+
import com.google.gson.Gson;
9+
10+
public class PersonResponse {
11+
private String message;
12+
13+
public String toString() {
14+
final Gson gson = new Gson();
15+
return gson.toJson(this);
16+
}
17+
18+
public String getMessage() {
19+
return message;
20+
}
21+
22+
public void setMessage(String message) {
23+
this.message = message;
24+
}
25+
}

0 commit comments

Comments
 (0)