embulk-filter-mssql_lookup 0.1.0 → 0.1.2

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: 824dbb4c0c5e607fc9a69c607b4fd9e741155c0b3933ea2e587584549f0bd3bc
4
- data.tar.gz: fcd7322b3786a0c9e933487226416b4226f095c01b1c9873385083dbfcd565e1
3
+ metadata.gz: 40422447490354ff161a3de66b9f38aa09276bacbcbe4c0951e728c0e7f16c97
4
+ data.tar.gz: 010cec4f77a7c7d9e7565ee80c9f8d90985632b9fb90dad43bf00b9aff27a69b
5
5
  SHA512:
6
- metadata.gz: 88ce5dcca3d990d6ffbb8590a10cde4db0a4d4ae8e746d0511184d662ac0fdb40210c16999ddf25dd076551f566ac3554d54365325304530ebbd3d0500ade97c
7
- data.tar.gz: c9adb088037d5eb57c9aa3cf63a44c5c22269b01f251db2f1275c7febd6f538a93f3252c26ef1b7fee8df9e8f3fa7f9e6b19192e2fa9b264b114262b249cdb77
6
+ metadata.gz: 0577f89f211199c9ace7105bb0f8997733a5c3f95c9398ed481a3b9a03254bd7d2367cc334e0541bf816e285be7ea139e33a32af362e4e6cfc0b658574e1ce10
7
+ data.tar.gz: aa72a4aa80975d05c6c070fd5be67962ae005f5b525946c904a3afc2a53d71443839606397dfad6d6612ea0e6ba454958b8b5bbf966eb046aede6b19450d1e81
File without changes
data/README.md CHANGED
@@ -1,14 +1,26 @@
1
+ <p align="center">
2
+ <a href="https://www.infoobjects.com/" target="blank"><img src="screenshots/logo.png" width="150" alt="InfoObjects Logo" /></a>
3
+ </p>
4
+ <p align="center">Infoobjects is a consulting company that helps enterprises transform how and where they run applications and infrastructure.
5
+ From strategy, to implementation, to ongoing managed services, Infoobjects creates tailored cloud solutions for enterprises at all stages of the cloud journey.</p>
6
+
1
7
  # MS-SQL lookup filter plugin for Embulk
8
+ [![license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
2
9
 
3
10
  An Embulk filter plugin for Lookup Transformation with MS-SQL database
4
11
 
5
12
  ## Configuration
6
13
 
7
14
  - **mssql_lookup**: Required attributes for the LookUp Filter Plugin -
15
+ - **filters**:
16
+ - **type**: Name of lookup type (required)
17
+ - **driver_path**: /home/infoobjects/Downloads/sqljdbc_1s1.2.0.0_enu/sqljdbc_11.2/enu/mssql-jdbc-11.2.0.jre8.jar
18
+ - **driver_class**: com.microsoft.sqlserver.jdbc.SQLServerDriver
8
19
  - **host**: database host (example `localhost`) (required)
9
20
  - **port**: database port (example port for mssql `1433`) (required
10
21
  - **database**: database name (required)
11
- - **tablename**: table name of your database (required)
22
+ - **schemaName**: name of schema in database in which table is located
23
+ - **table_name**: table name of your database (required)
12
24
  - **username**: username for your database (required)
13
25
  - **password**: password for database (required)
14
26
  - **mapping_from**: (Name of columns to be matched with table 2 columns) (required)
@@ -80,11 +92,14 @@ Output File generated :-
80
92
  ```
81
93
 
82
94
  ```yaml
83
- - type: mssql_lookup
95
+ type: mssql_lookup
96
+ driver_path: /home/infoobjects/Downloads/sqljdbc_1s1.2.0.0_enu/sqljdbc_11.2/enu/mssql-jdbc-11.2.0.jre8.jar
97
+ driver_class: com.microsoft.sqlserver.jdbc.SQLServerDriver
84
98
  host: localhost
85
99
  port: 1433
86
100
  database: test
87
- tablename: country_details
101
+ schemaName: testSchema1
102
+ table_name: country_details
88
103
  username: root
89
104
  password: root
90
105
  mapping_from:
@@ -125,4 +140,8 @@ Release gem:
125
140
 
126
141
  ```
127
142
  $ ./gradlew gemPush
128
- ```
143
+ ```
144
+
145
+ ## Licensing
146
+
147
+ 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.2"
17
17
 
18
18
  sourceCompatibility = 1.8
19
19
  targetCompatibility = 1.8
@@ -80,7 +80,7 @@ Gem::Specification.new do |spec|
80
80
  spec.authors = ["Infoobjects Inc."]
81
81
  spec.summary = %[Mssql Lookup filter plugin for Embulk]
82
82
  spec.description = %[Mssql Lookup]
83
- spec.email = ["ankit.navral@infoobjects.com"]
83
+ spec.email = ["utkarsh@infoobjects.com"]
84
84
  spec.licenses = ["MIT"]
85
85
  # TODO set this:
86
86
  spec.homepage = "https://github.com/ankit31223/embulk-filter-mysql_lookup"
data/example/config.yml CHANGED
@@ -1,59 +1,32 @@
1
1
  exec:
2
- max_threads: 2
2
+ max_threads: 1
3
3
  min_output_tasks: 1
4
4
  in:
5
5
  type: file
6
- path_prefix: /home/infoobjects/Downloads/sample/calendarFloat.csv
6
+ path_prefix: /home/infoobjects/Downloads/sample/calendarFloat1 (copy).csv
7
7
  parser:
8
8
  type: csv
9
9
  columns:
10
- - { name: dim_calendar_key, type: long }
11
- - { name: year_number, type: long }
10
+ - { name: Country_Name, type: string }
12
11
  - { name: quarter_number, type: long }
13
- - { name: attr_1, type: string }
12
+
14
13
  filters:
15
14
  - type: mssql_lookup
15
+ driver_path: /home/infoobjects/Downloads/sqljdbc_1s1.2.0.0_enu/sqljdbc_11.2/enu/mssql-jdbc-11.2.0.jre8.jar
16
+ driver_class: com.microsoft.sqlserver.jdbc.SQLServerDriver
16
17
  host: infoobjects-ThinkPad-E490
17
18
  database: test
18
- tablename: countryNew
19
+ schemaName: testSchema1
20
+ table_name: cnew
19
21
  username: SA
20
22
  password: Mssqlroot95
21
23
  mapping_from:
22
24
  - quarter_number
23
- - attr_1
24
25
  mapping_to:
25
- - id
26
- - country_code
26
+ - qtr_no
27
27
  new_columns:
28
- - { name: country_name, type: string }
29
- - type: mssql_lookup
30
- host: infoobjects-ThinkPad-E490
31
- database: test
32
- tablename: countryNew
33
- username: SA
34
- password: Mssqlroot95
35
- mapping_from:
36
- - quarter_number
37
- - attr_1
38
- mapping_to:
39
- - id
40
- - country_code
41
- new_columns:
42
- - { name: country_address, type: string }
43
- - type: mssql_lookup
44
- host: infoobjects-ThinkPad-E490
45
- database: test
46
- tablename: countryNew
47
- username: SA
48
- password: Mssqlroot95
49
- mapping_from:
50
- - quarter_number
51
- - attr_1
52
- mapping_to:
53
- - id
54
- - country_code
55
- new_columns:
56
- - { name: country_code,type: double}
28
+ - { name: qtr_name, type: string }
29
+
57
30
  out:
58
31
  type: file
59
32
  path_prefix: /home/infoobjects/sqlOutput/output.csv
Binary file
@@ -1,50 +1,191 @@
1
1
  package org.embulk.filter.mssql_lookup;
2
2
 
3
+ import org.embulk.config.ConfigException;
4
+ import org.jruby.RubyProcess;
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
- import java.sql.DatabaseMetaData;
5
19
  import java.sql.DriverManager;
6
- import java.sql.SQLException;
20
+ import java.util.Optional;
21
+ import java.util.concurrent.atomic.AtomicReference;
7
22
 
8
23
  public class DatabaseConnection {
9
24
  private static Connection connection = null;
10
25
  private DatabaseConnection(MssqlLookupFilterPlugin.PluginTask task) throws Exception{
11
26
  try {
12
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
13
- String dbURL = "jdbc:sqlserver://" +task.getHost() + ";databaseName=" +task.getDatabase();
14
- connection = DriverManager.getConnection(dbURL, task.getUserName(), task.getPassword());
15
- if (connection != null){
16
- DatabaseMetaData dm = null;
27
+ if(task.getDriverClass().isPresent()){
28
+ this.loadMsSqlJdbcDriver(task.getDriverClass().get(), task.getDriverPath());
29
+ }else{
30
+ this.loadMsSqlJdbcDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver",task.getDriverPath());
31
+ }
32
+ String url = "jdbc:sqlserver://" +task.getHost() + ";databaseName=" +task.getDatabase();
33
+ connection= DriverManager.getConnection(url, task.getUserName(), task.getPassword());
34
+ }catch (Exception e){
35
+ e.printStackTrace();
36
+ throw new Exception(e);
37
+ }
38
+ }
39
+ public static Connection getConnection(MssqlLookupFilterPlugin.PluginTask task){
40
+ try {
41
+ if(connection==null || connection.isClosed()){
17
42
  try {
18
- dm = connection.getMetaData();
19
- } catch (SQLException e) {
20
- throw new RuntimeException(e);
43
+ new DatabaseConnection(task);
44
+ return connection;
45
+ } catch (Exception e) {
46
+ e.printStackTrace();
47
+ throw new RuntimeException();
21
48
  }
22
- try {
23
- System.out.println("Driver name: " + dm.getDriverName());
24
- System.out.println("Driver version: " + dm.getDriverVersion());
25
- System.out.println("Product name: " + dm.getDatabaseProductName());
26
- System.out.println("Product version: " + dm.getDatabaseProductVersion());
27
- } catch (SQLException e) {
28
- throw new RuntimeException(e);
49
+ }
50
+ }catch (Exception e){
51
+ throw new RuntimeException(e);
52
+ }
53
+
54
+ return connection;
55
+ }
56
+
57
+ private Class<? extends java.sql.Driver> loadMsSqlJdbcDriver(
58
+ final String className,
59
+ final Optional<String>driverPath)
60
+ {
61
+ synchronized (mssqlJdbcDriver) {
62
+ if (mssqlJdbcDriver.get() != null) {
63
+ return mssqlJdbcDriver.get();
64
+ }
65
+
66
+ try {
67
+ // If the class is found from the ClassLoader of the plugin, that is prioritized the highest.
68
+ final Class<? extends java.sql.Driver> found = loadJdbcDriverClassForName(className);
69
+ mssqlJdbcDriver.compareAndSet(null, found);
70
+
71
+ if (driverPath.isPresent()) {
72
+ logger.warn(
73
+ "\"driver_path\" is set while the MsSQL JDBC driver class \"{}\" is found from the PluginClassLoader."
74
+ + " \"driver_path\" is ignored.", className);
29
75
  }
76
+ return found;
77
+ }
78
+ catch (final ClassNotFoundException ex) {
79
+ // Pass-through once.
80
+ }
30
81
 
82
+ if (driverPath.isPresent()) {
83
+ logger.info(
84
+ "\"driver_path\" is set to load the MsSQL JDBC driver class \"{}\". Adding it to classpath.", className);
85
+ this.addDriverJarToClasspath(driverPath.get());
86
+ }
87
+ else {
88
+ final File root = this.findPluginRoot();
89
+ final File driverLib = new File(root, "default_jdbc_driver");
90
+ final File[] files = driverLib.listFiles(new FileFilter() {
91
+ @Override
92
+ public boolean accept(final File file)
93
+ {
94
+ return file.isFile() && file.getName().endsWith(".jar");
95
+ }
96
+ });
97
+ if (files == null || files.length == 0) {
98
+ throw new ConfigException(new ClassNotFoundException(
99
+ "The MsSQL JDBC driver for the class \"" + className + "\" is not found"
100
+ + " in \"default_jdbc_driver\" (" + root.getAbsolutePath() + ")."));
101
+ }
102
+ for (final File file : files) {
103
+ logger.info(
104
+ "The MsSQL JDBC driver for the class \"{}\" is expected to be found"
105
+ + " in \"default_jdbc_driver\" at {}.", className, file.getAbsolutePath());
106
+ this.addDriverJarToClasspath(file.getAbsolutePath());
107
+ }
31
108
  }
32
109
 
33
- }catch (Exception e){
34
- e.printStackTrace();
35
- throw new Exception(e);
110
+ try {
111
+ // Retrying to find the class from the ClassLoader of the plugin.
112
+ final Class<? extends java.sql.Driver> found = loadJdbcDriverClassForName(className);
113
+ mssqlJdbcDriver.compareAndSet(null, found);
114
+ return found;
115
+ }
116
+ catch (final ClassNotFoundException ex) {
117
+ throw new ConfigException("The MsSQL JDBC driver for the class \"" + className + "\" is not found.", ex);
118
+ }
36
119
  }
37
120
  }
38
- public static Connection getConnection(MssqlLookupFilterPlugin.PluginTask task) throws SQLException {
39
- if(connection==null || connection.isClosed()){
40
- try {
41
- new DatabaseConnection(task);
42
- return connection;
43
- } catch (Exception e) {
44
- e.printStackTrace();
45
- throw new RuntimeException();
121
+
122
+ @SuppressWarnings("unchecked")
123
+ private static Class<? extends java.sql.Driver> loadJdbcDriverClassForName(final String className) throws ClassNotFoundException
124
+ {
125
+ return (Class<? extends java.sql.Driver>) Class.forName(className);
126
+ }
127
+
128
+ private static final AtomicReference<Class<? extends java.sql.Driver>> mssqlJdbcDriver = new AtomicReference<>();
129
+
130
+ private static final Logger logger = LoggerFactory.getLogger(DatabaseConnection.class);
131
+
132
+ protected void addDriverJarToClasspath(String glob)
133
+ {
134
+ // TODO match glob
135
+ final ClassLoader loader = getClass().getClassLoader();
136
+ if (!(loader instanceof URLClassLoader)) {
137
+ throw new RuntimeException("Plugin is not loaded by URLClassLoader unexpectedly.");
138
+ }
139
+ if (!"org.embulk.plugin.PluginClassLoader".equals(loader.getClass().getName())) {
140
+ throw new RuntimeException("Plugin is not loaded by PluginClassLoader unexpectedly.");
141
+ }
142
+ Path path = Paths.get(glob);
143
+ if (!path.toFile().exists()) {
144
+ throw new ConfigException("The specified driver jar doesn't exist: " + glob);
145
+ }
146
+ final Method addPathMethod;
147
+ try {
148
+ addPathMethod = loader.getClass().getMethod("addPath", Path.class);
149
+ } catch (final NoSuchMethodException ex) {
150
+ throw new RuntimeException("Plugin is not loaded a ClassLoader which has addPath(Path), unexpectedly.");
151
+ }
152
+ try {
153
+ addPathMethod.invoke(loader, Paths.get(glob));
154
+ } catch (final IllegalAccessException ex) {
155
+ throw new RuntimeException(ex);
156
+ } catch (final InvocationTargetException ex) {
157
+ final Throwable targetException = ex.getTargetException();
158
+ if (targetException instanceof MalformedURLException) {
159
+ throw new IllegalArgumentException(targetException);
160
+ } else if (targetException instanceof RuntimeException) {
161
+ throw (RuntimeException) targetException;
162
+ } else {
163
+ throw new RuntimeException(targetException);
46
164
  }
47
165
  }
48
- return connection;
49
166
  }
50
- }
167
+
168
+ protected File findPluginRoot()
169
+ {
170
+ try {
171
+ URL url = getClass().getResource("/" + getClass().getName().replace('.', '/') + ".class");
172
+ if (url.toString().startsWith("jar:")) {
173
+ url = new URL(url.toString().replaceAll("^jar:", "").replaceAll("![^!]*$", ""));
174
+ }
175
+
176
+ File folder = new File(url.toURI()).getParentFile();
177
+ for (;; folder = folder.getParentFile()) {
178
+ if (folder == null) {
179
+ throw new RuntimeException("Cannot find 'embulk-filter-xxx' folder.");
180
+ }
181
+
182
+ if (folder.getName().startsWith("embulk-input-")) {
183
+ return folder;
184
+ }
185
+ }
186
+ } catch (MalformedURLException | URISyntaxException e) {
187
+ throw new RuntimeException(e);
188
+ }
189
+ }
190
+
191
+ }
@@ -1,14 +1,8 @@
1
1
  package org.embulk.filter.mssql_lookup;
2
2
 
3
3
  import com.google.common.base.Optional;
4
-
5
4
  import com.google.common.collect.ImmutableList;
6
- import org.embulk.config.Config;
7
- import org.embulk.config.ConfigDefault;
8
- import org.embulk.config.ConfigDiff;
9
- import org.embulk.config.ConfigSource;
10
- import org.embulk.config.Task;
11
- import org.embulk.config.TaskSource;
5
+ import org.embulk.config.*;
12
6
  import org.embulk.spi.*;
13
7
  import org.embulk.spi.type.Types;
14
8
 
@@ -34,7 +28,7 @@ public class MssqlLookupFilterPlugin
34
28
  @Config("database")
35
29
  public String getDatabase();
36
30
 
37
- @Config("tablename")
31
+ @Config("table_name")
38
32
  public String getTableName();
39
33
 
40
34
  @Config("username")
@@ -52,6 +46,18 @@ public class MssqlLookupFilterPlugin
52
46
  @Config("new_columns")
53
47
  public SchemaConfig getNewColumns();
54
48
 
49
+ @Config("driver_path")
50
+ @ConfigDefault("null")
51
+ public java.util.Optional<String> getDriverPath();
52
+
53
+ @Config("driver_class")
54
+ @ConfigDefault("null")
55
+ public Optional<String> getDriverClass();
56
+
57
+ @Config("schemaName")
58
+ @ConfigDefault("null")
59
+ public Optional<String> getSchemaName();
60
+
55
61
  }
56
62
 
57
63
  @Override
@@ -101,7 +107,6 @@ public class MssqlLookupFilterPlugin
101
107
  Map<String, List<String>> map = new HashMap<>();
102
108
  Connection con = DatabaseConnection.getConnection(task);
103
109
  try {
104
-
105
110
  List<String> targetColumns = task.getMappingTo();
106
111
  List<String> newColumns = new ArrayList<>();
107
112
 
@@ -121,8 +126,12 @@ public class MssqlLookupFilterPlugin
121
126
  columnNeedsToBeFetched += newColumns.get(i);
122
127
  }
123
128
  }
124
- query += columnNeedsToBeFetched + " from " + task.getTableName();
129
+ if (task.getSchemaName().isPresent()) {
130
+ query += columnNeedsToBeFetched + " from " + task.getSchemaName().get() + "." + task.getTableName();
131
+ }else {
132
+ query += columnNeedsToBeFetched + " from " + task.getTableName();
125
133
 
134
+ }
126
135
  Statement stmt = con.createStatement();
127
136
  ResultSet rs = stmt.executeQuery(query);
128
137
 
@@ -233,10 +242,8 @@ public class MssqlLookupFilterPlugin
233
242
  }
234
243
  builder.addRecord();
235
244
  }
236
-
237
245
  }
238
246
 
239
-
240
247
  @Override
241
248
  public void finish() {
242
249
  builder.finish();
@@ -344,14 +351,12 @@ public class MssqlLookupFilterPlugin
344
351
  }else{
345
352
  java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf(matchedData);
346
353
  Instant instant = timestamp.toInstant();
347
- // Timestamp spiTimeStamp = Timestamp.ofInstant(instant);
348
354
  org.embulk.spi.time.Timestamp spiTimeStamp = org.embulk.spi.time.Timestamp.ofInstant(instant);
349
355
  builder.setTimestamp(colNum, spiTimeStamp);
350
356
  }
351
357
  } else {
352
358
  builder.setNull(colNum);
353
359
  }
354
-
355
360
  }
356
361
  }catch (Exception e){
357
362
  e.printStackTrace();
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-filter-mssql_lookup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
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-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -40,16 +40,16 @@ dependencies:
40
40
  version: '12.0'
41
41
  description: Mssql Lookup
42
42
  email:
43
- - ankit.navral@infoobjects.com
43
+ - utkarsh@infoobjects.com
44
44
  executables: []
45
45
  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
- - classpath/embulk-filter-mssql_lookup-0.1.0.jar
52
+ - classpath/embulk-filter-mssql_lookup-0.1.2.jar
53
53
  - classpath/sqljdbc4-4.0.jar
54
54
  - config/checkstyle/checkstyle.xml
55
55
  - config/checkstyle/default.xml
@@ -59,6 +59,7 @@ files:
59
59
  - gradlew
60
60
  - gradlew.bat
61
61
  - lib/embulk/filter/mssql_lookup.rb
62
+ - screenshots/logo.png
62
63
  - src/main/java/org/embulk/filter/mssql_lookup/DatabaseConnection.java
63
64
  - src/main/java/org/embulk/filter/mssql_lookup/MssqlLookupFilterPlugin.java
64
65
  - src/test/java/org/embulk/filter/mssql_lookup/TestMssqlLookupFilterPlugin.java