From 672be6c7ff62bf3aa0c23c2ab63eaa1c75bbb35b Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Fri, 10 Feb 2017 17:54:00 +0530 Subject: [PATCH 1/4] BAEL-650: Lambda and DynamoDB --- aws/pom.xml | 24 ++++++- .../lambda/dynamodb/SavePersonHandler.java | 57 +++++++++++++++ .../lambda/dynamodb/bean/PersonRequest.java | 71 +++++++++++++++++++ .../lambda/dynamodb/bean/PersonResponse.java | 25 +++++++ 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java create mode 100644 aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java create mode 100644 aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java diff --git a/aws/pom.xml b/aws/pom.xml index f3ae672a2f..4ea6b8786c 100644 --- a/aws/pom.xml +++ b/aws/pom.xml @@ -14,11 +14,23 @@ 1.1.0 + + com.amazonaws + aws-lambda-java-events + 1.3.0 + + commons-io commons-io 2.5 + + + com.google.code.gson + gson + 2.8.0 + @@ -26,7 +38,7 @@ org.apache.maven.plugins maven-shade-plugin - 2.3 + 3.0.0 false @@ -41,4 +53,14 @@ + + + artifactory-isg-release + https://repository.deere.com/artifactory/isg-release + + + axiom-nexus + http://isgnexus.deere.com/nexus/content/groups/public + + \ No newline at end of file diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java new file mode 100644 index 0000000000..47d3569d27 --- /dev/null +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java @@ -0,0 +1,57 @@ +/** + * "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE & + * COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE & + * COMPANY IS PROHIBITED." + */ +package com.baeldung.lambda.dynamodb; + +import com.amazonaws.regions.Region; +import com.amazonaws.regions.Regions; +import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; +import com.amazonaws.services.dynamodbv2.document.DynamoDB; +import com.amazonaws.services.dynamodbv2.document.Item; +import com.amazonaws.services.dynamodbv2.document.PutItemOutcome; +import com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec; +import com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException; +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import com.baeldung.lambda.dynamodb.bean.PersonRequest; +import com.baeldung.lambda.dynamodb.bean.PersonResponse; + +public class SavePersonHandler implements RequestHandler { + + private DynamoDB dynamoDb; + + private final String DYNAMODB_TABLE_NAME = "Person"; + private final Regions REGION = Regions.US_WEST_2; + + public PersonResponse handleRequest(PersonRequest personRequest, Context context) { + context.getLogger().log("personRequest: " + personRequest); + this.initDynamoDbClient(); + + persistData(personRequest); + + PersonResponse personResponse = new PersonResponse(); + personResponse.setMessage("Saved Successfully!!!"); + context.getLogger().log("personResponse: " + personResponse); + return personResponse; + } + + private PutItemOutcome persistData(PersonRequest personRequest) throws ConditionalCheckFailedException { + return this.dynamoDb.getTable(DYNAMODB_TABLE_NAME) + .putItem( + new PutItemSpec().withItem(new Item() + .withNumber("id", personRequest.getId()) + .withString("firstName", personRequest.getFirstName()) + .withString("lastName", personRequest.getLastName()) + .withNumber("age", personRequest.getAge()) + .withString("address", personRequest.getAddress()) + )); + } + + private void initDynamoDbClient() { + final AmazonDynamoDBClient client = new AmazonDynamoDBClient(); + client.setRegion(Region.getRegion(REGION)); + this.dynamoDb = new DynamoDB(client); + } +} diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java new file mode 100644 index 0000000000..1f699d7ee3 --- /dev/null +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java @@ -0,0 +1,71 @@ +/** + * "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE & + * COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE & + * COMPANY IS PROHIBITED." + */ +package com.baeldung.lambda.dynamodb.bean; + +import com.google.gson.Gson; + +public class PersonRequest { + private int id; + private String firstName; + private String lastName; + private int age; + private String address; + + public static void main(String[] args) { + PersonRequest personRequest = new PersonRequest(); + personRequest.setId(1); + personRequest.setFirstName("John"); + personRequest.setLastName("Doe"); + personRequest.setAge(30); + personRequest.setAddress("United States"); + System.out.println(personRequest); + } + + public String toString() { + final Gson gson = new Gson(); + return gson.toJson(this); + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java new file mode 100644 index 0000000000..19724adbfa --- /dev/null +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java @@ -0,0 +1,25 @@ +/** + * "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE & + * COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE & + * COMPANY IS PROHIBITED." + */ +package com.baeldung.lambda.dynamodb.bean; + +import com.google.gson.Gson; + +public class PersonResponse { + private String message; + + public String toString() { + final Gson gson = new Gson(); + return gson.toJson(this); + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} From c9bb4b9e37ca9d4a45a0d90adbd1059ceb7ae27d Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Sun, 12 Feb 2017 13:44:03 +0530 Subject: [PATCH 2/4] BAEL-650: Removed unwanted comments --- aws/pom.xml | 10 ---------- .../baeldung/lambda/dynamodb/SavePersonHandler.java | 5 ----- .../baeldung/lambda/dynamodb/bean/PersonRequest.java | 5 ----- .../baeldung/lambda/dynamodb/bean/PersonResponse.java | 5 ----- 4 files changed, 25 deletions(-) diff --git a/aws/pom.xml b/aws/pom.xml index 4ea6b8786c..881ba6522d 100644 --- a/aws/pom.xml +++ b/aws/pom.xml @@ -53,14 +53,4 @@ - - - artifactory-isg-release - https://repository.deere.com/artifactory/isg-release - - - axiom-nexus - http://isgnexus.deere.com/nexus/content/groups/public - - \ No newline at end of file diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java index 47d3569d27..70aa0fd68f 100644 --- a/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java @@ -1,8 +1,3 @@ -/** - * "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE & - * COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE & - * COMPANY IS PROHIBITED." - */ package com.baeldung.lambda.dynamodb; import com.amazonaws.regions.Region; diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java index 1f699d7ee3..6071f4448e 100644 --- a/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java @@ -1,8 +1,3 @@ -/** - * "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE & - * COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE & - * COMPANY IS PROHIBITED." - */ package com.baeldung.lambda.dynamodb.bean; import com.google.gson.Gson; diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java index 19724adbfa..a61294ddea 100644 --- a/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java @@ -1,8 +1,3 @@ -/** - * "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE & - * COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE & - * COMPANY IS PROHIBITED." - */ package com.baeldung.lambda.dynamodb.bean; import com.google.gson.Gson; From dd0158836921e4170255dddc3da808ebe47577a5 Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Wed, 15 Feb 2017 18:07:14 +0530 Subject: [PATCH 3/4] BAEL-650: Small refactoring --- .../lambda/dynamodb/SavePersonHandler.java | 23 ++++++++----------- .../lambda/dynamodb/bean/PersonRequest.java | 3 ++- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java index 70aa0fd68f..625da62efd 100644 --- a/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java @@ -17,35 +17,32 @@ public class SavePersonHandler implements RequestHandler Date: Thu, 23 Feb 2017 11:19:30 +0530 Subject: [PATCH 4/4] BAEL-650: Review comment changes --- aws/pom.xml | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/aws/pom.xml b/aws/pom.xml index 881ba6522d..681b76cfd4 100644 --- a/aws/pom.xml +++ b/aws/pom.xml @@ -7,29 +7,36 @@ jar aws + + 2.5 + 1.3.0 + 1.1.0 + 2.8.0 + + com.amazonaws aws-lambda-java-core - 1.1.0 + ${aws-lambda-java-core.version} com.amazonaws aws-lambda-java-events - 1.3.0 + ${aws-lambda-java-events.version} commons-io commons-io - 2.5 + ${commons-io.version} com.google.code.gson gson - 2.8.0 + ${gson.version}