Skip to content

Commit a2484c9

Browse files
committed
Lambda function code including DAOs, domain objects, POJOs, and managers
1 parent 8f74671 commit a2484c9

File tree

10 files changed

+641
-0
lines changed

10 files changed

+641
-0
lines changed

pom.xml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.amazonaws</groupId>
8+
<artifactId>LambdaDynamoDBJava8</artifactId>
9+
<version>1.0</version>
10+
<packaging>jar</packaging>
11+
12+
13+
<properties>
14+
<java.version>1.8</java.version>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
18+
<dependencies>
19+
20+
<dependency>
21+
<groupId>com.amazonaws</groupId>
22+
<artifactId>aws-lambda-java-core</artifactId>
23+
<version>1.1.0</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>com.amazonaws</groupId>
28+
<artifactId>aws-lambda-java-log4j</artifactId>
29+
<version>1.0.0</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.amazonaws</groupId>
34+
<artifactId>aws-java-sdk-dynamodb</artifactId>
35+
<version>1.11.6</version>
36+
</dependency>
37+
38+
</dependencies>
39+
40+
<build>
41+
42+
<finalName>${project.artifactId}</finalName>
43+
44+
<plugins>
45+
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-shade-plugin</artifactId>
49+
<version>2.3</version>
50+
<configuration>
51+
<createDependencyReducedPom>false</createDependencyReducedPom>
52+
</configuration>
53+
<executions>
54+
<execution>
55+
<phase>package</phase>
56+
<goals>
57+
<goal>shade</goal>
58+
</goals>
59+
</execution>
60+
</executions>
61+
</plugin>
62+
63+
<plugin>
64+
<groupId>org.apache.maven.plugins</groupId>
65+
<artifactId>maven-compiler-plugin</artifactId>
66+
<version>3.1</version>
67+
<configuration>
68+
<source>1.8</source>
69+
<target>1.8</target>
70+
</configuration>
71+
</plugin>
72+
73+
</plugins>
74+
75+
</build>
76+
77+
</project>
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
15+
package com.amazonaws.serverless.dao;
16+
17+
18+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
19+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
20+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression;
21+
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
22+
import com.amazonaws.serverless.domain.Event;
23+
import com.amazonaws.serverless.manager.DynamoDBManager;
24+
import org.apache.log4j.Logger;
25+
26+
import java.util.*;
27+
28+
29+
public class DynamoDBEventDao implements EventDao {
30+
31+
private static final Logger log = Logger.getLogger(DynamoDBEventDao.class);
32+
33+
private static final DynamoDBMapper mapper = DynamoDBManager.mapper();
34+
35+
private static volatile DynamoDBEventDao instance;
36+
37+
38+
private DynamoDBEventDao() { }
39+
40+
public static DynamoDBEventDao instance() {
41+
42+
if (instance == null) {
43+
synchronized(DynamoDBEventDao.class) {
44+
if (instance == null)
45+
instance = new DynamoDBEventDao();
46+
}
47+
}
48+
return instance;
49+
}
50+
51+
@Override
52+
public List<Event> findAllEvents() {
53+
return mapper.scan(Event.class, new DynamoDBScanExpression());
54+
}
55+
56+
@Override
57+
public List<Event> findEventsByCity(String city) {
58+
59+
Map<String, AttributeValue> eav = new HashMap<>();
60+
eav.put(":v1", new AttributeValue().withS(city));
61+
62+
DynamoDBQueryExpression<Event> query = new DynamoDBQueryExpression<Event>()
63+
.withIndexName(Event.CITY_INDEX)
64+
.withConsistentRead(false)
65+
.withKeyConditionExpression("city = :v1")
66+
.withExpressionAttributeValues(eav);
67+
68+
return mapper.query(Event.class, query);
69+
70+
71+
// NOTE: without an index, this query would require a full table scan with a filter:
72+
/*
73+
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()
74+
.withFilterExpression("city = :val1")
75+
.withExpressionAttributeValues(eav);
76+
77+
return mapper.scan(Event.class, scanExpression);
78+
*/
79+
80+
}
81+
82+
@Override
83+
public List<Event> findEventsByTeam(String team) {
84+
85+
DynamoDBQueryExpression<Event> homeQuery = new DynamoDBQueryExpression<>();
86+
Event eventKey = new Event();
87+
eventKey.setHomeTeam(team);
88+
homeQuery.setHashKeyValues(eventKey);
89+
List<Event> homeEvents = mapper.query(Event.class, homeQuery);
90+
91+
Map<String, AttributeValue> eav = new HashMap<>();
92+
eav.put(":v1", new AttributeValue().withS(team));
93+
DynamoDBQueryExpression<Event> awayQuery = new DynamoDBQueryExpression<Event>()
94+
.withIndexName(Event.AWAY_TEAM_INDEX)
95+
.withConsistentRead(false)
96+
.withKeyConditionExpression("awayTeam = :v1")
97+
.withExpressionAttributeValues(eav);
98+
99+
List<Event> awayEvents = mapper.query(Event.class, awayQuery);
100+
101+
// need to create a new list because PaginatedList from query is immutable
102+
List<Event> allEvents = new LinkedList<>();
103+
allEvents.addAll(homeEvents);
104+
allEvents.addAll(awayEvents);
105+
allEvents.sort( (e1, e2) -> e1.getEventDate() <= e2.getEventDate() ? -1 : 1 );
106+
107+
return allEvents;
108+
}
109+
110+
@Override
111+
public Optional<Event> findEventByTeamAndDate(String team, Long eventDate) {
112+
113+
Event event = mapper.load(Event.class, team, eventDate);
114+
115+
return Optional.ofNullable(event);
116+
}
117+
118+
@Override
119+
public void saveOrUpdateEvent(Event event) {
120+
121+
mapper.save(event);
122+
}
123+
124+
@Override
125+
public void deleteEvent(String team, Long eventDate) {
126+
127+
Optional<Event> oEvent = findEventByTeamAndDate(team, eventDate);
128+
if (oEvent.isPresent()) {
129+
mapper.delete(oEvent.get());
130+
}
131+
else {
132+
log.error("Could not delete event, no such team and date combination");
133+
throw new IllegalArgumentException("Delete failed for nonexistent event");
134+
}
135+
}
136+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
15+
package com.amazonaws.serverless.dao;
16+
17+
18+
import com.amazonaws.serverless.domain.Event;
19+
20+
import java.util.List;
21+
import java.util.Optional;
22+
23+
24+
public interface EventDao {
25+
26+
List<Event> findAllEvents();
27+
28+
List<Event> findEventsByCity(String city);
29+
30+
List<Event> findEventsByTeam(String team);
31+
32+
Optional<Event> findEventByTeamAndDate(String team, Long eventDate);
33+
34+
void saveOrUpdateEvent(Event event);
35+
36+
void deleteEvent(String team, Long eventDate);
37+
38+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
15+
package com.amazonaws.serverless.domain;
16+
17+
18+
import com.amazonaws.services.dynamodbv2.datamodeling.*;
19+
20+
import java.io.Serializable;
21+
22+
23+
@DynamoDBTable(tableName = "EVENT")
24+
public class Event implements Serializable {
25+
26+
private static final long serialVersionUID = -8243145429438016232L;
27+
public static final String CITY_INDEX = "City-Index";
28+
public static final String AWAY_TEAM_INDEX = "AwayTeam-Index";
29+
30+
@DynamoDBAttribute
31+
private Long eventId;
32+
33+
@DynamoDBRangeKey
34+
private Long eventDate;
35+
36+
@DynamoDBAttribute
37+
private String sport;
38+
39+
@DynamoDBHashKey
40+
private String homeTeam;
41+
42+
@DynamoDBIndexHashKey(globalSecondaryIndexName = AWAY_TEAM_INDEX)
43+
private String awayTeam;
44+
45+
@DynamoDBIndexHashKey(globalSecondaryIndexName = CITY_INDEX)
46+
private String city;
47+
48+
@DynamoDBAttribute
49+
private String country;
50+
51+
public Event() { }
52+
53+
public Event(String team, Long date) {
54+
this.homeTeam = team;
55+
this.eventDate = date;
56+
}
57+
58+
public Event(Long eventId, Long eventDate, String sport, String homeTeam, String awayTeam, String city, String country) {
59+
this.eventId = eventId;
60+
this.eventDate = eventDate;
61+
this.sport = sport;
62+
this.homeTeam = homeTeam;
63+
this.awayTeam = awayTeam;
64+
this.city = city;
65+
this.country = country;
66+
}
67+
68+
69+
public Long getEventId() {
70+
return eventId;
71+
}
72+
73+
public void setEventId(Long eventId) {
74+
this.eventId = eventId;
75+
}
76+
77+
public Long getEventDate() {
78+
return eventDate;
79+
}
80+
81+
public void setEventDate(Long eventDate) {
82+
this.eventDate = eventDate;
83+
}
84+
85+
public String getSport() {
86+
return sport;
87+
}
88+
89+
public void setSport(String sport) {
90+
this.sport = sport;
91+
}
92+
93+
public String getHomeTeam() {
94+
return homeTeam;
95+
}
96+
97+
public void setHomeTeam(String homeTeam) {
98+
this.homeTeam = homeTeam;
99+
}
100+
101+
public String getAwayTeam() {
102+
return awayTeam;
103+
}
104+
105+
public void setAwayTeam(String awayTeam) {
106+
this.awayTeam = awayTeam;
107+
}
108+
109+
public String getCity() {
110+
return city;
111+
}
112+
113+
public void setCity(String city) {
114+
this.city = city;
115+
}
116+
117+
public String getCountry() {
118+
return country;
119+
}
120+
121+
public void setCountry(String country) {
122+
this.country = country;
123+
}
124+
125+
}

0 commit comments

Comments
 (0)