Merge branch 'eugenp:master' into master

This commit is contained in:
Ulisses Lima 2022-05-07 17:07:42 -03:00 committed by GitHub
commit 5e53164431
132 changed files with 1616 additions and 332 deletions

View File

@ -31,6 +31,6 @@ public class BalancedBracketsUsingDeque {
}
}
return true;
return deque.isEmpty();
}
}

View File

@ -49,6 +49,12 @@ public class BalancedBracketsUsingDequeUnitTest {
assertThat(result).isFalse();
}
@Test
public void givenAnotherEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingDeque.isBalanced("{{}(");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingDeque.isBalanced("{[()]}");

View File

@ -49,6 +49,12 @@ public class BalancedBracketsUsingStringUnitTest {
assertThat(result).isFalse();
}
@Test
public void givenAnotherEvenLengthUnbalancedString_whenCheckingForBalance_shouldReturnFalse() {
boolean result = balancedBracketsUsingString.isBalanced("{{}(");
assertThat(result).isFalse();
}
@Test
public void givenEvenLengthBalancedString_whenCheckingForBalance_shouldReturnTrue() {
boolean result = balancedBracketsUsingString.isBalanced("{[()]}");

View File

@ -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
1 id zoneId FIRST_NAME MIDDLE_NAME LAST_NAME CITY gender transaction_date transaction_amount
2 1 EU11 Ana1 A MN London Female 1/2/2018 5200
3 2 EU12 Ana2 A MN London Female 29/3/2018 1000
4 3 EU13 Jack1 Josh MN London Male 19/6/2018 9600
5 4 EU14 Jack2 Josh MN London Male 9/9/2018 1000
6 5 EU15 Nick1 H Dee London Male 9/6/2021 3000
7 6 EU16 Nick2 H Dee London Male 27/9/2021 500
8 7 EU17 Nick3 H Dee London Male 8/12/2021 500
9 8 EU18 Sara1 H Dee London Female 7/4/2021 2000

View File

@ -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
}
]

View File

@ -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
}
]

View File

@ -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:

1
apache-spark/docker/init.sql Executable file
View File

@ -0,0 +1 @@
CREATE DATABASE customerdb;

View File

@ -20,37 +20,31 @@
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>${org.apache.spark.spark-core.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>${org.apache.spark.spark-sql.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-graphx_2.11</artifactId>
<version>${org.apache.spark.spark-graphx.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>graphframes</groupId>
<artifactId>graphframes</artifactId>
<version>${graphframes.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.11</artifactId>
<version>${org.apache.spark.spark-streaming.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_2.11</artifactId>
<version>${org.apache.spark.spark-mllib.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
@ -67,6 +61,11 @@
<artifactId>spark-cassandra-connector-java_2.11</artifactId>
<version>${com.datastax.spark.spark-cassandra-connector-java.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgres.version}</version>
</dependency>
</dependencies>
<build>
@ -108,6 +107,7 @@
<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-java.version>1.6.0-M1</com.datastax.spark.spark-cassandra-connector-java.version>
<postgres.version>42.3.3</postgres.version>
</properties>
</project>

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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)
});
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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"));
}
}

View File

@ -4,6 +4,6 @@ This module contains modules about core Java
## 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)
- [Will an Error Be Caught by Catch Block in Java?](https://www.baeldung.com/java-error-catch)

View File

@ -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();
}
}

View File

@ -13,4 +13,5 @@ This module contains articles about core java exceptions
- [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)
- [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)

View File

@ -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)
- [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)
- [Java Scanner.skip method with examples](https://www.baeldung.com/java-scanner-skip)
- [[<-- Prev]](/core-java-modules/core-java-io-3)

View File

@ -6,4 +6,5 @@
- [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)
- [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)

View File

@ -0,0 +1,5 @@
## Core Java Lang (Part 5)
This module contains articles about core features in the Java language
## TODO ##

View File

@ -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>

View File

@ -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);
}
}
}

View File

@ -9,10 +9,9 @@
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>

View File

@ -9,10 +9,9 @@
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>

View File

@ -9,10 +9,9 @@
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>

View File

@ -73,6 +73,7 @@
<module>core-java-lang-2</module>
<module>core-java-lang-3</module>
<module>core-java-lang-4</module>
<module>core-java-lang-5</module>
<module>core-java-lang-math</module>
<module>core-java-lang-math-2</module>
<module>core-java-lang-math-3</module>
@ -117,6 +118,9 @@
<module>core-java-regex</module>
<module>core-java-regex-2</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>
</modules>

View File

@ -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/

View File

@ -0,0 +1,4 @@
FROM openjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

View File

@ -0,0 +1 @@
### Relevant Articles:

View File

@ -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>

View File

@ -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!";
}
}

View File

@ -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);
}
}

View File

@ -28,6 +28,7 @@
<module>docker-sample-app</module>
<module>docker-caching/single-module-caching</module>
<module>docker-caching/multi-module-caching</module>
<module>docker-push-to-private-repo</module>
</modules>
</project>

View File

@ -0,0 +1,3 @@
### Relevant Articles:
- [Error Handling in GraphQL With Spring Boot](https://www.baeldung.com/spring-graphql-error-handling)

View File

@ -7,3 +7,4 @@
- [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 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)

View File

@ -9,4 +9,5 @@ This module contains articles about JSON.
- [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)
- [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)

View File

@ -1,3 +1,4 @@
### Relevant Articles:
- [Understanding Mavens “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)

View File

@ -278,13 +278,6 @@
</license>
</licenses>
<repositories>
<repository>
<id>redhat-repository-techpreview</id>
<url>https://maven.repository.redhat.com/techpreview/all/</url>
</repository>
</repositories>
<properties>
<querydsl.version>3.7.4</querydsl.version>
<deltaspike.version>1.8.2</deltaspike.version>
@ -296,7 +289,7 @@
<!-- other plugin versions -->
<war.plugin.version>2.6</war.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>
</project>

View File

@ -85,7 +85,7 @@
<hibernate-core.version>5.6.7.Final</hibernate-core.version>
<maven.deploy.skip>true</maven.deploy.skip>
<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>
<hsqldb.version>2.3.4</hsqldb.version>
</properties>

View File

@ -1,10 +1,5 @@
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.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
@ -12,62 +7,58 @@ import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import com.baeldung.hibernate.joincolumn.Email;
import com.baeldung.hibernate.joincolumn.Office;
import com.baeldung.hibernate.joincolumn.OfficeAddress;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import static java.util.Objects.requireNonNull;
public class HibernateUtil {
private static String PROPERTY_FILE_NAME;
public static SessionFactory getSessionFactory() throws IOException {
return getSessionFactory(null);
public static SessionFactory getSessionFactory(String propertyFileName, List<Class<?>> classes) throws IOException {
ServiceRegistry serviceRegistry = configureServiceRegistry(propertyFileName);
return makeSessionFactory(serviceRegistry, classes);
}
public static SessionFactory getSessionFactory(String propertyFileName) throws IOException {
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) {
private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry, List<Class<?>> classes) {
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
for (Class<?> clazz: classes) {
metadataSources = metadataSources.addAnnotatedClass(clazz);
}
metadataSources.addPackage("com.baeldung.hibernate.pojo");
metadataSources.addAnnotatedClass(com.baeldung.hibernate.joincolumn.OfficialEmployee.class);
metadataSources.addAnnotatedClass(Email.class);
metadataSources.addAnnotatedClass(Office.class);
metadataSources.addAnnotatedClass(OfficeAddress.class);
Metadata metadata = metadataSources.getMetadataBuilder()
.build();
return metadata.getSessionFactoryBuilder()
.build();
Metadata metadata = metadataSources
.getMetadataBuilder()
.build();
return metadata.getSessionFactoryBuilder().build();
}
private static ServiceRegistry configureServiceRegistry() throws IOException {
return configureServiceRegistry(getProperties());
private static ServiceRegistry configureServiceRegistry(String propertyFileName) throws IOException {
return configureServiceRegistry(getProperties(propertyFileName));
}
private static ServiceRegistry configureServiceRegistry(Properties properties) throws IOException {
private static ServiceRegistry configureServiceRegistry(Properties properties) {
return new StandardServiceRegistryBuilder().applySettings(properties)
.build();
}
public static Properties getProperties() throws IOException {
public static Properties getProperties(String propertyFileName) throws IOException {
Properties properties = new Properties();
URL propertiesURL = Thread.currentThread()
.getContextClassLoader()
.getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties"));
try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) {
String file = getResourceURL(propertyFileName).getFile();
try (FileInputStream inputStream = new FileInputStream(file)) {
properties.load(inputStream);
}
return properties;
}
private static URL getResourceURL(String propertyFileName) {
return requireNonNull(Thread.currentThread()
.getContextClassLoader()
.getResource(StringUtils.defaultString(propertyFileName, "hibernate.properties")));
}
}

View File

@ -1,6 +1,5 @@
package com.baeldung.hibernate.customtypes;
import com.baeldung.hibernate.pojo.Phone;
import org.hibernate.annotations.Columns;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;

View File

@ -7,45 +7,51 @@ import com.baeldung.hibernate.oneToMany.model.ItemOIO;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
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() {
if (sessionFactory == null) {
sessionFactory = buildSessionFactory();
}
return sessionFactory;
return SESSION_FACTORY;
}
private static SessionFactory buildSessionFactory() {
try {
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.configure("hibernate-annotation.cfg.xml")
.build();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(dbSettings())
.build();
Metadata metadata = new MetadataSources(serviceRegistry)
.addAnnotatedClass(Cart.class)
.addAnnotatedClass(CartOIO.class)
.addAnnotatedClass(Item.class)
.addAnnotatedClass(ItemOIO.class)
.getMetadataBuilder()
.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
.build();
Metadata metadata = new MetadataSources(serviceRegistry)
.addAnnotatedClass(Cart.class)
.addAnnotatedClass(CartOIO.class)
.addAnnotatedClass(Item.class)
.addAnnotatedClass(ItemOIO.class)
.buildMetadata();
return metadata.getSessionFactoryBuilder().build();
return metadata.buildSessionFactory();
}
} catch (Throwable ex) {
LOGGER.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
private static Map<String, String> dbSettings() {
Map<String, String> dbSettings = new HashMap<>();
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