Skip to content

Commit 5acd5e8

Browse files
committed
ETL design pattern issue-328
1 parent 8e8edaa commit 5acd5e8

File tree

6 files changed

+804
-0
lines changed

6 files changed

+804
-0
lines changed

Diff for: etl/etc/etl.urm.puml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@startuml
2+
@enduml

Diff for: etl/pom.xml

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
This project is licensed under the MIT license. Module model-view-viewmodel is
5+
using ZK framework licensed under LGPL (see lgpl-3.0.txt).
6+
7+
The MIT License
8+
Copyright © 2014-2022 Ilkka Seppälä
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy
11+
of this software and associated documentation files (the "Software"), to deal
12+
in the Software without restriction, including without limitation the rights
13+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
copies of the Software, and to permit persons to whom the Software is
15+
furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in
18+
all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
THE SOFTWARE.
27+
28+
-->
29+
<project xmlns="http://maven.apache.org/POM/4.0.0"
30+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
31+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
32+
<modelVersion>4.0.0</modelVersion>
33+
<parent>
34+
<groupId>com.iluwatar</groupId>
35+
<artifactId>java-design-patterns</artifactId>
36+
<version>1.26.0-SNAPSHOT</version>
37+
</parent>
38+
<artifactId>etl</artifactId>
39+
<dependencies>
40+
<dependency>
41+
<groupId>org.json</groupId>
42+
<artifactId>json</artifactId>
43+
<version>20240303</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>javax.json</groupId>
47+
<artifactId>javax.json-api</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.glassfish</groupId>
51+
<artifactId>javax.json</artifactId>
52+
<version>1.1</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.junit.jupiter</groupId>
56+
<artifactId>junit-jupiter-engine</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
<build>
61+
<resources>
62+
<resource>
63+
<directory>src/main/resources</directory>
64+
</resource>
65+
</resources>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-assembly-plugin</artifactId>
70+
<executions>
71+
<execution>
72+
<configuration>
73+
<archive>
74+
<manifest>
75+
<mainClass>com.iluwatar.command.App</mainClass>
76+
</manifest>
77+
</archive>
78+
</configuration>
79+
</execution>
80+
</executions>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
</project>

Diff for: etl/src/main/java/com/iluwatar/etl/App.java

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.etl;
26+
27+
import java.io.IOException;
28+
import java.net.URISyntaxException;
29+
import java.util.List;
30+
31+
/**
32+
* The ETL pattern is a ...
33+
*/
34+
public class App {
35+
36+
/**
37+
* Program entry point.
38+
*
39+
* @param args command line args
40+
*/
41+
public static void main(String[] args) {
42+
var extract = new Extract();
43+
var transform = new Transform();
44+
45+
46+
try {
47+
List<List<String>> result = extract.extract();
48+
transform.transform(result);
49+
} catch (IOException e) {
50+
// TODO Auto-generated catch block
51+
e.printStackTrace();
52+
}
53+
54+
}
55+
}

Diff for: etl/src/main/java/com/iluwatar/etl/Extract.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.iluwatar.etl;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
import java.net.URL;
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
public class Extract {
13+
14+
public List<List<String>> extract() throws IOException {
15+
URL resourceUrl = getClass().getClassLoader().getResource("SocialMediaTop100.csv");
16+
17+
List<List<String>> result = new ArrayList<>();
18+
19+
if (resourceUrl != null) {
20+
InputStream is = resourceUrl.openStream();
21+
result = convert(is);
22+
23+
} else {
24+
System.out.println("Resource not found");
25+
}
26+
27+
return result;
28+
}
29+
30+
public static List<List<String>> convert(InputStream inputStream) throws IOException {
31+
List<List<String>> result = new ArrayList<>();
32+
33+
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
34+
String line;
35+
while ((line = br.readLine()) != null) {
36+
String[] values = line.split(","); // Assuming comma-separated values
37+
result.add(Arrays.asList(values));
38+
}
39+
}
40+
41+
return result;
42+
}
43+
44+
}

Diff for: etl/src/main/java/com/iluwatar/etl/Transform.java

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.iluwatar.etl;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.charset.StandardCharsets;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.util.ArrayList;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.Map.Entry;
14+
15+
import javax.json.Json;
16+
import javax.json.JsonArrayBuilder;
17+
import javax.json.JsonObject;
18+
import javax.json.JsonObjectBuilder;
19+
20+
/**
21+
* */
22+
public class Transform {
23+
24+
public void transform(List<List<String>> lines) throws IOException {
25+
26+
lines.remove(0);
27+
28+
JsonObject json = createPersonsJSON(lines);
29+
// Get the path to the resources folder
30+
Path resourceDirectory = Paths.get("target");
31+
Path filePath = resourceDirectory.resolve("output.txt");
32+
33+
// Write the string to the file
34+
Path path = Files.writeString(filePath, json.toString(), StandardCharsets.UTF_8);
35+
System.out.println("File written successfully at" + path.toString());
36+
}
37+
38+
private static Map<String, List<List<String>>> createPersonMap(List<List<String>> lines) {
39+
Map<String, List<List<String>>> namedMap = new HashMap();
40+
lines.stream().forEach(list -> {
41+
String key = list.get(1);
42+
if(namedMap.containsKey(key)) {
43+
List<List<String>> values = namedMap.get(key);
44+
values.add(list);
45+
namedMap.put(key, values);
46+
} else {
47+
List<List<String>> values = new ArrayList();
48+
values.add(list);
49+
namedMap.put(key, values);
50+
}
51+
52+
});
53+
54+
return namedMap;
55+
}
56+
57+
private static JsonObject createPersonsJSON(List<List<String>> lines) {
58+
Map<String, List<List<String>>> namedMap = createPersonMap(lines);
59+
60+
JsonArrayBuilder outerPersonsBuilder = Json.createArrayBuilder();
61+
62+
namedMap.entrySet().forEach(person ->
63+
outerPersonsBuilder.add(createPersonBuilder(person)));
64+
65+
return Json.createObjectBuilder().add("persons", outerPersonsBuilder).build() ;
66+
67+
}
68+
69+
private static JsonObjectBuilder createPersonBuilder(Entry<String, List<List<String>>> person) {
70+
71+
JsonArrayBuilder outerPersonBuilder = Json.createArrayBuilder();
72+
73+
JsonObjectBuilder nameBuilder = Json.createObjectBuilder()
74+
.add("name", person.getKey());
75+
76+
JsonArrayBuilder outerMediaBuilder = createMediaBuilder(person);
77+
JsonObjectBuilder mediaBuilder = Json.createObjectBuilder()
78+
.add("media", outerMediaBuilder);
79+
80+
outerPersonBuilder.add(nameBuilder).add(mediaBuilder);
81+
return Json.createObjectBuilder().add("person", outerPersonBuilder);
82+
83+
}
84+
85+
private static JsonArrayBuilder createMediaBuilder(Entry<String, List<List<String>>> person) {
86+
JsonArrayBuilder outerMediaBuilder = Json.createArrayBuilder();
87+
88+
person.getValue().forEach(medium -> {
89+
if(medium.contains("Twitter"))
90+
outerMediaBuilder.add(createMediumBuilder(medium, "Twitter"));
91+
if(medium.contains("Instagram"))
92+
outerMediaBuilder.add(createMediumBuilder(medium, "Instagram"));
93+
if(medium.contains("TikTok"))
94+
outerMediaBuilder.add(createMediumBuilder(medium, "TikTok"));
95+
if(medium.contains("Twitch"))
96+
outerMediaBuilder.add(createMediumBuilder(medium, "Twitch"));
97+
if(medium.contains("YouTube"))
98+
outerMediaBuilder.add(createMediumBuilder(medium, "YouTube"));
99+
});
100+
101+
return outerMediaBuilder;
102+
}
103+
104+
private static JsonObjectBuilder createMediumBuilder(List<String> medium, String mediumName) {
105+
106+
JsonObjectBuilder postsBuilder = Json.createObjectBuilder()
107+
.add("posts", medium.get(3));
108+
JsonObjectBuilder followerBuilder = Json.createObjectBuilder()
109+
.add("followers", medium.get(2));
110+
111+
JsonArrayBuilder outerBuilder = Json.createArrayBuilder()
112+
.add(postsBuilder).add(followerBuilder);
113+
return Json.createObjectBuilder().add(mediumName,outerBuilder);
114+
115+
}
116+
117+
118+
}

0 commit comments

Comments
 (0)