Merge branch 'master' into BAEL-4590
This commit is contained in:
+4
-1
@@ -87,4 +87,7 @@ transaction.log
|
||||
apache-cxf/cxf-aegis/baeldung.xml
|
||||
testing-modules/report-*.json
|
||||
|
||||
libraries-2/*.db
|
||||
libraries-2/*.db
|
||||
|
||||
# SDKMan
|
||||
.sdkmanrc
|
||||
|
||||
@@ -9,4 +9,5 @@
|
||||
- [The Caesar Cipher in Java](https://www.baeldung.com/java-caesar-cipher)
|
||||
- [Implementing a 2048 Solver in Java](https://www.baeldung.com/2048-java-solver)
|
||||
- [Finding Top K Elements in an Array](https://www.baeldung.com/java-array-top-elements)
|
||||
- [Reversing a Linked List in Java](https://www.baeldung.com/java-reverse-linked-list)
|
||||
- More articles: [[<-- prev]](/../algorithms-miscellaneous-5)
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
public class LinkedListReversal {
|
||||
|
||||
ListNode reverseList(ListNode head) {
|
||||
ListNode previous = null;
|
||||
ListNode current = head;
|
||||
while (current != null) {
|
||||
ListNode nextElement = current.getNext();
|
||||
current.setNext(previous);
|
||||
previous = current;
|
||||
current = nextElement;
|
||||
}
|
||||
return previous;
|
||||
}
|
||||
|
||||
ListNode reverseListRecursive(ListNode head) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
}
|
||||
if (head.getNext() == null) {
|
||||
return head;
|
||||
}
|
||||
ListNode node = reverseListRecursive(head.getNext());
|
||||
head.getNext().setNext(head);
|
||||
head.setNext(null);
|
||||
return node;
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
public class ListNode {
|
||||
|
||||
private int data;
|
||||
private ListNode next;
|
||||
|
||||
ListNode(int data) {
|
||||
this.data = data;
|
||||
this.next = null;
|
||||
}
|
||||
|
||||
public int getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public ListNode getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setData(int data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setNext(ListNode next) {
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class LinkedListReversalUnitTest {
|
||||
@Test
|
||||
public void givenLinkedList_whenIterativeReverse_thenOutputCorrectResult() {
|
||||
ListNode head = constructLinkedList();
|
||||
ListNode node = head;
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
assertNotNull(node);
|
||||
assertEquals(i, node.getData());
|
||||
node = node.getNext();
|
||||
}
|
||||
LinkedListReversal reversal = new LinkedListReversal();
|
||||
node = reversal.reverseList(head);
|
||||
for (int i = 5; i >= 1; i--) {
|
||||
assertNotNull(node);
|
||||
assertEquals(i, node.getData());
|
||||
node = node.getNext();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLinkedList_whenRecursiveReverse_thenOutputCorrectResult() {
|
||||
ListNode head = constructLinkedList();
|
||||
ListNode node = head;
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
assertNotNull(node);
|
||||
assertEquals(i, node.getData());
|
||||
node = node.getNext();
|
||||
}
|
||||
LinkedListReversal reversal = new LinkedListReversal();
|
||||
node = reversal.reverseListRecursive(head);
|
||||
for (int i = 5; i >= 1; i--) {
|
||||
assertNotNull(node);
|
||||
assertEquals(i, node.getData());
|
||||
node = node.getNext();
|
||||
}
|
||||
}
|
||||
|
||||
private ListNode constructLinkedList() {
|
||||
ListNode head = null;
|
||||
ListNode tail = null;
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
ListNode node = new ListNode(i);
|
||||
if (head == null) {
|
||||
head = node;
|
||||
} else {
|
||||
tail.setNext(node);
|
||||
}
|
||||
tail = node;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
||||
@@ -129,6 +129,11 @@
|
||||
<artifactId>zookeeper</artifactId>
|
||||
<version>${zookeeper.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+75
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.differences.dataframe.dataset.rdd;
|
||||
|
||||
|
||||
public class TouristData {
|
||||
|
||||
private String region;
|
||||
private String country;
|
||||
private String year;
|
||||
private String series;
|
||||
private Double value;
|
||||
private String footnotes;
|
||||
private String source;
|
||||
|
||||
public String getRegion() {
|
||||
return region;
|
||||
}
|
||||
|
||||
public void setRegion(String region) {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
public void setYear(String year) {
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public String getSeries() {
|
||||
return series;
|
||||
}
|
||||
|
||||
public void setSeries(String series) {
|
||||
this.series = series;
|
||||
}
|
||||
|
||||
public Double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getFootnotes() {
|
||||
return footnotes;
|
||||
}
|
||||
|
||||
public void setFootnotes(String footnotes) {
|
||||
this.footnotes = footnotes;
|
||||
}
|
||||
|
||||
public String getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public void setSource(String source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TouristData [region=" + region + ", country=" + country + ", year=" + year + ", series=" + series + ", value=" + value + ", footnotes=" + footnotes + ", source=" + source + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.differences.rdd;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.spark.SparkConf;
|
||||
import org.apache.spark.api.java.JavaPairRDD;
|
||||
import org.apache.spark.api.java.JavaRDD;
|
||||
import org.apache.spark.api.java.JavaSparkContext;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import scala.Tuple2;
|
||||
|
||||
public class ActionsUnitTest {
|
||||
private static JavaRDD<String> tourists;
|
||||
private static JavaSparkContext sc;
|
||||
public static final String COMMA_DELIMITER = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
SparkConf conf = new SparkConf().setAppName("reduce")
|
||||
.setMaster("local[*]");
|
||||
sc = new JavaSparkContext(conf);
|
||||
tourists = sc.textFile("data/Tourist.csv").filter(line -> !line.startsWith("Region"));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() {
|
||||
sc.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDistinctCount_thenReturnDistinctNumRecords() {
|
||||
JavaRDD<String> countries = tourists.map(line -> {
|
||||
String[] columns = line.split(COMMA_DELIMITER);
|
||||
return columns[1];
|
||||
})
|
||||
.distinct();
|
||||
Long numberOfCountries = countries.count();
|
||||
System.out.println("Count: " + numberOfCountries);
|
||||
|
||||
assertEquals(Long.valueOf(220), numberOfCountries);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReduceByKeySum_thenTotalValuePerKey() {
|
||||
JavaRDD<String> touristsExpenditure = tourists.filter(line -> line.split(COMMA_DELIMITER)[3].contains("expenditure"));
|
||||
|
||||
JavaPairRDD<String, Double> expenditurePairRdd = touristsExpenditure.mapToPair(line -> {
|
||||
String[] columns = line.split(COMMA_DELIMITER);
|
||||
return new Tuple2<>(columns[1], Double.valueOf(columns[6]));
|
||||
});
|
||||
List<Tuple2<String, Double>> totalByCountry = expenditurePairRdd.reduceByKey((x, y) -> x + y)
|
||||
.collect();
|
||||
System.out.println("Total per Country: " + totalByCountry);
|
||||
|
||||
for(Tuple2<String, Double> tuple : totalByCountry) {
|
||||
if (tuple._1.equals("Mexico")) {
|
||||
assertEquals(Double.valueOf(99164), tuple._2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.differences.rdd;
|
||||
|
||||
import static org.apache.spark.sql.functions.col;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.spark.sql.DataFrameReader;
|
||||
import org.apache.spark.sql.Dataset;
|
||||
import org.apache.spark.sql.Row;
|
||||
import org.apache.spark.sql.SparkSession;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DataFrameUnitTest {
|
||||
private static SparkSession session;
|
||||
private static Dataset<Row> data;
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
session = SparkSession.builder()
|
||||
.appName("TouristDataFrameExample")
|
||||
.master("local[*]")
|
||||
.getOrCreate();
|
||||
DataFrameReader dataFrameReader = session.read();
|
||||
data = dataFrameReader.option("header", "true")
|
||||
.csv("data/Tourist.csv");
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() {
|
||||
session.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSelectSpecificColumns_thenColumnsFiltered() {
|
||||
Dataset<Row> selectedData = data.select(col("country"), col("year"), col("value"));
|
||||
selectedData.show();
|
||||
|
||||
List<String> resultList = Arrays.asList(selectedData.columns());
|
||||
assertTrue(resultList.contains("country"));
|
||||
assertTrue(resultList.contains("year"));
|
||||
assertTrue(resultList.contains("value"));
|
||||
assertFalse(resultList.contains("Series"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilteringByCountry_thenCountryRecordsSelected() {
|
||||
Dataset<Row> filteredData = data.filter(col("country").equalTo("Mexico"));
|
||||
filteredData.show();
|
||||
|
||||
filteredData.foreach(record -> {
|
||||
assertEquals("Mexico", record.get(1));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGroupCountByCountry_thenContryTotalRecords() {
|
||||
Dataset<Row> recordsPerCountry = data.groupBy(col("country"))
|
||||
.count();
|
||||
recordsPerCountry.show();
|
||||
|
||||
Dataset<Row> filteredData = recordsPerCountry.filter(col("country").equalTo("Sweden"));
|
||||
assertEquals(new Long(12), filteredData.first()
|
||||
.get(1));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.baeldung.differences.rdd;
|
||||
|
||||
import static org.apache.spark.sql.functions.col;
|
||||
import static org.apache.spark.sql.functions.sum;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.apache.spark.api.java.function.FilterFunction;
|
||||
import org.apache.spark.sql.DataFrameReader;
|
||||
import org.apache.spark.sql.Dataset;
|
||||
import org.apache.spark.sql.Encoders;
|
||||
import org.apache.spark.sql.Row;
|
||||
import org.apache.spark.sql.SparkSession;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.differences.dataframe.dataset.rdd.TouristData;
|
||||
|
||||
public class DatasetUnitTest {
|
||||
private static SparkSession session;
|
||||
private static Dataset<TouristData> typedDataset;
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
session = SparkSession.builder()
|
||||
.appName("TouristDatasetExample")
|
||||
.master("local[*]")
|
||||
.getOrCreate();
|
||||
DataFrameReader dataFrameReader = session.read();
|
||||
Dataset<Row> data = dataFrameReader.option("header", "true")
|
||||
.csv("data/Tourist.csv");
|
||||
Dataset<Row> responseWithSelectedColumns = data.select(col("region"),
|
||||
col("country"), col("year"), col("series"), col("value").cast("double"),
|
||||
col("footnotes"), col("source"));
|
||||
typedDataset = responseWithSelectedColumns.as(Encoders.bean(TouristData.class));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() {
|
||||
session.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilteringByCountry_thenCountryRecordsSelected() {
|
||||
Dataset<TouristData> selectedData = typedDataset
|
||||
.filter((FilterFunction<TouristData>) record -> record.getCountry()
|
||||
.equals("Norway"));
|
||||
selectedData.show();
|
||||
|
||||
selectedData.foreach(record -> {
|
||||
assertEquals("Norway", record.getCountry());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGroupCountByCountry_thenContryTotalRecords() {
|
||||
Dataset<Row> countriesCount = typedDataset.groupBy(typedDataset.col("country"))
|
||||
.count();
|
||||
countriesCount.show();
|
||||
|
||||
assertEquals(Long.valueOf(220), Long.valueOf(countriesCount.count()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilteredByPropertyRange_thenRetreiveValidRecords() {
|
||||
// Filter records with existing data for years between 2010 and 2017
|
||||
typedDataset.filter((FilterFunction<TouristData>) record -> record.getYear() != null
|
||||
&& (Long.valueOf(record.getYear()) > 2010 && Long.valueOf(record.getYear()) < 2017))
|
||||
.show();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSumValue_thenRetreiveTotalValue() {
|
||||
// Total tourist expenditure by country
|
||||
typedDataset.filter((FilterFunction<TouristData>) record -> record.getValue() != null
|
||||
&& record.getSeries()
|
||||
.contains("expenditure"))
|
||||
.groupBy("country")
|
||||
.agg(sum("value"))
|
||||
.show();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.differences.rdd;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.spark.SparkConf;
|
||||
import org.apache.spark.api.java.JavaRDD;
|
||||
import org.apache.spark.api.java.JavaSparkContext;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TransformationsUnitTest {
|
||||
|
||||
public static final String COMMA_DELIMITER = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
|
||||
private static JavaSparkContext sc;
|
||||
private static JavaRDD<String> tourists;
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
SparkConf conf = new SparkConf().setAppName("uppercaseCountries")
|
||||
.setMaster("local[*]");
|
||||
sc = new JavaSparkContext(conf);
|
||||
tourists = sc.textFile("data/Tourist.csv")
|
||||
.filter(line -> !line.startsWith("Region")); //filter header row
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() {
|
||||
sc.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMapUpperCase_thenCountryNameUppercased() {
|
||||
JavaRDD<String> upperCaseCountries = tourists.map(line -> {
|
||||
String[] columns = line.split(COMMA_DELIMITER);
|
||||
return columns[1].toUpperCase();
|
||||
})
|
||||
.distinct();
|
||||
|
||||
upperCaseCountries.saveAsTextFile("data/output/uppercase.txt");
|
||||
|
||||
upperCaseCountries.foreach(country -> {
|
||||
//replace non alphanumerical characters
|
||||
country = country.replaceAll("[^a-zA-Z]", "");
|
||||
assertTrue(StringUtils.isAllUpperCase(country));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilterByCountry_thenShowRequestedCountryRecords() {
|
||||
JavaRDD<String> touristsInMexico = tourists.filter(line -> line.split(COMMA_DELIMITER)[1].equals("Mexico"));
|
||||
|
||||
touristsInMexico.saveAsTextFile("data/output/touristInMexico.txt");
|
||||
|
||||
touristsInMexico.foreach(record -> {
|
||||
assertEquals("Mexico", record.split(COMMA_DELIMITER)[1]);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,3 +5,4 @@ This module contains articles about AWS Lambda
|
||||
### Relevant Articles:
|
||||
- [Using AWS Lambda with API Gateway](https://www.baeldung.com/aws-lambda-api-gateway)
|
||||
- [Introduction to AWS Serverless Application Model](https://www.baeldung.com/aws-serverless)
|
||||
- [How to Implement Hibernate in an AWS Lambda Function in Java](https://www.baeldung.com/java-aws-lambda-hibernate)
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>aws-lambda-examples</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>aws-lambda-examples</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-dynamodb</artifactId>
|
||||
<version>${aws-java-sdk.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-core</artifactId>
|
||||
<version>${aws-java-sdk.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-core</artifactId>
|
||||
<version>${aws-lambda-java-core.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-events</artifactId>
|
||||
<version>${aws-lambda-java-events.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.json-simple</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
<version>${json-simple.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${maven-shade-plugin.version}</version>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<json-simple.version>1.1.1</json-simple.version>
|
||||
<commons-io.version>2.5</commons-io.version>
|
||||
<aws-lambda-java-events.version>1.3.0</aws-lambda-java-events.version>
|
||||
<aws-lambda-java-core.version>1.2.0</aws-lambda-java-core.version>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
<aws-java-sdk.version>1.11.241</aws-java-sdk.version>
|
||||
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+7
-83
@@ -5,95 +5,19 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>aws-lambda</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>aws-lambda</name>
|
||||
<packaging>jar</packaging>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-dynamodb</artifactId>
|
||||
<version>${aws-java-sdk.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-core</artifactId>
|
||||
<version>${aws-java-sdk.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-core</artifactId>
|
||||
<version>${aws-lambda-java-core.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-events</artifactId>
|
||||
<version>${aws-lambda-java-events.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.json-simple</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
<version>${json-simple.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<modules>
|
||||
<module>lambda</module>
|
||||
<module>shipping-tracker/ShippingFunction</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${maven-shade-plugin.version}</version>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<json-simple.version>1.1.1</json-simple.version>
|
||||
<commons-io.version>2.5</commons-io.version>
|
||||
<aws-lambda-java-events.version>1.3.0</aws-lambda-java-events.version>
|
||||
<aws-lambda-java-core.version>1.2.0</aws-lambda-java-core.version>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
<aws-java-sdk.version>1.11.241</aws-java-sdk.version>
|
||||
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
.aws-sam/
|
||||
@@ -0,0 +1,73 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>ShippingFunction</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>Shipping Tracker Lambda Function</name>
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<hibernate.version>5.4.21.Final</hibernate.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-core</artifactId>
|
||||
<version>1.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-lambda-java-events</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.11.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-hikaricp</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.2.16</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.1.1</version>
|
||||
<configuration>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
package com.baeldung.lambda.shipping;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.Context;
|
||||
import com.amazonaws.services.lambda.runtime.RequestHandler;
|
||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
|
||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hibernate.cfg.AvailableSettings.*;
|
||||
import static org.hibernate.cfg.AvailableSettings.PASS;
|
||||
|
||||
/**
|
||||
* Handler for requests to Lambda function.
|
||||
*/
|
||||
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
|
||||
try (SessionFactory sessionFactory = createSessionFactory()) {
|
||||
ShippingService service = new ShippingService(sessionFactory, new ShippingDao());
|
||||
return routeRequest(input, service);
|
||||
}
|
||||
}
|
||||
|
||||
private APIGatewayProxyResponseEvent routeRequest(APIGatewayProxyRequestEvent input, ShippingService service) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Content-Type", "application/json");
|
||||
headers.put("X-Custom-Header", "application/json");
|
||||
|
||||
Object result = "OK";
|
||||
|
||||
switch (input.getResource()) {
|
||||
case "/consignment":
|
||||
result = service.createConsignment(
|
||||
fromJson(input.getBody(), Consignment.class));
|
||||
break;
|
||||
case "/consignment/{id}":
|
||||
result = service.view(input.getPathParameters().get("id"));
|
||||
break;
|
||||
case "/consignment/{id}/item":
|
||||
service.addItem(input.getPathParameters().get("id"),
|
||||
fromJson(input.getBody(), Item.class));
|
||||
break;
|
||||
case "/consignment/{id}/checkin":
|
||||
service.checkIn(input.getPathParameters().get("id"),
|
||||
fromJson(input.getBody(), Checkin.class));
|
||||
break;
|
||||
}
|
||||
|
||||
return new APIGatewayProxyResponseEvent()
|
||||
.withHeaders(headers)
|
||||
.withStatusCode(200)
|
||||
.withBody(toJson(result));
|
||||
}
|
||||
|
||||
private static <T> String toJson(T object) {
|
||||
try {
|
||||
return OBJECT_MAPPER.writeValueAsString(object);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> T fromJson(String json, Class<T> type) {
|
||||
try {
|
||||
return OBJECT_MAPPER.readValue(json, type);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static SessionFactory createSessionFactory() {
|
||||
Map<String, String> settings = new HashMap<>();
|
||||
settings.put(URL, System.getenv("DB_URL"));
|
||||
settings.put(DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
|
||||
settings.put(DEFAULT_SCHEMA, "shipping");
|
||||
settings.put(DRIVER, "org.postgresql.Driver");
|
||||
settings.put(USER, System.getenv("DB_USER"));
|
||||
settings.put(PASS, System.getenv("DB_PASSWORD"));
|
||||
settings.put("hibernate.hikari.connectionTimeout", "20000");
|
||||
settings.put("hibernate.hikari.minimumIdle", "1");
|
||||
settings.put("hibernate.hikari.maximumPoolSize", "2");
|
||||
settings.put("hibernate.hikari.idleTimeout", "30000");
|
||||
|
||||
// commented out as we only need them on first use
|
||||
// settings.put(HBM2DDL_AUTO, "create-only");
|
||||
// settings.put(HBM2DDL_DATABASE_ACTION, "create");
|
||||
|
||||
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||
.applySettings(settings)
|
||||
.build();
|
||||
|
||||
return new MetadataSources(registry)
|
||||
.addAnnotatedClass(Consignment.class)
|
||||
.addAnnotatedClass(Item.class)
|
||||
.addAnnotatedClass(Checkin.class)
|
||||
.buildMetadata()
|
||||
.buildSessionFactory();
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.lambda.shipping;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Checkin {
|
||||
private String timeStamp;
|
||||
private String location;
|
||||
|
||||
@Column(name = "timestamp")
|
||||
public String getTimeStamp() {
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
public void setTimeStamp(String timeStamp) {
|
||||
this.timeStamp = timeStamp;
|
||||
}
|
||||
|
||||
@Column(name = "location")
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user