Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
commit
22c9996047
|
@ -68,6 +68,11 @@
|
|||
<artifactId>commons-exec</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>one.util</groupId>
|
||||
<artifactId>streamex</artifactId>
|
||||
<version>0.6.5</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
|
|
@ -0,0 +1,105 @@
|
|||
package com.baeldung.map.java_8;
|
||||
|
||||
import com.baeldung.sort.Employee;
|
||||
import one.util.streamex.EntryStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class MergeMaps {
|
||||
|
||||
private static Map<String, Employee> map1 = new HashMap<>();
|
||||
private static Map<String, Employee> map2 = new HashMap<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
initialize();
|
||||
|
||||
mergeFunction();
|
||||
|
||||
streamConcat();
|
||||
|
||||
streamOf();
|
||||
|
||||
streamEx();
|
||||
|
||||
streamMerge();
|
||||
}
|
||||
|
||||
private static void streamMerge() {
|
||||
|
||||
Map<String, Employee> map3 = map2.entrySet()
|
||||
.stream()
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(v1, v2) -> new Employee(v1.getId(), v2.getName()),
|
||||
() -> new HashMap<>(map1)
|
||||
)
|
||||
);
|
||||
|
||||
System.out.println(map3);
|
||||
}
|
||||
|
||||
private static void streamEx() {
|
||||
Map<String, Employee> map3 = EntryStream.of(map1)
|
||||
.append(EntryStream.of(map2))
|
||||
.toMap((e1, e2) -> e1);
|
||||
|
||||
System.out.println(map3);
|
||||
|
||||
}
|
||||
|
||||
private static void streamOf() {
|
||||
Map<String, Employee> map3 = Stream.of(map1, map2)
|
||||
.flatMap(map -> map.entrySet().stream())
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(v1, v2) -> new Employee(v1.getId(), v2.getName())
|
||||
)
|
||||
);
|
||||
|
||||
map3.entrySet().forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static void streamConcat() {
|
||||
Map<String, Employee> result = Stream.concat(map1.entrySet().stream(), map2.entrySet().stream()).collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(value1, value2) -> new Employee(value2.getId(), value1.getName())
|
||||
));
|
||||
|
||||
result.entrySet().forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static void mergeFunction() {
|
||||
Map<String, Employee> map3 = new HashMap<>(map1);
|
||||
|
||||
map2.forEach(
|
||||
(key, value) -> map3.merge(key, value, (v1, v2) ->
|
||||
new Employee(v1.getId(), v2.getName()))
|
||||
);
|
||||
|
||||
map3.entrySet().forEach(System.out::println);
|
||||
}
|
||||
|
||||
|
||||
private static void initialize() {
|
||||
Employee employee1 = new Employee(1L, "Henry");
|
||||
map1.put(employee1.getName(), employee1);
|
||||
Employee employee2 = new Employee(22L, "Annie");
|
||||
map1.put(employee2.getName(), employee2);
|
||||
Employee employee3 = new Employee(8L, "John");
|
||||
map1.put(employee3.getName(), employee3);
|
||||
|
||||
Employee employee4 = new Employee(2L, "George");
|
||||
map2.put(employee4.getName(), employee4);
|
||||
Employee employee5 = new Employee(3L, "Henry");
|
||||
map2.put(employee5.getName(), employee5);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.java.map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.collections4.MultiMap;
|
||||
import org.apache.commons.collections4.MultiMapUtils;
|
||||
import org.apache.commons.collections4.MultiValuedMap;
|
||||
import org.apache.commons.collections4.map.MultiValueMap;
|
||||
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
|
||||
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.LinkedHashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.TreeMultimap;
|
||||
|
||||
|
||||
public class KeyCheckTest {
|
||||
|
||||
@Test
|
||||
public void whenKeyIsPresent_thenContainsKeyReturnsTrue() {
|
||||
Map<String, String> map = Collections.singletonMap("key", "value");
|
||||
|
||||
assertTrue(map.containsKey("key"));
|
||||
assertFalse(map.containsKey("missing"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenKeyHasNullValue_thenGetStillWorks() {
|
||||
Map<String, String> map = Collections.singletonMap("nothing", null);
|
||||
|
||||
assertTrue(map.containsKey("nothing"));
|
||||
assertNull(map.get("nothing"));
|
||||
}
|
||||
}
|
|
@ -36,7 +36,6 @@
|
|||
- [Guide to sun.misc.Unsafe](http://www.baeldung.com/java-unsafe)
|
||||
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
|
||||
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
|
||||
- [Guide to UUID in JAVA](http://www.baeldung.com/guide-to-uuid-in-java)
|
||||
- [How to Add a Single Element to a Stream](http://www.baeldung.com/java-stream-append-prepend)
|
||||
- [Iterating Over Enum Values in Java](http://www.baeldung.com/java-enum-iteration)
|
||||
- [Kotlin Java Interoperability](http://www.baeldung.com/kotlin-java-interoperability)
|
||||
|
|
|
@ -118,7 +118,7 @@
|
|||
|
||||
<properties>
|
||||
<!-- marshalling -->
|
||||
<jackson.version>2.9.6</jackson.version>
|
||||
<jackson.version>2.9.7</jackson.version>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.8</commons-lang3.version>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.jackson.xmlToJson;
|
||||
|
||||
public enum Color {
|
||||
PINK, BLUE, YELLOW, RED;
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.jackson.xmlToJson;
|
||||
|
||||
public class Flower {
|
||||
|
||||
private String name;
|
||||
|
||||
private Color color;
|
||||
|
||||
private Integer petals;
|
||||
|
||||
public Flower() { }
|
||||
|
||||
public Flower(String name, Color color, Integer petals) {
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
this.petals = petals;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public Integer getPetals() {
|
||||
return petals;
|
||||
}
|
||||
|
||||
public void setPetals(Integer petals) {
|
||||
this.petals = petals;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.jackson.xmlToJson;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class XmlToJsonUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() {
|
||||
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
|
||||
|
||||
try {
|
||||
XmlMapper xmlMapper = new XmlMapper();
|
||||
Flower poppy = xmlMapper.readValue(flowerXML, Flower.class);
|
||||
|
||||
assertEquals(poppy.getName(), "Poppy");
|
||||
assertEquals(poppy.getColor(), Color.RED);
|
||||
assertEquals(poppy.getPetals(), new Integer(9));
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String json = mapper.writeValueAsString(poppy);
|
||||
|
||||
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}");
|
||||
System.out.println(json);
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() {
|
||||
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
|
||||
|
||||
try {
|
||||
XmlMapper xmlMapper = new XmlMapper();
|
||||
JsonNode node = xmlMapper.readTree(flowerXML.getBytes());
|
||||
|
||||
ObjectMapper jsonMapper = new ObjectMapper();
|
||||
String json = jsonMapper.writeValueAsString(node);
|
||||
|
||||
System.out.println(json);
|
||||
|
||||
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}");
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
# Overview
|
||||
This is an example of a ETL stream pipeline, mixing a starter application with custom transform and sink.
|
||||
|
||||
# Applications
|
||||
JDBC Source - Application Starter distributed by default
|
||||
|
||||
customer-transform - Custom application to transform the data
|
||||
|
||||
customer-mongodb-sink - Custom application to sink the data
|
|
@ -0,0 +1,75 @@
|
|||
<?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>
|
||||
|
||||
<groupId>com.customer</groupId>
|
||||
<artifactId>customer-mongodb-sink</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>customer-mongodb-sink</name>
|
||||
<description>Example ETL Load Project</description>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-test-support</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
|
@ -0,0 +1,27 @@
|
|||
package com.customer.customermongodbsink;
|
||||
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
@Document(collection = "customer")
|
||||
public class Customer {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.customer.customermongodbsink;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Sink;
|
||||
|
||||
@EnableBinding(Sink.class)
|
||||
public class CustomerListener {
|
||||
|
||||
@Autowired
|
||||
private CustomerRepository repository;
|
||||
|
||||
@StreamListener(Sink.INPUT)
|
||||
public void save(Customer customer) {
|
||||
repository.save(customer);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.customer.customermongodbsink;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CustomerMongodbSinkApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CustomerMongodbSinkApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.customer.customermongodbsink;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CustomerRepository extends MongoRepository<Customer, Long> {
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
<?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>
|
||||
|
||||
<groupId>com.customer</groupId>
|
||||
<artifactId>customer-transform</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>customer-transform</name>
|
||||
<description>Example transform ETL step</description>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-test-support</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
|
@ -0,0 +1,29 @@
|
|||
package com.customer.customertransform;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@JsonProperty("customer_name")
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@JsonProperty("name")
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.customer.customertransform;
|
||||
|
||||
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.messaging.Processor;
|
||||
import org.springframework.integration.annotation.Transformer;
|
||||
|
||||
@EnableBinding(Processor.class)
|
||||
public class CustomerProcessorConfiguration {
|
||||
|
||||
@Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
|
||||
public Customer convertToPojo(Customer payload) {
|
||||
|
||||
return payload;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.customer.customertransform;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CustomerTransformApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CustomerTransformApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<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>
|
||||
<groupId>org.baeldung.spring.cloud</groupId>
|
||||
<artifactId>etl-spring-cloud-data-flow</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>org.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-data-flow</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>customer-mongodb-sink</module>
|
||||
<module>customer-transform</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
|
@ -19,6 +19,7 @@
|
|||
<module>time-processor</module>
|
||||
<module>log-sink</module>
|
||||
<module>batch-job</module>
|
||||
<module>etl</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
|
Loading…
Reference in New Issue