Merge branch 'eugenp:master' into master
This commit is contained in:
commit
5e53164431
|
@ -31,6 +31,6 @@ public class BalancedBracketsUsingDeque {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return deque.isEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -49,6 +49,12 @@ public class BalancedBracketsUsingDequeUnitTest {
|
||||||
assertThat(result).isFalse();
|
assertThat(result).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnotherEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
|
||||||
|
boolean result = balancedBracketsUsingDeque.isBalanced("{{}(");
|
||||||
|
assertThat(result).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
|
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
|
||||||
boolean result = balancedBracketsUsingDeque.isBalanced("{[()]}");
|
boolean result = balancedBracketsUsingDeque.isBalanced("{[()]}");
|
||||||
|
|
|
@ -49,6 +49,12 @@ public class BalancedBracketsUsingStringUnitTest {
|
||||||
assertThat(result).isFalse();
|
assertThat(result).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnotherEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
|
||||||
|
boolean result = balancedBracketsUsingString.isBalanced("{{}(");
|
||||||
|
assertThat(result).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
|
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
|
||||||
boolean result = balancedBracketsUsingString.isBalanced("{[()]}");
|
boolean result = balancedBracketsUsingString.isBalanced("{[()]}");
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
id,zoneId,FIRST_NAME,MIDDLE_NAME,LAST_NAME,CITY,gender,transaction_date,transaction_amount
|
||||||
|
1,EU11,Ana1,A,MN, London,Female,1/2/2018,5200
|
||||||
|
2,EU12,Ana2,A,MN, London,Female,29/3/2018,1000
|
||||||
|
3,EU13,Jack1,Josh,MN, London,Male,19/6/2018,9600
|
||||||
|
4,EU14,Jack2,Josh,MN, London,Male,9/9/2018,1000
|
||||||
|
5,EU15,Nick1,H,Dee,London,Male,9/6/2021,3000
|
||||||
|
6,EU16,Nick2,H,Dee,London,Male,27/9/2021,500
|
||||||
|
7,EU17,Nick3,H,Dee,London,Male,8/12/2021,500
|
||||||
|
8,EU18,Sara1,H,Dee,London,Female,7/4/2021,2000
|
|
|
@ -0,0 +1,114 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"zoneId": "LONDON",
|
||||||
|
"customerId": 1,
|
||||||
|
"gender": "Female",
|
||||||
|
"name": "jane austin",
|
||||||
|
"contact": {
|
||||||
|
"street_Address": "XYZ Road",
|
||||||
|
"address2": "house 47",
|
||||||
|
"zipcode": "MK1110",
|
||||||
|
"county": "DCounty",
|
||||||
|
"phone_number": "(919) 403-0025",
|
||||||
|
"customer_city": "CityA"
|
||||||
|
},
|
||||||
|
"transaction_date": "2021-02-05",
|
||||||
|
"transaction_amount": 15000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"zoneId": "LONDON",
|
||||||
|
"customerId": 2,
|
||||||
|
"gender": "Male",
|
||||||
|
"name": "Jack Trevor",
|
||||||
|
"contact": {
|
||||||
|
"street_Address": " kingfisher road",
|
||||||
|
"address2": "SUITE 110",
|
||||||
|
"zipcode": "HM1190",
|
||||||
|
"county": "CCounty",
|
||||||
|
"phone_number": "(919) 403-0025",
|
||||||
|
"customer_city": "CityB"
|
||||||
|
},
|
||||||
|
"transaction_date": "2021-02-05",
|
||||||
|
"transaction_amount": 12000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"zoneId": "LONDON",
|
||||||
|
"customerId": 11,
|
||||||
|
"gender": "Female",
|
||||||
|
"name": "jane1 austin",
|
||||||
|
"contact": {
|
||||||
|
"street_Address": "B Road",
|
||||||
|
"address2": "house 47",
|
||||||
|
"zipcode": "MK1110",
|
||||||
|
"county": "BCounty",
|
||||||
|
"phone_number": "(919) 403-0025",
|
||||||
|
"customer_city": "CityA"
|
||||||
|
},
|
||||||
|
"transaction_date": "2021-02-05",
|
||||||
|
"transaction_amount": 1000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"zoneId": "LONDON",
|
||||||
|
"customerId": 21,
|
||||||
|
"gender": "Male",
|
||||||
|
"name": "Jack1 Trevor",
|
||||||
|
"contact": {
|
||||||
|
"street_Address": " A road",
|
||||||
|
"address2": "SUITE 777",
|
||||||
|
"zipcode": "AZ890",
|
||||||
|
"county": "ACounty",
|
||||||
|
"phone_number": "(919) 403-0025",
|
||||||
|
"customer_city": "CityB"
|
||||||
|
},
|
||||||
|
"transaction_date": "2021-02-05",
|
||||||
|
"transaction_amount": 1000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"zoneId": "Wales",
|
||||||
|
"customerId": 3,
|
||||||
|
"gender": "Male",
|
||||||
|
"name": "John Jack",
|
||||||
|
"contact": {
|
||||||
|
"street_Address": "sunny croft",
|
||||||
|
"address2": "SUITE 1",
|
||||||
|
"zipcode": "SN1030",
|
||||||
|
"county": "bucks",
|
||||||
|
"phone_number": "(919) 403-0025",
|
||||||
|
"customer_city": "Cardiff"
|
||||||
|
},
|
||||||
|
"transaction_date": "2018-02-05",
|
||||||
|
"transaction_amount": 5000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"zoneId": "LONDON",
|
||||||
|
"customerId": 4,
|
||||||
|
"gender": "Male",
|
||||||
|
"name": "Jack Trevor",
|
||||||
|
"contact": {
|
||||||
|
"street_Address": " kingfisher road",
|
||||||
|
"address2": "SUITE 110",
|
||||||
|
"zipcode": "HM1190",
|
||||||
|
"county": "Hampshire",
|
||||||
|
"phone_number": "(919) 403-0025",
|
||||||
|
"customer_city": "CityB"
|
||||||
|
},
|
||||||
|
"transaction_date": "2021-02-05",
|
||||||
|
"transaction_amount": 500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"zoneId": "Wales",
|
||||||
|
"customerId": 5,
|
||||||
|
"gender": "Male",
|
||||||
|
"name": "John Jack",
|
||||||
|
"contact": {
|
||||||
|
"street_Address": "sunny croft",
|
||||||
|
"address2": "SUITE 1",
|
||||||
|
"zipcode": "SN1030",
|
||||||
|
"county": "ECounty",
|
||||||
|
"phone_number": "(919) 403-0025",
|
||||||
|
"customer_city": "Cardiff"
|
||||||
|
},
|
||||||
|
"transaction_date": "2018-01-25",
|
||||||
|
"transaction_amount": 500
|
||||||
|
}
|
||||||
|
]
|
|
@ -0,0 +1,20 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id": "1",
|
||||||
|
"gender": "Female",
|
||||||
|
"name": "Jo",
|
||||||
|
"transaction_amount": 200
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id":"2",
|
||||||
|
"gender": "Male",
|
||||||
|
"name": "Mike",
|
||||||
|
"transaction_amount": 500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "3",
|
||||||
|
"gender": "Male",
|
||||||
|
"name": "Dave",
|
||||||
|
"transaction_amount": 5000
|
||||||
|
}
|
||||||
|
]
|
|
@ -0,0 +1,33 @@
|
||||||
|
version: "3"
|
||||||
|
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:12.3-alpine
|
||||||
|
restart: always
|
||||||
|
environment:
|
||||||
|
POSTGRES_PASSWORD: postgres
|
||||||
|
POSTGRES_USER: postgres
|
||||||
|
expose:
|
||||||
|
- 5432
|
||||||
|
ports:
|
||||||
|
- 5432:5432
|
||||||
|
command: -p 5432
|
||||||
|
volumes:
|
||||||
|
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
|
||||||
|
# - postgres:/var/lib/postgresql/data
|
||||||
|
|
||||||
|
pgadmin:
|
||||||
|
image: dpage/pgadmin4:4.23
|
||||||
|
environment:
|
||||||
|
PGADMIN_DEFAULT_EMAIL: admin@pgadmin.com
|
||||||
|
PGADMIN_DEFAULT_PASSWORD: password
|
||||||
|
PGADMIN_LISTEN_PORT: 80
|
||||||
|
ports:
|
||||||
|
- 15432:80
|
||||||
|
volumes:
|
||||||
|
- pgadmin:/var/lib/pgadmin
|
||||||
|
depends_on:
|
||||||
|
- postgres
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
pgadmin:
|
|
@ -0,0 +1 @@
|
||||||
|
CREATE DATABASE customerdb;
|
|
@ -20,37 +20,31 @@
|
||||||
<groupId>org.apache.spark</groupId>
|
<groupId>org.apache.spark</groupId>
|
||||||
<artifactId>spark-core_2.11</artifactId>
|
<artifactId>spark-core_2.11</artifactId>
|
||||||
<version>${org.apache.spark.spark-core.version}</version>
|
<version>${org.apache.spark.spark-core.version}</version>
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.spark</groupId>
|
<groupId>org.apache.spark</groupId>
|
||||||
<artifactId>spark-sql_2.11</artifactId>
|
<artifactId>spark-sql_2.11</artifactId>
|
||||||
<version>${org.apache.spark.spark-sql.version}</version>
|
<version>${org.apache.spark.spark-sql.version}</version>
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.spark</groupId>
|
<groupId>org.apache.spark</groupId>
|
||||||
<artifactId>spark-graphx_2.11</artifactId>
|
<artifactId>spark-graphx_2.11</artifactId>
|
||||||
<version>${org.apache.spark.spark-graphx.version}</version>
|
<version>${org.apache.spark.spark-graphx.version}</version>
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>graphframes</groupId>
|
<groupId>graphframes</groupId>
|
||||||
<artifactId>graphframes</artifactId>
|
<artifactId>graphframes</artifactId>
|
||||||
<version>${graphframes.version}</version>
|
<version>${graphframes.version}</version>
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.spark</groupId>
|
<groupId>org.apache.spark</groupId>
|
||||||
<artifactId>spark-streaming_2.11</artifactId>
|
<artifactId>spark-streaming_2.11</artifactId>
|
||||||
<version>${org.apache.spark.spark-streaming.version}</version>
|
<version>${org.apache.spark.spark-streaming.version}</version>
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.spark</groupId>
|
<groupId>org.apache.spark</groupId>
|
||||||
<artifactId>spark-mllib_2.11</artifactId>
|
<artifactId>spark-mllib_2.11</artifactId>
|
||||||
<version>${org.apache.spark.spark-mllib.version}</version>
|
<version>${org.apache.spark.spark-mllib.version}</version>
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.spark</groupId>
|
<groupId>org.apache.spark</groupId>
|
||||||
|
@ -67,6 +61,11 @@
|
||||||
<artifactId>spark-cassandra-connector-java_2.11</artifactId>
|
<artifactId>spark-cassandra-connector-java_2.11</artifactId>
|
||||||
<version>${com.datastax.spark.spark-cassandra-connector-java.version}</version>
|
<version>${com.datastax.spark.spark-cassandra-connector-java.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.postgresql</groupId>
|
||||||
|
<artifactId>postgresql</artifactId>
|
||||||
|
<version>${postgres.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -108,6 +107,7 @@
|
||||||
<org.apache.spark.spark-streaming-kafka.version>2.4.8</org.apache.spark.spark-streaming-kafka.version>
|
<org.apache.spark.spark-streaming-kafka.version>2.4.8</org.apache.spark.spark-streaming-kafka.version>
|
||||||
<com.datastax.spark.spark-cassandra-connector.version>2.5.2</com.datastax.spark.spark-cassandra-connector.version>
|
<com.datastax.spark.spark-cassandra-connector.version>2.5.2</com.datastax.spark.spark-cassandra-connector.version>
|
||||||
<com.datastax.spark.spark-cassandra-connector-java.version>1.6.0-M1</com.datastax.spark.spark-cassandra-connector-java.version>
|
<com.datastax.spark.spark-cassandra-connector-java.version>1.6.0-M1</com.datastax.spark.spark-cassandra-connector-java.version>
|
||||||
|
<postgres.version>42.3.3</postgres.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,52 @@
|
||||||
|
package com.baeldung.dataframes;
|
||||||
|
|
||||||
|
public class Customer {
|
||||||
|
String id;
|
||||||
|
String name;
|
||||||
|
String gender;
|
||||||
|
int transaction_amount;
|
||||||
|
|
||||||
|
public Customer() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Customer(String id, String name, String gender, int transaction_amount) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.gender = gender;
|
||||||
|
this.transaction_amount = transaction_amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGender() {
|
||||||
|
return gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGender(String gender) {
|
||||||
|
this.gender = gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTransaction_amount() {
|
||||||
|
return transaction_amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTransaction_amount(int transaction_amount) {
|
||||||
|
this.transaction_amount = transaction_amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,129 @@
|
||||||
|
package com.baeldung.dataframes;
|
||||||
|
|
||||||
|
import static org.apache.spark.sql.functions.col;
|
||||||
|
import static org.apache.spark.sql.functions.column;
|
||||||
|
import static org.apache.spark.sql.functions.concat;
|
||||||
|
import static org.apache.spark.sql.functions.lit;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.apache.spark.sql.Dataset;
|
||||||
|
import org.apache.spark.sql.Row;
|
||||||
|
import org.apache.spark.sql.SaveMode;
|
||||||
|
import org.apache.spark.sql.SparkSession;
|
||||||
|
import org.apache.spark.sql.functions;
|
||||||
|
|
||||||
|
public class CustomerDataAggregationPipeline {
|
||||||
|
private static final SparkSession SPARK_SESSION = SparkDriver.getSparkSession();
|
||||||
|
|
||||||
|
private final Properties dbProperties;
|
||||||
|
|
||||||
|
public CustomerDataAggregationPipeline(Properties properties) {
|
||||||
|
dbProperties = properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// replace with actual DB properties
|
||||||
|
Properties dbProps = new Properties();
|
||||||
|
dbProps.setProperty("connectionURL", "jdbc:postgresql://localhost:5432/customerdb");
|
||||||
|
dbProps.setProperty("driver", "org.postgresql.Driver");
|
||||||
|
dbProps.setProperty("user", "postgres");
|
||||||
|
dbProps.setProperty("password", "postgres");
|
||||||
|
|
||||||
|
new CustomerDataAggregationPipeline(dbProps).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
Dataset<Row> ebayDFRaw = ingestCustomerDataFromEbay();
|
||||||
|
Dataset<Row> ebayDf = normalizeCustomerDataFromEbay(ebayDFRaw);
|
||||||
|
|
||||||
|
Dataset<Row> amazonDFRaw = ingestCustomerDataFromAmazon();
|
||||||
|
Dataset<Row> amazonDf = normalizeCustomerDataFromAmazon(amazonDFRaw);
|
||||||
|
|
||||||
|
Dataset<Row> combineDataframes = combineDataframes(ebayDf, amazonDf);
|
||||||
|
|
||||||
|
Dataset<Row> rowDataset = aggregateYearlySalesByGender(combineDataframes);
|
||||||
|
|
||||||
|
exportData(rowDataset);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dataset<Row> ingestCustomerDataFromAmazon() {
|
||||||
|
return SPARK_SESSION.read()
|
||||||
|
.format("csv")
|
||||||
|
.option("header", "true")
|
||||||
|
.schema(SchemaFactory.customerSchema())
|
||||||
|
.option("dateFormat", "m/d/YYYY")
|
||||||
|
.load("data/customerData.csv");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dataset<Row> ingestCustomerDataFromEbay() {
|
||||||
|
return SPARK_SESSION.read()
|
||||||
|
.format("org.apache.spark.sql.execution.datasources.json.JsonFileFormat")
|
||||||
|
.option("multiline", true)
|
||||||
|
.load("data/customerData.json");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dataset<Row> combineDataframes(Dataset<Row> df1, Dataset<Row> df2) {
|
||||||
|
return df1.unionByName(df2);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dataset<Row> normalizeCustomerDataFromEbay(Dataset<Row> rawDataset) {
|
||||||
|
Dataset<Row> transformedDF = rawDataset.withColumn("id", concat(rawDataset.col("zoneId"), lit("-"), rawDataset.col("customerId")))
|
||||||
|
.drop(column("customerId"))
|
||||||
|
.withColumn("source", lit("ebay"))
|
||||||
|
.withColumn("city", rawDataset.col("contact.customer_city"))
|
||||||
|
.drop(column("contact"))
|
||||||
|
.drop(column("zoneId"))
|
||||||
|
.withColumn("year", functions.year(col("transaction_date")))
|
||||||
|
.drop("transaction_date")
|
||||||
|
.withColumn("firstName", functions.split(column("name"), " ")
|
||||||
|
.getItem(0))
|
||||||
|
.withColumn("lastName", functions.split(column("name"), " ")
|
||||||
|
.getItem(1))
|
||||||
|
.drop(column("name"));
|
||||||
|
|
||||||
|
print(transformedDF);
|
||||||
|
return transformedDF;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dataset<Row> normalizeCustomerDataFromAmazon(Dataset<Row> rawDataset) {
|
||||||
|
|
||||||
|
Dataset<Row> transformedDF = rawDataset.withColumn("id", concat(rawDataset.col("zoneId"), lit("-"), rawDataset.col("id")))
|
||||||
|
.withColumn("source", lit("amazon"))
|
||||||
|
.withColumnRenamed("CITY", "city")
|
||||||
|
.withColumnRenamed("PHONE_NO", "contactNo")
|
||||||
|
.withColumnRenamed("POSTCODE", "postCode")
|
||||||
|
.withColumnRenamed("FIRST_NAME", "firstName")
|
||||||
|
.drop(column("MIDDLE_NAME"))
|
||||||
|
.drop(column("zoneId"))
|
||||||
|
.withColumnRenamed("LAST_NAME", "lastName")
|
||||||
|
.withColumn("year", functions.year(col("transaction_date")))
|
||||||
|
.drop("transaction_date");
|
||||||
|
|
||||||
|
print(transformedDF);
|
||||||
|
return transformedDF;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dataset<Row> aggregateYearlySalesByGender(Dataset<Row> dataset) {
|
||||||
|
|
||||||
|
Dataset<Row> aggDF = dataset.groupBy(column("year"), column("source"), column("gender"))
|
||||||
|
.sum("transaction_amount")
|
||||||
|
.withColumnRenamed("sum(transaction_amount)", "annual_spending")
|
||||||
|
.orderBy(col("year").asc(), col("annual_spending").desc());
|
||||||
|
|
||||||
|
print(aggDF);
|
||||||
|
return aggDF;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void print(Dataset<Row> aggDs) {
|
||||||
|
aggDs.show();
|
||||||
|
aggDs.printSchema();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void exportData(Dataset<Row> dataset) {
|
||||||
|
String connectionURL = dbProperties.getProperty("connectionURL");
|
||||||
|
dataset.write()
|
||||||
|
.mode(SaveMode.Overwrite)
|
||||||
|
.jdbc(connectionURL, "customer", dbProperties);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
package com.baeldung.dataframes;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.apache.spark.sql.Dataset;
|
||||||
|
import org.apache.spark.sql.Row;
|
||||||
|
|
||||||
|
public class CustomerToDataFrameConverterApp {
|
||||||
|
|
||||||
|
private static final List<Customer> CUSTOMERS = Arrays.asList(
|
||||||
|
aCustomerWith("01", "jo", "Female", 2000),
|
||||||
|
aCustomerWith("02", "jack", "Male", 1200)
|
||||||
|
);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Dataset<Row> dataFrame = convertAfterMappingRows(CUSTOMERS);
|
||||||
|
print(dataFrame);
|
||||||
|
Dataset<Row> customerDF = convertToDataFrameWithNoChange();
|
||||||
|
print(customerDF);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Dataset<Row> convertToDataFrameWithNoChange() {
|
||||||
|
return SparkDriver.getSparkSession().createDataFrame(CUSTOMERS, Customer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Dataset<Row> convertAfterMappingRows(List<Customer> customer) {
|
||||||
|
List<Row> rows = customer.stream()
|
||||||
|
.map(c -> new CustomerToRowMapper().call(c))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
return SparkDriver.getSparkSession()
|
||||||
|
.createDataFrame(rows, SchemaFactory.minimumCustomerDataSchema());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Customer aCustomerWith(String id, String name, String gender, int amount) {
|
||||||
|
return new Customer(id, name, gender, amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void print(Dataset<Row> dataFrame) {
|
||||||
|
dataFrame.printSchema();
|
||||||
|
dataFrame.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.dataframes;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.spark.api.java.function.MapFunction;
|
||||||
|
import org.apache.spark.sql.Row;
|
||||||
|
import org.apache.spark.sql.RowFactory;
|
||||||
|
|
||||||
|
public class CustomerToRowMapper implements MapFunction<Customer, Row> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Row call(Customer customer) {
|
||||||
|
Row row = RowFactory.create(
|
||||||
|
customer.getId(), customer.getName().toUpperCase(),
|
||||||
|
StringUtils.substring(customer.getGender(),0, 1),
|
||||||
|
customer.getTransaction_amount());
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.dataframes;
|
||||||
|
|
||||||
|
import org.apache.spark.sql.Dataset;
|
||||||
|
import org.apache.spark.sql.Encoders;
|
||||||
|
import org.apache.spark.sql.Row;
|
||||||
|
|
||||||
|
public class DataFrameToCustomerConverterApp {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Dataset<Row> df = SparkDriver.getSparkSession()
|
||||||
|
.read()
|
||||||
|
.format("org.apache.spark.sql.execution.datasources.json.JsonFileFormat")
|
||||||
|
.option("multiline", true)
|
||||||
|
.load("data/minCustomerData.json");
|
||||||
|
df.show();
|
||||||
|
df.printSchema();
|
||||||
|
Dataset<Customer> customerDS = df.map(new RowToCustomerMapper(), Encoders.bean(Customer. class));
|
||||||
|
customerDS.show();
|
||||||
|
customerDS.printSchema();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
package com.baeldung.dataframes;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.apache.spark.sql.Dataset;
|
||||||
|
import org.apache.spark.sql.Encoders;
|
||||||
|
import org.apache.spark.sql.Row;
|
||||||
|
import org.apache.spark.sql.SparkSession;
|
||||||
|
|
||||||
|
public class DataSetToDataFrameConverterApp {
|
||||||
|
|
||||||
|
private static final SparkSession SPARK_SESSION = SparkDriver.getSparkSession();
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Dataset<Customer> customerDataset = convertToDataSetFromPOJO();
|
||||||
|
Dataset<Row> customerDataFrame = customerDataset.toDF();
|
||||||
|
print(customerDataFrame);
|
||||||
|
|
||||||
|
List<String> names = getNames();
|
||||||
|
Dataset<String> namesDataset = convertToDataSetFromStrings(names);
|
||||||
|
Dataset<Row> namesDataFrame = namesDataset.toDF();
|
||||||
|
print(namesDataFrame);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dataset<String> convertToDataSetFromStrings(List<String> names) {
|
||||||
|
return SPARK_SESSION.createDataset(names, Encoders.STRING());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dataset<Customer> convertToDataSetFromPOJO() {
|
||||||
|
return SPARK_SESSION.createDataset(CUSTOMERS, Encoders.bean(Customer.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final List<Customer> CUSTOMERS = Arrays.asList(
|
||||||
|
aCustomerWith("01", "jo", "Female", 2000),
|
||||||
|
aCustomerWith("02", "jack", "Female", 1200),
|
||||||
|
aCustomerWith("03", "ash", "male", 2000),
|
||||||
|
aCustomerWith("04", "emma", "Female", 2000)
|
||||||
|
);
|
||||||
|
|
||||||
|
private static List<String> getNames() {
|
||||||
|
return CUSTOMERS.stream()
|
||||||
|
.map(Customer::getName)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void print(Dataset<Row> df) {
|
||||||
|
df.show();
|
||||||
|
df.printSchema();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Customer aCustomerWith(String id, String name, String gender, int amount) {
|
||||||
|
return new Customer(id, name, gender, amount);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.dataframes;
|
||||||
|
|
||||||
|
import org.apache.spark.api.java.function.MapFunction;
|
||||||
|
import org.apache.spark.sql.Row;
|
||||||
|
|
||||||
|
class RowToCustomerMapper implements MapFunction<Row, Customer> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Customer call(Row row) {
|
||||||
|
|
||||||
|
Customer customer = new Customer();
|
||||||
|
customer.setId(row.getAs("id"));
|
||||||
|
customer.setName(row.getAs("name"));
|
||||||
|
customer.setGender(row.getAs("gender"));
|
||||||
|
customer.setTransaction_amount(Math.toIntExact(row.getAs("transaction_amount")));
|
||||||
|
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.dataframes;
|
||||||
|
|
||||||
|
import org.apache.spark.sql.types.DataTypes;
|
||||||
|
import org.apache.spark.sql.types.StructField;
|
||||||
|
import org.apache.spark.sql.types.StructType;
|
||||||
|
|
||||||
|
public class SchemaFactory {
|
||||||
|
|
||||||
|
public static StructType customerSchema() {
|
||||||
|
return DataTypes.createStructType(
|
||||||
|
new StructField[] { DataTypes.createStructField("id", DataTypes.IntegerType, false),
|
||||||
|
DataTypes.createStructField("zoneId", DataTypes.StringType, false),
|
||||||
|
DataTypes.createStructField("FIRST_NAME", DataTypes.StringType, false),
|
||||||
|
DataTypes.createStructField("MIDDLE_NAME", DataTypes.StringType, false),
|
||||||
|
DataTypes.createStructField("LAST_NAME", DataTypes.StringType, false),
|
||||||
|
DataTypes.createStructField("CITY", DataTypes.StringType, false),
|
||||||
|
DataTypes.createStructField("gender", DataTypes.StringType, false),
|
||||||
|
DataTypes.createStructField("transaction_date", DataTypes.DateType, false),
|
||||||
|
DataTypes.createStructField("transaction_amount", DataTypes.IntegerType, false)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static StructType minimumCustomerDataSchema() {
|
||||||
|
return DataTypes.createStructType(new StructField[] {
|
||||||
|
DataTypes.createStructField("id", DataTypes.StringType, true),
|
||||||
|
DataTypes.createStructField("name", DataTypes.StringType, true),
|
||||||
|
DataTypes.createStructField("gender", DataTypes.StringType, true),
|
||||||
|
DataTypes.createStructField("transaction_amount", DataTypes.IntegerType, true)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.baeldung.dataframes;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.apache.spark.sql.SparkSession;
|
||||||
|
|
||||||
|
public class SparkDriver implements Serializable {
|
||||||
|
|
||||||
|
public static SparkSession getSparkSession() {
|
||||||
|
return SparkSession.builder()
|
||||||
|
.appName("Customer Aggregation pipeline")
|
||||||
|
.master("local")
|
||||||
|
.getOrCreate();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package com.baeldung.dataframes;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class CustomerDataAggregationPipelineLiveTest {
|
||||||
|
|
||||||
|
private static Connection conn;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void beforeAll() throws SQLException {
|
||||||
|
DriverManager.registerDriver(new org.postgresql.Driver());
|
||||||
|
String dbURL1 = "jdbc:postgresql://localhost:5432/customerdb";
|
||||||
|
conn = DriverManager.getConnection(dbURL1, "postgres", "postgres");
|
||||||
|
|
||||||
|
String sql = "drop table if exists customer";
|
||||||
|
|
||||||
|
PreparedStatement statement = conn.prepareStatement(sql);
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenCSVAndJSON_whenRun_thenStoresAggregatedDataFrameInDB() throws Exception {
|
||||||
|
Properties dbProps = new Properties();
|
||||||
|
dbProps.setProperty("connectionURL", "jdbc:postgresql://localhost:5432/customerdb");
|
||||||
|
dbProps.setProperty("driver", "org.postgresql.Driver");
|
||||||
|
dbProps.setProperty("user", "postgres");
|
||||||
|
dbProps.setProperty("password", "postgres");
|
||||||
|
|
||||||
|
CustomerDataAggregationPipeline pipeline = new CustomerDataAggregationPipeline(dbProps);
|
||||||
|
pipeline.run();
|
||||||
|
|
||||||
|
String allCustomersSql = "Select count(*) from customer";
|
||||||
|
|
||||||
|
Statement statement = conn.createStatement();
|
||||||
|
ResultSet resultSet = statement.executeQuery(allCustomersSql);
|
||||||
|
resultSet.next();
|
||||||
|
int count = resultSet.getInt(1);
|
||||||
|
assertEquals(7, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.baeldung.dataframes;
|
||||||
|
|
||||||
|
import static com.baeldung.dataframes.CustomerToDataFrameConverterApp.convertAfterMappingRows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.spark.sql.Dataset;
|
||||||
|
import org.apache.spark.sql.Row;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class CustomerToDataFrameConverterAppUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenCustomers_whenConvertAfterMappingRows_thenConvertsToDataSet() {
|
||||||
|
|
||||||
|
List<Customer> customers = Arrays.asList(
|
||||||
|
new Customer("01", "jo", "Female", 2000),
|
||||||
|
new Customer("02", "jack", "Male", 1200)
|
||||||
|
);
|
||||||
|
|
||||||
|
Dataset<Row> customerDF = convertAfterMappingRows(customers);
|
||||||
|
List<Row> rows = customerDF.collectAsList();
|
||||||
|
Row row1 = rows.get(0);
|
||||||
|
Row row2 = rows.get(1);
|
||||||
|
|
||||||
|
assertEquals("01", row1.get(0));
|
||||||
|
assertEquals( "JO", row1.get(1));
|
||||||
|
assertEquals( "F", row1.get(2));
|
||||||
|
assertEquals( 2000, row1.get(3));
|
||||||
|
|
||||||
|
assertEquals("02", row2.get(0));
|
||||||
|
assertEquals( "JACK", row2.get(1));
|
||||||
|
assertEquals( "M", row2.get(2));
|
||||||
|
assertEquals( 1200, row2.get(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenCustomers_whenConvertWithNoChange_thenConvertsToDataSet() {
|
||||||
|
|
||||||
|
List<Customer> customers = Arrays.asList(
|
||||||
|
new Customer("01", "jo", "Female", 2000),
|
||||||
|
new Customer("02", "jack", "Male", 1200)
|
||||||
|
);
|
||||||
|
|
||||||
|
Dataset<Row> customerDF = CustomerToDataFrameConverterApp.convertToDataFrameWithNoChange();
|
||||||
|
List<Row> rows = customerDF.collectAsList();
|
||||||
|
Row row1 = rows.get(0);
|
||||||
|
Row row2 = rows.get(1);
|
||||||
|
|
||||||
|
assertEquals("01", row1.getAs("id"));
|
||||||
|
assertEquals( "jo", row1.getAs("name"));
|
||||||
|
assertEquals( "Female", row1.getAs("gender"));
|
||||||
|
assertEquals( 2000, (int)row1.getAs("transaction_amount"));
|
||||||
|
|
||||||
|
assertEquals("02", row2.getAs("id"));
|
||||||
|
assertEquals( "jack", row2.getAs("name"));
|
||||||
|
assertEquals( "Male", row2.getAs("gender"));
|
||||||
|
assertEquals( 1200, (int)row2.getAs("transaction_amount"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,6 @@ This module contains modules about core Java
|
||||||
|
|
||||||
## Relevant articles:
|
## Relevant articles:
|
||||||
|
|
||||||
- [Multi-Module Maven Application with Java Modules](https://www.baeldung.com/maven-multi-module-project-java-jpms)
|
|
||||||
- [Understanding the NumberFormatException in Java](https://www.baeldung.com/java-number-format-exception)
|
- [Understanding the NumberFormatException in Java](https://www.baeldung.com/java-number-format-exception)
|
||||||
- [Will an Error Be Caught by Catch Block in Java?](https://www.baeldung.com/java-error-catch)
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.setvslazyset;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
AtomicInteger atomic = new AtomicInteger(0);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Application app = new Application();
|
||||||
|
new Thread(() -> {
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
//app.atomic.set(i);
|
||||||
|
app.atomic.lazySet(i);
|
||||||
|
System.out.println("Set: " + i);
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
new Thread(() -> {
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
synchronized (app.atomic) {
|
||||||
|
int counter = app.atomic.get();
|
||||||
|
System.out.println("Get: " + counter);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -13,4 +13,5 @@ This module contains articles about core java exceptions
|
||||||
- [The StackOverflowError in Java](https://www.baeldung.com/java-stack-overflow-error)
|
- [The StackOverflowError in Java](https://www.baeldung.com/java-stack-overflow-error)
|
||||||
- [Checked and Unchecked Exceptions in Java](https://www.baeldung.com/java-checked-unchecked-exceptions)
|
- [Checked and Unchecked Exceptions in Java](https://www.baeldung.com/java-checked-unchecked-exceptions)
|
||||||
- [Common Java Exceptions](https://www.baeldung.com/java-common-exceptions)
|
- [Common Java Exceptions](https://www.baeldung.com/java-common-exceptions)
|
||||||
|
- [Will an Error Be Caught by Catch Block in Java?](https://www.baeldung.com/java-error-catch)
|
||||||
- [[Next -->]](/core-java-modules/core-java-exceptions-2)
|
- [[Next -->]](/core-java-modules/core-java-exceptions-2)
|
|
@ -9,4 +9,5 @@ This module contains articles about core Java input and output (IO)
|
||||||
- [SequenceInputStream Class in Java](https://www.baeldung.com/java-sequenceinputstream)
|
- [SequenceInputStream Class in Java](https://www.baeldung.com/java-sequenceinputstream)
|
||||||
- [Read a File Into a Map in Java](https://www.baeldung.com/java-read-file-into-map)
|
- [Read a File Into a Map in Java](https://www.baeldung.com/java-read-file-into-map)
|
||||||
- [Read User Input Until a Condition is Met](https://www.baeldung.com/java-read-input-until-condition)
|
- [Read User Input Until a Condition is Met](https://www.baeldung.com/java-read-input-until-condition)
|
||||||
|
- [Java Scanner.skip method with examples](https://www.baeldung.com/java-scanner-skip)
|
||||||
- [[<-- Prev]](/core-java-modules/core-java-io-3)
|
- [[<-- Prev]](/core-java-modules/core-java-io-3)
|
||||||
|
|
|
@ -6,4 +6,5 @@
|
||||||
- [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips)
|
- [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips)
|
||||||
- [Exceptions in Java 8 Lambda Expressions](http://www.baeldung.com/java-lambda-exceptions)
|
- [Exceptions in Java 8 Lambda Expressions](http://www.baeldung.com/java-lambda-exceptions)
|
||||||
- [Method References in Java](https://www.baeldung.com/java-method-references)
|
- [Method References in Java](https://www.baeldung.com/java-method-references)
|
||||||
- [The Double Colon Operator in Java 8](https://www.baeldung.com/java-8-double-colon-operator)
|
- [The Double Colon Operator in Java 8](https://www.baeldung.com/java-8-double-colon-operator)
|
||||||
|
- [Serialize a Lambda in Java](https://www.baeldung.com/java-serialize-lambda)
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
## Core Java Lang (Part 5)
|
||||||
|
|
||||||
|
This module contains articles about core features in the Java language
|
||||||
|
|
||||||
|
## TODO ##
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?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>core-java-lang-5</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-lang-5</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>core-java-lang-5</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,125 @@
|
||||||
|
package com.baeldung.nullchecks;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class NullChecksUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenReferenceEqualityOnPrimitives_thenCompareValues() {
|
||||||
|
int a = 10;
|
||||||
|
int b = 15;
|
||||||
|
int c = 10;
|
||||||
|
int d = a;
|
||||||
|
|
||||||
|
// different values check
|
||||||
|
assertFalse(a == b);
|
||||||
|
|
||||||
|
// same values check
|
||||||
|
assertTrue(a == c);
|
||||||
|
|
||||||
|
// same references check
|
||||||
|
assertTrue(a == d);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenReferenceEqualityOnObjects_thenCompareReferences() {
|
||||||
|
Person a = new Person("Bob", 20);
|
||||||
|
Person b = new Person("Mike", 40);
|
||||||
|
Person c = new Person("Bob", 20);
|
||||||
|
Person d = a;
|
||||||
|
Person e = null;
|
||||||
|
|
||||||
|
// different values check
|
||||||
|
assertFalse(a == b);
|
||||||
|
|
||||||
|
// same values check
|
||||||
|
assertFalse(a == c);
|
||||||
|
|
||||||
|
// same references check
|
||||||
|
assertTrue(a == d);
|
||||||
|
|
||||||
|
// same references check - for nulls
|
||||||
|
assertTrue(e == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenValueEqualityOnPrimitives_thenCompareValues() {
|
||||||
|
int a = 10;
|
||||||
|
Integer b = a;
|
||||||
|
|
||||||
|
assertTrue(b.equals(10));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenValueEqualityOnObjects_thenCompareValues() {
|
||||||
|
Person a = new Person("Bob", 20);
|
||||||
|
Person b = new Person("Mike", 40);
|
||||||
|
Person c = new Person("Bob", 20);
|
||||||
|
Person d = a;
|
||||||
|
Person e = null;
|
||||||
|
|
||||||
|
// different values check
|
||||||
|
assertFalse(a.equals(b));
|
||||||
|
|
||||||
|
// same values check
|
||||||
|
assertTrue(a.equals(c));
|
||||||
|
|
||||||
|
// same references check
|
||||||
|
assertTrue(a.equals(d));
|
||||||
|
|
||||||
|
// null checks
|
||||||
|
assertFalse(a.equals(e));
|
||||||
|
assertThrows(NullPointerException.class, () -> e.equals(a));
|
||||||
|
|
||||||
|
// null checks fixed
|
||||||
|
assertFalse(e != null && e.equals(a));
|
||||||
|
|
||||||
|
// using Objects.equals
|
||||||
|
assertFalse(Objects.equals(e, a));
|
||||||
|
assertTrue(Objects.equals(null, e));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Person {
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
public Person(String name, int age) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Person person = (Person) o;
|
||||||
|
return age == person.age && Objects.equals(name, person.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(name, age);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,10 +9,9 @@
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
<artifactId>parent-java</artifactId>
|
<artifactId>core-java-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-java</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
|
@ -9,10 +9,9 @@
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
<artifactId>parent-java</artifactId>
|
<artifactId>core-java-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-java</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
|
@ -9,10 +9,9 @@
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
<artifactId>parent-java</artifactId>
|
<artifactId>core-java-modules</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-java</relativePath>
|
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
|
@ -73,6 +73,7 @@
|
||||||
<module>core-java-lang-2</module>
|
<module>core-java-lang-2</module>
|
||||||
<module>core-java-lang-3</module>
|
<module>core-java-lang-3</module>
|
||||||
<module>core-java-lang-4</module>
|
<module>core-java-lang-4</module>
|
||||||
|
<module>core-java-lang-5</module>
|
||||||
<module>core-java-lang-math</module>
|
<module>core-java-lang-math</module>
|
||||||
<module>core-java-lang-math-2</module>
|
<module>core-java-lang-math-2</module>
|
||||||
<module>core-java-lang-math-3</module>
|
<module>core-java-lang-math-3</module>
|
||||||
|
@ -117,6 +118,9 @@
|
||||||
<module>core-java-regex</module>
|
<module>core-java-regex</module>
|
||||||
<module>core-java-regex-2</module>
|
<module>core-java-regex-2</module>
|
||||||
<module>core-java-uuid</module>
|
<module>core-java-uuid</module>
|
||||||
|
<module>java-collections-conversions</module>
|
||||||
|
<module>java-collections-conversions-2</module>
|
||||||
|
<module>java-collections-maps-3</module>
|
||||||
<module>pre-jpms</module>
|
<module>pre-jpms</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
HELP.md
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
|
@ -0,0 +1,4 @@
|
||||||
|
FROM openjdk:11
|
||||||
|
ARG JAR_FILE=target/*.jar
|
||||||
|
COPY ${JAR_FILE} app.jar
|
||||||
|
ENTRYPOINT ["java","-jar","/app.jar"]
|
|
@ -0,0 +1 @@
|
||||||
|
### Relevant Articles:
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.docker</groupId>
|
||||||
|
<artifactId>docker</artifactId>
|
||||||
|
<version>0.0.1</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>push-to-private-repo</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>push-to-private-repo</name>
|
||||||
|
<description>Example application to showcase how to push a docker image to a private repository</description>
|
||||||
|
<properties>
|
||||||
|
<java.version>11</java.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.baeldung.docker.push;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class HelloWorldController {
|
||||||
|
@GetMapping("/helloworld")
|
||||||
|
String helloWorld() {
|
||||||
|
return "Hello World!";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.docker.push;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class PushToPrivateRepoApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(PushToPrivateRepoApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
<module>docker-sample-app</module>
|
<module>docker-sample-app</module>
|
||||||
<module>docker-caching/single-module-caching</module>
|
<module>docker-caching/single-module-caching</module>
|
||||||
<module>docker-caching/multi-module-caching</module>
|
<module>docker-caching/multi-module-caching</module>
|
||||||
|
<module>docker-push-to-private-repo</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
### Relevant Articles:
|
||||||
|
|
||||||
|
- [Error Handling in GraphQL With Spring Boot](https://www.baeldung.com/spring-graphql-error-handling)
|
|
@ -7,3 +7,4 @@
|
||||||
- [Automorphic Numbers in Java](https://www.baeldung.com/java-automorphic-numbers)
|
- [Automorphic Numbers in Java](https://www.baeldung.com/java-automorphic-numbers)
|
||||||
- [Convert Byte Size Into a Human-Readable Format in Java](https://www.baeldung.com/java-human-readable-byte-size)
|
- [Convert Byte Size Into a Human-Readable Format in Java](https://www.baeldung.com/java-human-readable-byte-size)
|
||||||
- [Convert boolean to int in Java](https://www.baeldung.com/java-boolean-to-int)
|
- [Convert boolean to int in Java](https://www.baeldung.com/java-boolean-to-int)
|
||||||
|
- [Generate a Random Value From an Enum](https://www.baeldung.com/java-enum-random-value)
|
||||||
|
|
|
@ -9,4 +9,5 @@ This module contains articles about JSON.
|
||||||
- [Hypermedia Serialization With JSON-LD](https://www.baeldung.com/json-linked-data)
|
- [Hypermedia Serialization With JSON-LD](https://www.baeldung.com/json-linked-data)
|
||||||
- [Generate a Java Class From JSON](https://www.baeldung.com/java-generate-class-from-json)
|
- [Generate a Java Class From JSON](https://www.baeldung.com/java-generate-class-from-json)
|
||||||
- [A Guide to FastJson](https://www.baeldung.com/fastjson)
|
- [A Guide to FastJson](https://www.baeldung.com/fastjson)
|
||||||
|
- [Check Whether a String is Valid JSON in Java](https://www.baeldung.com/java-validate-json-string)
|
||||||
- More Articles: [[<-- prev]](/json)
|
- More Articles: [[<-- prev]](/json)
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
|
|
||||||
- [Understanding Maven’s “relativePath” Tag for a Parent POM](https://www.baeldung.com/maven-relativepath)
|
- [Understanding Maven’s “relativePath” Tag for a Parent POM](https://www.baeldung.com/maven-relativepath)
|
||||||
|
- [How to Disable a Maven Plugin Defined in a Parent POM](https://www.baeldung.com/maven-disable-parent-pom-plugin)
|
||||||
|
|
|
@ -278,13 +278,6 @@
|
||||||
</license>
|
</license>
|
||||||
</licenses>
|
</licenses>
|
||||||
|
|
||||||
<repositories>
|
|
||||||
<repository>
|
|
||||||
<id>redhat-repository-techpreview</id>
|
|
||||||
<url>https://maven.repository.redhat.com/techpreview/all/</url>
|
|
||||||
</repository>
|
|
||||||
</repositories>
|
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<querydsl.version>3.7.4</querydsl.version>
|
<querydsl.version>3.7.4</querydsl.version>
|
||||||
<deltaspike.version>1.8.2</deltaspike.version>
|
<deltaspike.version>1.8.2</deltaspike.version>
|
||||||
|
@ -296,7 +289,7 @@
|
||||||
<!-- other plugin versions -->
|
<!-- other plugin versions -->
|
||||||
<war.plugin.version>2.6</war.plugin.version>
|
<war.plugin.version>2.6</war.plugin.version>
|
||||||
<apt-maven-plugin.version>1.1.3</apt-maven-plugin.version>
|
<apt-maven-plugin.version>1.1.3</apt-maven-plugin.version>
|
||||||
<jandex.version>1.2.5.Final-redhat-1</jandex.version>
|
<jandex.version>1.2.4.Final</jandex.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -85,7 +85,7 @@
|
||||||
<hibernate-core.version>5.6.7.Final</hibernate-core.version>
|
<hibernate-core.version>5.6.7.Final</hibernate-core.version>
|
||||||
<maven.deploy.skip>true</maven.deploy.skip>
|
<maven.deploy.skip>true</maven.deploy.skip>
|
||||||
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
|
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
|
||||||
<h2.version>1.4.200</h2.version>
|
<h2.version>2.1.212</h2.version>
|
||||||
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
||||||
<hsqldb.version>2.3.4</hsqldb.version>
|
<hsqldb.version>2.3.4</hsqldb.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
|
@ -1,10 +1,5 @@
|
||||||
package com.baeldung.hibernate;
|
package com.baeldung.hibernate;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.boot.Metadata;
|
import org.hibernate.boot.Metadata;
|
||||||
|
@ -12,62 +7,58 @@ import org.hibernate.boot.MetadataSources;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
|
|
||||||
import com.baeldung.hibernate.joincolumn.Email;
|
import java.io.FileInputStream;
|
||||||
import com.baeldung.hibernate.joincolumn.Office;
|
import java.io.IOException;
|
||||||
import com.baeldung.hibernate.joincolumn.OfficeAddress;
|
import java.net.URL;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import static java.util.Objects.requireNonNull;
|
||||||
|
|
||||||
public class HibernateUtil {
|
public class HibernateUtil {
|
||||||
private static String PROPERTY_FILE_NAME;
|
|
||||||
|
|
||||||
public static SessionFactory getSessionFactory() throws IOException {
|
public static SessionFactory getSessionFactory(String propertyFileName, List<Class<?>> classes) throws IOException {
|
||||||
return getSessionFactory(null);
|
ServiceRegistry serviceRegistry = configureServiceRegistry(propertyFileName);
|
||||||
|
return makeSessionFactory(serviceRegistry, classes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SessionFactory getSessionFactory(String propertyFileName) throws IOException {
|
private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry, List<Class<?>> classes) {
|
||||||
PROPERTY_FILE_NAME = propertyFileName;
|
|
||||||
ServiceRegistry serviceRegistry = configureServiceRegistry();
|
|
||||||
return makeSessionFactory(serviceRegistry);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SessionFactory getSessionFactoryByProperties(Properties properties) throws IOException {
|
|
||||||
ServiceRegistry serviceRegistry = configureServiceRegistry(properties);
|
|
||||||
return makeSessionFactory(serviceRegistry);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry) {
|
|
||||||
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
||||||
|
for (Class<?> clazz: classes) {
|
||||||
|
metadataSources = metadataSources.addAnnotatedClass(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
metadataSources.addPackage("com.baeldung.hibernate.pojo");
|
Metadata metadata = metadataSources
|
||||||
metadataSources.addAnnotatedClass(com.baeldung.hibernate.joincolumn.OfficialEmployee.class);
|
.getMetadataBuilder()
|
||||||
metadataSources.addAnnotatedClass(Email.class);
|
.build();
|
||||||
metadataSources.addAnnotatedClass(Office.class);
|
|
||||||
metadataSources.addAnnotatedClass(OfficeAddress.class);
|
|
||||||
|
|
||||||
Metadata metadata = metadataSources.getMetadataBuilder()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
return metadata.getSessionFactoryBuilder()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
|
return metadata.getSessionFactoryBuilder().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ServiceRegistry configureServiceRegistry() throws IOException {
|
private static ServiceRegistry configureServiceRegistry(String propertyFileName) throws IOException {
|
||||||
return configureServiceRegistry(getProperties());
|
return configureServiceRegistry(getProperties(propertyFileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ServiceRegistry configureServiceRegistry(Properties properties) throws IOException {
|
private static ServiceRegistry configureServiceRegistry(Properties properties) {
|
||||||
return new StandardServiceRegistryBuilder().applySettings(properties)
|
return new StandardServiceRegistryBuilder().applySettings(properties)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Properties getProperties() throws IOException {
|
public static Properties getProperties(String propertyFileName) throws IOException {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
URL propertiesURL = Thread.currentThread()
|
|
||||||
.getContextClassLoader()
|
String file = getResourceURL(propertyFileName).getFile();
|
||||||
.getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties"));
|
|
||||||
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
|
try (FileInputStream inputStream = new FileInputStream(file)) {
|
||||||
properties.load(inputStream);
|
properties.load(inputStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
return properties;
|
return properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static URL getResourceURL(String propertyFileName) {
|
||||||
|
return requireNonNull(Thread.currentThread()
|
||||||
|
.getContextClassLoader()
|
||||||
|
.getResource(StringUtils.defaultString(propertyFileName, "hibernate.properties")));
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,5 @@
|
||||||
package com.baeldung.hibernate.customtypes;
|
package com.baeldung.hibernate.customtypes;
|
||||||
|
|
||||||
import com.baeldung.hibernate.pojo.Phone;
|
|
||||||
import org.hibernate.annotations.Columns;
|
import org.hibernate.annotations.Columns;
|
||||||
import org.hibernate.annotations.Parameter;
|
import org.hibernate.annotations.Parameter;
|
||||||
import org.hibernate.annotations.Type;
|
import org.hibernate.annotations.Type;
|
||||||
|
|
|
@ -7,45 +7,51 @@ import com.baeldung.hibernate.oneToMany.model.ItemOIO;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.boot.Metadata;
|
import org.hibernate.boot.Metadata;
|
||||||
import org.hibernate.boot.MetadataSources;
|
import org.hibernate.boot.MetadataSources;
|
||||||
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
|
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
import org.hibernate.cfg.Environment;
|
||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class HibernateAnnotationUtil {
|
public class HibernateAnnotationUtil {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(HibernateAnnotationUtil.class);
|
private static final SessionFactory SESSION_FACTORY = buildSessionFactory();
|
||||||
|
|
||||||
private static SessionFactory sessionFactory;
|
/**
|
||||||
|
* Utility class
|
||||||
|
*/
|
||||||
|
private HibernateAnnotationUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
public static SessionFactory getSessionFactory() {
|
public static SessionFactory getSessionFactory() {
|
||||||
if (sessionFactory == null) {
|
return SESSION_FACTORY;
|
||||||
sessionFactory = buildSessionFactory();
|
|
||||||
}
|
|
||||||
return sessionFactory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SessionFactory buildSessionFactory() {
|
private static SessionFactory buildSessionFactory() {
|
||||||
try {
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
||||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
.applySettings(dbSettings())
|
||||||
.configure("hibernate-annotation.cfg.xml")
|
.build();
|
||||||
.build();
|
|
||||||
|
|
||||||
Metadata metadata = new MetadataSources(serviceRegistry)
|
Metadata metadata = new MetadataSources(serviceRegistry)
|
||||||
.addAnnotatedClass(Cart.class)
|
.addAnnotatedClass(Cart.class)
|
||||||
.addAnnotatedClass(CartOIO.class)
|
.addAnnotatedClass(CartOIO.class)
|
||||||
.addAnnotatedClass(Item.class)
|
.addAnnotatedClass(Item.class)
|
||||||
.addAnnotatedClass(ItemOIO.class)
|
.addAnnotatedClass(ItemOIO.class)
|
||||||
.getMetadataBuilder()
|
.buildMetadata();
|
||||||
.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
return metadata.getSessionFactoryBuilder().build();
|
return metadata.buildSessionFactory();
|
||||||
|
}
|
||||||
|
|
||||||
} catch (Throwable ex) {
|
private static Map<String, String> dbSettings() {
|
||||||
LOGGER.error("Initial SessionFactory creation failed.", ex);
|
Map<String, String> dbSettings = new HashMap<>();
|
||||||
throw new ExceptionInInitializerError(ex);
|
dbSettings.put(Environment.URL, "jdbc:h2:mem:spring_hibernate_one_to_many");
|
||||||
}
|
dbSettings.put(Environment.USER, "sa");
|
||||||
|
dbSettings.put(Environment.PASS, "");
|
||||||
|
dbSettings.put(Environment.DRIVER, "org.h2.Driver");
|
||||||
|
dbSettings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
|
||||||
|
dbSettings.put(Environment.SHOW_SQL, "true");
|
||||||
|
dbSettings.put(Environment.HBM2DDL_AUTO, "create");
|
||||||
|
return dbSettings;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue