embulk-filter-mysql_lookup 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/build.gradle +1 -1
- data/classpath/embulk-filter-mysql_lookup-0.1.5.jar +0 -0
- data/config1.yml +47 -0
- data/embulk-filter-mysql_lookup-0.1.4.gem +0 -0
- data/example/{config.yml → config1.yml} +2 -0
- data/src/main/java/org/embulk/filter/mysql_lookup/DatabaseConnection.java +158 -1
- data/src/main/java/org/embulk/filter/mysql_lookup/MysqlLookupFilterPlugin.java +16 -14
- metadata +6 -4
- data/classpath/embulk-filter-mysql_lookup-0.1.4.jar +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58c954c9c0b2d3f57d21464af114e2f115068901d8c5a6a1f52171337e58fdb7
|
4
|
+
data.tar.gz: 8de014de954b493160c205f3b5d5a5fb657011bf332b20717e3d354612495c73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23c6c2974484352b108c942e453b51d856a779e809bdf4e39407f16b3b33e72e2f1009cd2bf99fabd16c5cf891c1cf11dd7f0c1e6e5c94a0253ae06cd842e985
|
7
|
+
data.tar.gz: 6ae8efc216663bf2be139ee48f79535b3ad426c9aa781d189ba8d436592002f2522e5c8349aafa8a4d330cfc4feac06e6e30fae3b93e1915f53fa009120c7c6d
|
data/README.md
CHANGED
@@ -13,6 +13,10 @@ An Embulk filter plugin for Lookup Transformation with MySQL database
|
|
13
13
|
## Configuration
|
14
14
|
|
15
15
|
- **mysql_lookup**: Required attributes for the LookUp Filter Plugin -
|
16
|
+
- **filters**:
|
17
|
+
- **type**: Name of lookup type (required)
|
18
|
+
- **driver_path**: path to the jar file of the MySQL JDBC driver. If not set, the bundled JDBC driver (MySQL Connector/J 8.0.19) will be used (string)
|
19
|
+
- **driver_class**: Here we can provide driver class name,if not set,the com.mysql.cj.jdbc.Driver class will be used
|
16
20
|
- **host**: database host (example `localhost`) (required)
|
17
21
|
- **port**: database port (example port for mssql `1433`) (required
|
18
22
|
- **database**: database name (required)
|
data/build.gradle
CHANGED
Binary file
|
data/config1.yml
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
exec:
|
2
|
+
max_threads: 2
|
3
|
+
min_output_tasks: 1
|
4
|
+
in:
|
5
|
+
type: file
|
6
|
+
path_prefix: C:\Users\AnkitKumar\Desktop\embulk-filter-mysqllookup\calendar.csv
|
7
|
+
parser:
|
8
|
+
type: csv
|
9
|
+
columns:
|
10
|
+
- { name: dim_calendar_key, type: long }
|
11
|
+
- { name: year_number, type: long }
|
12
|
+
- { name: quarter_number, type: long }
|
13
|
+
- { name: attr_1, type: string }
|
14
|
+
filters:
|
15
|
+
- type: mysql_lookup
|
16
|
+
host: localhost
|
17
|
+
port: 3306
|
18
|
+
driver_path: C:/Users/AnkitKumar/Desktop/embulk-plugins/mysql-connector-java-8.0.19.jar
|
19
|
+
driver_class: com.mysql.cj.jdbc.Driver
|
20
|
+
database: information
|
21
|
+
table_name: country
|
22
|
+
username: root
|
23
|
+
password: root
|
24
|
+
mapping_from:
|
25
|
+
- quarter_number
|
26
|
+
- attr_1
|
27
|
+
mapping_to:
|
28
|
+
- id
|
29
|
+
- country_code
|
30
|
+
new_columns:
|
31
|
+
- { name: counry_address, type: string }
|
32
|
+
- { name: country_code,type: double}
|
33
|
+
out:
|
34
|
+
type: file
|
35
|
+
path_prefix: C:/Users/AnkitKumar/Desktop/embulk-files/sample/output.csv
|
36
|
+
file_ext: csv
|
37
|
+
formatter:
|
38
|
+
type: csv
|
39
|
+
delimiter: "\t"
|
40
|
+
newline: CRLF
|
41
|
+
newline_in_field: LF
|
42
|
+
charset: UTF-8
|
43
|
+
quote_policy: MINIMAL
|
44
|
+
quote: '"'
|
45
|
+
escape: "\\"
|
46
|
+
null_string: "\\N"
|
47
|
+
default_timezone: 'UTC'
|
Binary file
|
@@ -15,6 +15,8 @@ filters:
|
|
15
15
|
- type: mysql_lookup
|
16
16
|
host: localhost
|
17
17
|
port: 3306
|
18
|
+
driver_path: C:/Users/AnkitKumar/Desktop/embulk-plugins/mysql-connector-java-8.0.19.jar
|
19
|
+
driver_class: com.mysql.cj.jdbc.Driver
|
18
20
|
database: information
|
19
21
|
table_name: country
|
20
22
|
username: root
|
@@ -1,14 +1,35 @@
|
|
1
1
|
package org.embulk.filter.mysql_lookup;
|
2
2
|
|
3
|
+
|
4
|
+
import org.embulk.config.ConfigException;
|
5
|
+
import org.slf4j.Logger;
|
6
|
+
import org.slf4j.LoggerFactory;
|
7
|
+
|
8
|
+
import java.io.File;
|
9
|
+
import java.io.FileFilter;
|
10
|
+
import java.lang.reflect.InvocationTargetException;
|
11
|
+
import java.lang.reflect.Method;
|
12
|
+
import java.net.MalformedURLException;
|
13
|
+
import java.net.URISyntaxException;
|
14
|
+
import java.net.URL;
|
15
|
+
import java.net.URLClassLoader;
|
16
|
+
import java.nio.file.Path;
|
17
|
+
import java.nio.file.Paths;
|
3
18
|
import java.sql.Connection;
|
4
19
|
import java.sql.DriverManager;
|
20
|
+
import java.util.Optional;
|
21
|
+
import java.util.concurrent.atomic.AtomicReference;
|
5
22
|
|
6
23
|
public class DatabaseConnection {
|
7
24
|
private static Connection connection=null;
|
8
25
|
|
9
26
|
private DatabaseConnection(MysqlLookupFilterPlugin.PluginTask task) throws Exception {
|
10
27
|
try{
|
11
|
-
|
28
|
+
if(task.getDriverClass().isPresent()){
|
29
|
+
this.loadMySqlJdbcDriver(task.getDriverClass().get(),task.getDriverPath());
|
30
|
+
}else{
|
31
|
+
this.loadMySqlJdbcDriver("com.mysql.cj.jdbc.Driver",task.getDriverPath());
|
32
|
+
}
|
12
33
|
String url = "jdbc:mysql://" + task.getHost() + ":"+task.getPort()+"/"+task.getDatabase();
|
13
34
|
connection= DriverManager.getConnection(url, task.getUserName(), task.getPassword());
|
14
35
|
}catch (Exception e){
|
@@ -35,4 +56,140 @@ public class DatabaseConnection {
|
|
35
56
|
|
36
57
|
return connection;
|
37
58
|
}
|
59
|
+
|
60
|
+
private Class<? extends java.sql.Driver> loadMySqlJdbcDriver(
|
61
|
+
final String className,
|
62
|
+
final Optional<String> driverPath)
|
63
|
+
{
|
64
|
+
synchronized (mysqlJdbcDriver) {
|
65
|
+
if (mysqlJdbcDriver.get() != null) {
|
66
|
+
return mysqlJdbcDriver.get();
|
67
|
+
}
|
68
|
+
|
69
|
+
try {
|
70
|
+
// If the class is found from the ClassLoader of the plugin, that is prioritized the highest.
|
71
|
+
final Class<? extends java.sql.Driver> found = loadJdbcDriverClassForName(className);
|
72
|
+
mysqlJdbcDriver.compareAndSet(null, found);
|
73
|
+
|
74
|
+
if (driverPath.isPresent()) {
|
75
|
+
logger.warn(
|
76
|
+
"\"driver_path\" is set while the MySQL JDBC driver class \"{}\" is found from the PluginClassLoader."
|
77
|
+
+ " \"driver_path\" is ignored.", className);
|
78
|
+
}
|
79
|
+
return found;
|
80
|
+
}
|
81
|
+
catch (final ClassNotFoundException ex) {
|
82
|
+
// Pass-through once.
|
83
|
+
}
|
84
|
+
|
85
|
+
if (driverPath.isPresent()) {
|
86
|
+
logger.info(
|
87
|
+
"\"driver_path\" is set to load the MySQL JDBC driver class \"{}\". Adding it to classpath.", className);
|
88
|
+
this.addDriverJarToClasspath(driverPath.get());
|
89
|
+
}
|
90
|
+
else {
|
91
|
+
final File root = this.findPluginRoot();
|
92
|
+
final File driverLib = new File(root, "default_jdbc_driver");
|
93
|
+
final File[] files = driverLib.listFiles(new FileFilter() {
|
94
|
+
@Override
|
95
|
+
public boolean accept(final File file)
|
96
|
+
{
|
97
|
+
return file.isFile() && file.getName().endsWith(".jar");
|
98
|
+
}
|
99
|
+
});
|
100
|
+
if (files == null || files.length == 0) {
|
101
|
+
throw new ConfigException(new ClassNotFoundException(
|
102
|
+
"The MySQL JDBC driver for the class \"" + className + "\" is not found"
|
103
|
+
+ " in \"default_jdbc_driver\" (" + root.getAbsolutePath() + ")."));
|
104
|
+
}
|
105
|
+
for (final File file : files) {
|
106
|
+
logger.info(
|
107
|
+
"The MySQL JDBC driver for the class \"{}\" is expected to be found"
|
108
|
+
+ " in \"default_jdbc_driver\" at {}.", className, file.getAbsolutePath());
|
109
|
+
this.addDriverJarToClasspath(file.getAbsolutePath());
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
try {
|
114
|
+
// Retrying to find the class from the ClassLoader of the plugin.
|
115
|
+
final Class<? extends java.sql.Driver> found = loadJdbcDriverClassForName(className);
|
116
|
+
mysqlJdbcDriver.compareAndSet(null, found);
|
117
|
+
return found;
|
118
|
+
}
|
119
|
+
catch (final ClassNotFoundException ex) {
|
120
|
+
throw new ConfigException("The MySQL JDBC driver for the class \"" + className + "\" is not found.", ex);
|
121
|
+
}
|
122
|
+
}
|
123
|
+
}
|
124
|
+
|
125
|
+
@SuppressWarnings("unchecked")
|
126
|
+
private static Class<? extends java.sql.Driver> loadJdbcDriverClassForName(final String className) throws ClassNotFoundException
|
127
|
+
{
|
128
|
+
return (Class<? extends java.sql.Driver>) Class.forName(className);
|
129
|
+
}
|
130
|
+
|
131
|
+
private static final AtomicReference<Class<? extends java.sql.Driver>> mysqlJdbcDriver = new AtomicReference<>();
|
132
|
+
|
133
|
+
private static final Logger logger = LoggerFactory.getLogger(DatabaseConnection.class);
|
134
|
+
|
135
|
+
protected void addDriverJarToClasspath(String glob)
|
136
|
+
{
|
137
|
+
// TODO match glob
|
138
|
+
final ClassLoader loader = getClass().getClassLoader();
|
139
|
+
if (!(loader instanceof URLClassLoader)) {
|
140
|
+
throw new RuntimeException("Plugin is not loaded by URLClassLoader unexpectedly.");
|
141
|
+
}
|
142
|
+
if (!"org.embulk.plugin.PluginClassLoader".equals(loader.getClass().getName())) {
|
143
|
+
throw new RuntimeException("Plugin is not loaded by PluginClassLoader unexpectedly.");
|
144
|
+
}
|
145
|
+
Path path = Paths.get(glob);
|
146
|
+
if (!path.toFile().exists()) {
|
147
|
+
throw new ConfigException("The specified driver jar doesn't exist: " + glob);
|
148
|
+
}
|
149
|
+
final Method addPathMethod;
|
150
|
+
try {
|
151
|
+
addPathMethod = loader.getClass().getMethod("addPath", Path.class);
|
152
|
+
} catch (final NoSuchMethodException ex) {
|
153
|
+
throw new RuntimeException("Plugin is not loaded a ClassLoader which has addPath(Path), unexpectedly.");
|
154
|
+
}
|
155
|
+
try {
|
156
|
+
addPathMethod.invoke(loader, Paths.get(glob));
|
157
|
+
} catch (final IllegalAccessException ex) {
|
158
|
+
throw new RuntimeException(ex);
|
159
|
+
} catch (final InvocationTargetException ex) {
|
160
|
+
final Throwable targetException = ex.getTargetException();
|
161
|
+
if (targetException instanceof MalformedURLException) {
|
162
|
+
throw new IllegalArgumentException(targetException);
|
163
|
+
} else if (targetException instanceof RuntimeException) {
|
164
|
+
throw (RuntimeException) targetException;
|
165
|
+
} else {
|
166
|
+
throw new RuntimeException(targetException);
|
167
|
+
}
|
168
|
+
}
|
169
|
+
}
|
170
|
+
|
171
|
+
protected File findPluginRoot()
|
172
|
+
{
|
173
|
+
try {
|
174
|
+
URL url = getClass().getResource("/" + getClass().getName().replace('.', '/') + ".class");
|
175
|
+
if (url.toString().startsWith("jar:")) {
|
176
|
+
url = new URL(url.toString().replaceAll("^jar:", "").replaceAll("![^!]*$", ""));
|
177
|
+
}
|
178
|
+
|
179
|
+
File folder = new File(url.toURI()).getParentFile();
|
180
|
+
for (;; folder = folder.getParentFile()) {
|
181
|
+
if (folder == null) {
|
182
|
+
throw new RuntimeException("Cannot find 'embulk-filter-xxx' folder.");
|
183
|
+
}
|
184
|
+
|
185
|
+
if (folder.getName().startsWith("embulk-input-")) {
|
186
|
+
return folder;
|
187
|
+
}
|
188
|
+
}
|
189
|
+
} catch (MalformedURLException | URISyntaxException e) {
|
190
|
+
throw new RuntimeException(e);
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
|
38
195
|
}
|
@@ -1,21 +1,13 @@
|
|
1
1
|
package org.embulk.filter.mysql_lookup;
|
2
2
|
import com.google.common.collect.ImmutableList;
|
3
|
-
import org.embulk.config
|
4
|
-
import org.embulk.config.ConfigSource;
|
5
|
-
import org.embulk.config.Task;
|
6
|
-
import org.embulk.config.TaskSource;
|
3
|
+
import org.embulk.config.*;
|
7
4
|
import org.embulk.spi.*;
|
8
5
|
import org.embulk.spi.time.Timestamp;
|
9
6
|
import org.embulk.spi.type.Types;
|
10
|
-
|
11
|
-
import java.sql
|
12
|
-
import java.sql.SQLException;
|
13
|
-
import java.sql.Statement;
|
7
|
+
|
8
|
+
import java.sql.*;
|
14
9
|
import java.time.Instant;
|
15
|
-
import java.util
|
16
|
-
import java.util.HashMap;
|
17
|
-
import java.util.List;
|
18
|
-
import java.util.Map;
|
10
|
+
import java.util.*;
|
19
11
|
|
20
12
|
public class MysqlLookupFilterPlugin
|
21
13
|
implements FilterPlugin {
|
@@ -27,6 +19,15 @@ public class MysqlLookupFilterPlugin
|
|
27
19
|
@Config("port")
|
28
20
|
public String getPort();
|
29
21
|
|
22
|
+
@Config("driver_path")
|
23
|
+
@ConfigDefault("null")
|
24
|
+
public Optional<String> getDriverPath();
|
25
|
+
|
26
|
+
@Config("driver_class")
|
27
|
+
@ConfigDefault("null")
|
28
|
+
public Optional<String> getDriverClass();
|
29
|
+
|
30
|
+
|
30
31
|
@Config("database")
|
31
32
|
public String getDatabase();
|
32
33
|
|
@@ -96,8 +97,8 @@ public class MysqlLookupFilterPlugin
|
|
96
97
|
private Map<String, List<String>> getKeyValueMap(PluginTask task) throws SQLException {
|
97
98
|
Map<String, List<String>> map = new HashMap<>();
|
98
99
|
Connection con = DatabaseConnection.getConnection(task);
|
99
|
-
try {
|
100
100
|
|
101
|
+
try {
|
101
102
|
List<String> targetColumns = task.getMappingTo();
|
102
103
|
List<String> newColumns = new ArrayList<>();
|
103
104
|
|
@@ -124,6 +125,8 @@ public class MysqlLookupFilterPlugin
|
|
124
125
|
|
125
126
|
while (rs.next()) {
|
126
127
|
|
128
|
+
//console.log("");
|
129
|
+
|
127
130
|
//for key
|
128
131
|
String key = "";
|
129
132
|
String comp = "";
|
@@ -160,7 +163,6 @@ public class MysqlLookupFilterPlugin
|
|
160
163
|
return map;
|
161
164
|
}
|
162
165
|
|
163
|
-
|
164
166
|
public static class MyOutput implements PageOutput {
|
165
167
|
private PageReader reader;
|
166
168
|
private PageBuilder builder;
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: embulk-filter-mysql_lookup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- InfoObjects Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -49,13 +49,15 @@ files:
|
|
49
49
|
- LICENSE
|
50
50
|
- README.md
|
51
51
|
- build.gradle
|
52
|
-
- classpath/embulk-filter-mysql_lookup-0.1.
|
52
|
+
- classpath/embulk-filter-mysql_lookup-0.1.5.jar
|
53
53
|
- classpath/mysql-connector-java-8.0.19.jar
|
54
54
|
- classpath/protobuf-java-3.6.1.jar
|
55
55
|
- config/checkstyle/checkstyle.xml
|
56
56
|
- config/checkstyle/default.xml
|
57
|
+
- config1.yml
|
57
58
|
- embulk-filter-mysql_lookup-0.1.3.gem
|
58
|
-
-
|
59
|
+
- embulk-filter-mysql_lookup-0.1.4.gem
|
60
|
+
- example/config1.yml
|
59
61
|
- gradle/wrapper/gradle-wrapper.jar
|
60
62
|
- gradle/wrapper/gradle-wrapper.properties
|
61
63
|
- gradlew
|
Binary file
|