BAEL-650: Lambda and DynamoDB
This commit is contained in:
parent
a09e314e38
commit
672be6c7ff
24
aws/pom.xml
24
aws/pom.xml
|
@ -14,11 +14,23 @@
|
||||||
<version>1.1.0</version>
|
<version>1.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>aws-lambda-java-events</artifactId>
|
||||||
|
<version>1.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-io</groupId>
|
<groupId>commons-io</groupId>
|
||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
<version>2.5</version>
|
<version>2.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.8.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -26,7 +38,7 @@
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-shade-plugin</artifactId>
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
<version>2.3</version>
|
<version>3.0.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
@ -41,4 +53,14 @@
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>artifactory-isg-release</id>
|
||||||
|
<url>https://repository.deere.com/artifactory/isg-release</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>axiom-nexus</id>
|
||||||
|
<url>http://isgnexus.deere.com/nexus/content/groups/public</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
</project>
|
</project>
|
|
@ -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<PersonRequest, PersonResponse> {
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue