embulk-filter-postgres_lookup 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6741ec605cc98b03bd51993021bf8a0d9dcecc828ce90f0af88617c7d2ffd80
4
- data.tar.gz: b8e5cabee3f41285f0f112e5b22fadd600cf0945482c1069648e02ca3eb6ba51
3
+ metadata.gz: c4f7537cd7ded0bd9873dc73a98f54d9ca49c17415fe1301d6029de55887eeb6
4
+ data.tar.gz: 5027e388a1d320c3e3a2a6b544efceb95234b6495fbe3ac334b14c5a3683e72b
5
5
  SHA512:
6
- metadata.gz: 7a7ab485b7c312547d89f3fb92b4fdd9ef1d6e1c631fe4a2f31f65373f8974bf3312e2e033630014ed5ccaf8563348b18c1075cc16b8891c321f72ee58d7b986
7
- data.tar.gz: 206c090fc9e79c89c287cda83a284c02b65edcabb8a75c4c032252b2638a386d78932e26a7daa5c549906a3de488b371615b4704b028dbb3adb294f641f519f5
6
+ metadata.gz: 6c35d18b107e7c2ad2642d0148c7b00a5ad4a6f0d58cacc44f1bc4a09a78e248cfc60d3e0dca5d0548e01494e9e611d2c3e59f1d6386648386ff6e814f3b9518
7
+ data.tar.gz: 9720d33ee111bf903312e04f203ca0ed5c97f272fafd01afe9510afcaca0ae3c77161802529ec4954df87b76fd8dfd207071c5b70509c7c5459d535268c5bdb2
File without changes
data/README.md CHANGED
@@ -1,9 +1,16 @@
1
+
2
+ <p align="center">
3
+ <a href="https://www.infoobjects.com/" target="blank"><img src="screenshots/logo.png" width="150" alt="InfoObjects Logo" /></a>
4
+ </p>
5
+ <p align="center">Infoobjects is a consulting company that helps enterprises transform how and where they run applications and infrastructure.
6
+ From strategy, to implementation, to ongoing managed services, Infoobjects creates tailored cloud solutions for enterprises at all stages of the cloud journey.</p>
7
+
1
8
  # Postgres lookup filter plugin for Embulk
9
+ [![license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
10
+
2
11
 
3
12
  An Embulk filter plugin for Lookup Transformation with Postgres database
4
13
 
5
- ## Configuration
6
-
7
14
  - **postgres_lookup**: Required attributes for the LookUp Filter Plugin -
8
15
  - **host**: database host (example `localhost`) (required)
9
16
  - **port**: database port (example port for postgres `1433`) (required)
@@ -131,4 +138,7 @@ Release gem:
131
138
 
132
139
  ```
133
140
  $ ./gradlew gemPush
134
- ```
141
+ ```
142
+ ## Licensing
143
+
144
+ InfoObjects [license](LICENSE) (MIT License)
data/build.gradle CHANGED
@@ -13,7 +13,7 @@ configurations {
13
13
  provided
14
14
  }
15
15
 
16
- version = "0.1.0"
16
+ version = "0.1.3"
17
17
 
18
18
  sourceCompatibility = 1.8
19
19
  targetCompatibility = 1.8
data/example/config.yml CHANGED
@@ -3,7 +3,7 @@ exec:
3
3
  min_output_tasks: 1
4
4
  in:
5
5
  type: file
6
- path_prefix: C:\Users\Abhishek Gupta\Desktop\new one\embulk-filter-postgress_lookup\calendar.csv
6
+ path_prefix: C:\Users\Abhishek Gupta\Desktop\github\embulk-filter-postgres_lookup\calendar.csv
7
7
  parser:
8
8
  type: csv
9
9
  columns:
@@ -13,28 +13,13 @@ in:
13
13
  - { name: attr_1, type: string }
14
14
  filters:
15
15
  - type: postgress_lookup
16
- lookup_destination: mysql
16
+ driver_path: C:/Users/Abhishek Gupta/Desktop/postgresql-9.0-801.jdbc4.jar
17
+ driver_class: org.postgresql.Driver
18
+ schema_name: testSchema
17
19
  host: localhost
18
20
  port: 5432
19
21
  database: Abhishek
20
- tablename: country
21
- username: postgres
22
- password: root
23
- mapping_from:
24
- - quarter_number
25
- - attr_1
26
- mapping_to:
27
- - id
28
- - country_address
29
- new_columns:
30
- - { name: country_name, type: string }
31
- - { name: country_address, type: string }
32
- - type: postgress_lookup
33
- lookup_destination: mysql
34
- host: localhost
35
- port: 5432
36
- database: Abhishek
37
- tablename: country
22
+ table: country
38
23
  username: postgres
39
24
  password: root
40
25
  mapping_from:
Binary file
@@ -1,7 +1,24 @@
1
1
  package org.embulk.filter.postgress_lookup;
2
2
 
3
+ import org.embulk.config.ConfigException;
4
+ import org.slf4j.Logger;
5
+ import org.slf4j.LoggerFactory;
6
+
7
+ import java.io.File;
8
+ import java.io.FileFilter;
9
+ import java.lang.reflect.InvocationTargetException;
10
+ import java.lang.reflect.Method;
11
+ import java.net.MalformedURLException;
12
+ import java.net.URISyntaxException;
13
+ import java.net.URL;
14
+ import java.net.URLClassLoader;
15
+ import java.nio.file.Path;
16
+ import java.nio.file.Paths;
3
17
  import java.sql.Connection;
18
+ import java.sql.Driver;
4
19
  import java.sql.DriverManager;
20
+ import java.util.Optional;
21
+ import java.util.concurrent.atomic.AtomicReference;
5
22
 
6
23
  public class PostGresConnection {
7
24
 
@@ -9,7 +26,12 @@ public class PostGresConnection {
9
26
 
10
27
  private PostGresConnection(PostgressLookupFilterPlugin.PluginTask task) throws Exception {
11
28
  try{
12
- Class.forName("org.postgresql.Driver");
29
+ // Class.forName("org.postgresql.Driver");
30
+ if(task.getDriverClass().isPresent()){
31
+ this.loadMySqlJdbcDriver(task.getDriverClass().get(),task.getDriverPath());
32
+ }else{
33
+ this.loadMySqlJdbcDriver("org.postgresql.Driver",task.getDriverPath());
34
+ }
13
35
  String url = "jdbc:postgresql://" + task.getHost() + ":"+task.getPort()+"/"+task.getDatabase();
14
36
  connection= DriverManager.getConnection(url, task.getUserName(), task.getPassword());
15
37
  }catch (Exception e){
@@ -36,4 +58,137 @@ public class PostGresConnection {
36
58
 
37
59
  return connection;
38
60
  }
61
+ private Class<? extends java.sql.Driver> loadMySqlJdbcDriver(
62
+ final String className,
63
+ final Optional<String> driverPath)
64
+ {
65
+ synchronized (mysqlJdbcDriver) {
66
+ if (mysqlJdbcDriver.get() != null) {
67
+ return mysqlJdbcDriver.get();
68
+ }
69
+
70
+ try {
71
+ // If the class is found from the ClassLoader of the plugin, that is prioritized the highest.
72
+ final Class<? extends java.sql.Driver> found = loadJdbcDriverClassForName(className);
73
+ mysqlJdbcDriver.compareAndSet(null, found);
74
+
75
+ if (driverPath.isPresent()) {
76
+ logger.warn(
77
+ "\"driver_path\" is set while the MySQL JDBC driver class \"{}\" is found from the PluginClassLoader."
78
+ + " \"driver_path\" is ignored.", className);
79
+ }
80
+ return found;
81
+ }
82
+ catch (final ClassNotFoundException ex) {
83
+ // Pass-through once.
84
+ }
85
+
86
+ if (driverPath.isPresent()) {
87
+ logger.info(
88
+ "\"driver_path\" is set to load the MySQL JDBC driver class \"{}\". Adding it to classpath.", className);
89
+ this.addDriverJarToClasspath(driverPath.get());
90
+ }
91
+ else {
92
+ final File root = this.findPluginRoot();
93
+ final File driverLib = new File(root, "default_jdbc_driver");
94
+ final File[] files = driverLib.listFiles(new FileFilter() {
95
+ @Override
96
+ public boolean accept(final File file)
97
+ {
98
+ return file.isFile() && file.getName().endsWith(".jar");
99
+ }
100
+ });
101
+ if (files == null || files.length == 0) {
102
+ throw new ConfigException(new ClassNotFoundException(
103
+ "The MySQL JDBC driver for the class \"" + className + "\" is not found"
104
+ + " in \"default_jdbc_driver\" (" + root.getAbsolutePath() + ")."));
105
+ }
106
+ for (final File file : files) {
107
+ logger.info(
108
+ "The MySQL JDBC driver for the class \"{}\" is expected to be found"
109
+ + " in \"default_jdbc_driver\" at {}.", className, file.getAbsolutePath());
110
+ this.addDriverJarToClasspath(file.getAbsolutePath());
111
+ }
112
+ }
113
+
114
+ try {
115
+ // Retrying to find the class from the ClassLoader of the plugin.
116
+ final Class<? extends java.sql.Driver> found = loadJdbcDriverClassForName(className);
117
+ mysqlJdbcDriver.compareAndSet(null, found);
118
+ return found;
119
+ }
120
+ catch (final ClassNotFoundException ex) {
121
+ throw new ConfigException("The MySQL JDBC driver for the class \"" + className + "\" is not found.", ex);
122
+ }
123
+ }
124
+ }
125
+
126
+ @SuppressWarnings("unchecked")
127
+ private static Class<? extends java.sql.Driver> loadJdbcDriverClassForName(final String className) throws ClassNotFoundException
128
+ {
129
+ return (Class<? extends java.sql.Driver>) Class.forName(className);
130
+ }
131
+
132
+ private static final AtomicReference<Class<? extends Driver>> mysqlJdbcDriver = new AtomicReference<>();
133
+
134
+ private static final Logger logger = LoggerFactory.getLogger(PostGresConnection.class);
135
+
136
+ protected void addDriverJarToClasspath(String glob)
137
+ {
138
+ // TODO match glob
139
+ final ClassLoader loader = getClass().getClassLoader();
140
+ if (!(loader instanceof URLClassLoader)) {
141
+ throw new RuntimeException("Plugin is not loaded by URLClassLoader unexpectedly.");
142
+ }
143
+ if (!"org.embulk.plugin.PluginClassLoader".equals(loader.getClass().getName())) {
144
+ throw new RuntimeException("Plugin is not loaded by PluginClassLoader unexpectedly.");
145
+ }
146
+ Path path = Paths.get(glob);
147
+ if (!path.toFile().exists()) {
148
+ throw new ConfigException("The specified driver jar doesn't exist: " + glob);
149
+ }
150
+ final Method addPathMethod;
151
+ try {
152
+ addPathMethod = loader.getClass().getMethod("addPath", Path.class);
153
+ } catch (final NoSuchMethodException ex) {
154
+ throw new RuntimeException("Plugin is not loaded a ClassLoader which has addPath(Path), unexpectedly.");
155
+ }
156
+ try {
157
+ addPathMethod.invoke(loader, Paths.get(glob));
158
+ } catch (final IllegalAccessException ex) {
159
+ throw new RuntimeException(ex);
160
+ } catch (final InvocationTargetException ex) {
161
+ final Throwable targetException = ex.getTargetException();
162
+ if (targetException instanceof MalformedURLException) {
163
+ throw new IllegalArgumentException(targetException);
164
+ } else if (targetException instanceof RuntimeException) {
165
+ throw (RuntimeException) targetException;
166
+ } else {
167
+ throw new RuntimeException(targetException);
168
+ }
169
+ }
170
+ }
171
+
172
+ protected File findPluginRoot()
173
+ {
174
+ try {
175
+ URL url = getClass().getResource("/" + getClass().getName().replace('.', '/') + ".class");
176
+ if (url.toString().startsWith("jar:")) {
177
+ url = new URL(url.toString().replaceAll("^jar:", "").replaceAll("![^!]*$", ""));
178
+ }
179
+
180
+ File folder = new File(url.toURI()).getParentFile();
181
+ for (;; folder = folder.getParentFile()) {
182
+ if (folder == null) {
183
+ throw new RuntimeException("Cannot find 'embulk-filter-xxx' folder.");
184
+ }
185
+
186
+ if (folder.getName().startsWith("embulk-input-")) {
187
+ return folder;
188
+ }
189
+ }
190
+ } catch (URISyntaxException | MalformedURLException e) {
191
+ throw new RuntimeException(e);
192
+ }
193
+ }
39
194
  }
@@ -2,23 +2,14 @@ package org.embulk.filter.postgress_lookup;
2
2
 
3
3
 
4
4
  import com.google.common.collect.ImmutableList;
5
- import org.embulk.config.Config;
6
- import org.embulk.config.ConfigSource;
7
- import org.embulk.config.Task;
8
- import org.embulk.config.TaskSource;
5
+ import org.embulk.config.*;
9
6
  import org.embulk.spi.*;
10
7
  import org.embulk.spi.time.Timestamp;
11
8
  import org.embulk.spi.type.Types;
12
9
 
13
- import java.sql.Connection;
14
- import java.sql.ResultSet;
15
- import java.sql.SQLException;
16
- import java.sql.Statement;
10
+ import java.sql.*;
17
11
  import java.time.Instant;
18
- import java.util.ArrayList;
19
- import java.util.HashMap;
20
- import java.util.List;
21
- import java.util.Map;
12
+ import java.util.*;
22
13
 
23
14
  public class PostgressLookupFilterPlugin
24
15
  implements FilterPlugin {
@@ -34,7 +25,7 @@ public class PostgressLookupFilterPlugin
34
25
  @Config("database")
35
26
  public String getDatabase();
36
27
 
37
- @Config("tablename")
28
+ @Config("table")
38
29
  public String getTableName();
39
30
 
40
31
  @Config("username")
@@ -51,6 +42,19 @@ public class PostgressLookupFilterPlugin
51
42
 
52
43
  @Config("new_columns")
53
44
  public SchemaConfig getNewColumns();
45
+
46
+ @Config("driver_path")
47
+ @ConfigDefault("null")
48
+ public Optional<String> getDriverPath();
49
+
50
+ @Config("driver_class")
51
+ @ConfigDefault("null")
52
+ public Optional<String> getDriverClass();
53
+
54
+ @Config("schema_name")
55
+ @ConfigDefault("null")
56
+ public Optional<String> getSchemaName();
57
+
54
58
  }
55
59
 
56
60
 
@@ -100,6 +104,22 @@ public class PostgressLookupFilterPlugin
100
104
  private Map<String, List<String>> getKeyValueMap(PluginTask task) throws SQLException {
101
105
  Map<String, List<String>> map = new HashMap<>();
102
106
  Connection con = PostGresConnection.getConnection(task);
107
+ DatabaseMetaData databaseMetaData =con.getMetaData();
108
+ String identifierQuoteString=databaseMetaData.getIdentifierQuoteString();
109
+ //String schemaName=null;
110
+
111
+ if (task.getSchemaName().isPresent()) {
112
+ String sql = "SET search_path TO " + identifierQuoteString + task.getSchemaName().get() + identifierQuoteString;
113
+ System.out.println(sql);
114
+
115
+ Statement stmt = con.createStatement();
116
+ try {
117
+ stmt.executeUpdate(sql);
118
+ } finally {
119
+ stmt.close();
120
+ }
121
+ }
122
+ con.setAutoCommit(false);
103
123
  try {
104
124
 
105
125
  List<String> targetColumns = task.getMappingTo();
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-filter-postgres_lookup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
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-09-16 00:00:00.000000000 Z
11
+ date: 2022-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -46,23 +46,23 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
- - LICENSE.txt
49
+ - LICENSE
50
50
  - README.md
51
51
  - build.gradle
52
52
  - calendar.csv
53
- - classpath/embulk-filter-postgres_lookup-0.1.0.jar
53
+ - classpath/embulk-filter-postgres_lookup-0.1.3.jar
54
54
  - classpath/mysql-connector-java-8.0.19.jar
55
55
  - classpath/postgresql-9.0-801.jdbc4.jar
56
56
  - classpath/protobuf-java-3.6.1.jar
57
57
  - config/checkstyle/checkstyle.xml
58
58
  - config/checkstyle/default.xml
59
- - embulk-filter-postgress_lookup-0.1.0.gem
60
59
  - example/config.yml
61
60
  - gradle/wrapper/gradle-wrapper.jar
62
61
  - gradle/wrapper/gradle-wrapper.properties
63
62
  - gradlew
64
63
  - gradlew.bat
65
64
  - lib/embulk/filter/postgress_lookup.rb
65
+ - screenshots/logo.png
66
66
  - src/main/java/org/embulk/filter/postgress_lookup/PostGresConnection.java
67
67
  - src/main/java/org/embulk/filter/postgress_lookup/PostgressLookupFilterPlugin.java
68
68
  - src/test/java/org/embulk/filter/postgress_lookup/TestPostgressLookupFilterPlugin.java