embulk-output-postgres-udf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 119bb63a7d408ba19802366ad829f6644c5ebdfa
4
+ data.tar.gz: 791d5cb015c132186faba0d45e7fdf6b8329cd0d
5
+ SHA512:
6
+ metadata.gz: 8515ee4cce2a925e880b329e1c9c64b14ffa0f43784ef5a93180958c48def62dc8398cfc87d71f5be3902d49f014a9cc5dcb165d2f799cb6dbef9d6e7ea729d5
7
+ data.tar.gz: aa56b4f09c277780d32d9cd99afbb9d8b1a645fffda249e5b6fee08313094f1274f1c23c2cd2329e4cb6493a6aee59de73882ba3cb7c1f899df282c89ca87ea1
@@ -0,0 +1,8 @@
1
+ *~
2
+ /pkg/
3
+ /tmp/
4
+ .gradle/
5
+ /classpath/
6
+ build/
7
+ .idea
8
+ *.iml
@@ -0,0 +1,21 @@
1
+
2
+ MIT License
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,68 @@
1
+ # PostgreSQL UDF output plugin for Embulk
2
+
3
+ Dumps records to PostgreSQL via user-defined function.
4
+
5
+ ## Overview
6
+
7
+ * **Plugin type**: output
8
+ * **Resume supported**: no
9
+
10
+ ## Configuration
11
+
12
+ - **host**: database host name description (string, required)
13
+ - **port**: database port number (integer, default: 5432)
14
+ - **user**: database login user name (string, required)
15
+ - **password**: database login password (string, default: "")
16
+ - **database**: destination database name (string, required)
17
+ - **schema**: database login password (string, default: "public")
18
+ - **function**: function body (string, required)
19
+ - **language**: function language (string, default: "plpgsql")
20
+
21
+ ## Example
22
+
23
+ ```yaml
24
+ in:
25
+ type: file
26
+ path_prefix: /path/to/csv/sample_
27
+ parser:
28
+ charset: UTF-8
29
+ newline: CRLF
30
+ type: csv
31
+ delimiter: ','
32
+ quote: '"'
33
+ escape: ''
34
+ skip_header_lines: 1
35
+ columns:
36
+ - {name: id, type: long}
37
+ - {name: account, type: long}
38
+ - {name: time, type: timestamp, format: '%Y-%m-%d %H:%M:%S'}
39
+ - {name: purchase, type: timestamp, format: '%Y%m%d'}
40
+ - {name: comment, type: string}
41
+ out:
42
+ type: postgres_udf
43
+ host: localhost
44
+ user: postgres
45
+ database: postgres
46
+ function: |
47
+ begin
48
+ begin
49
+ create table if not exists sample (
50
+ id integer,
51
+ account integer,
52
+ time timestamp,
53
+ purchase timestamp,
54
+ comment text
55
+ );
56
+ exception
57
+ when unique_violation then -- do nothing
58
+ end;
59
+ insert into sample values(id, account, time, purchase, comment);
60
+ end;
61
+ ```
62
+
63
+
64
+ ## Build
65
+
66
+ ```
67
+ $ ./gradlew gem
68
+ ```
@@ -0,0 +1,58 @@
1
+ plugins {
2
+ id "com.jfrog.bintray" version "1.1"
3
+ id "com.github.jruby-gradle.base" version "0.1.5"
4
+ id "java"
5
+ }
6
+ import com.github.jrubygradle.JRubyExec
7
+ repositories {
8
+ mavenCentral()
9
+ jcenter()
10
+ }
11
+ configurations {
12
+ provided
13
+ }
14
+
15
+ version = "0.1.0"
16
+
17
+ dependencies {
18
+ compile "org.embulk:embulk-core:0.5.4"
19
+ provided "org.embulk:embulk-core:0.5.4"
20
+ // compile "YOUR_JAR_DEPENDENCY_GROUP:YOUR_JAR_DEPENDENCY_MODULE:YOUR_JAR_DEPENDENCY_VERSION"
21
+ compile 'org.postgresql:postgresql:9.4-1200-jdbc41'
22
+ testCompile "junit:junit:4.+"
23
+ }
24
+
25
+ task classpath(type: Copy, dependsOn: ["jar"]) {
26
+ doFirst { file("classpath").deleteDir() }
27
+ from (configurations.runtime - configurations.provided + files(jar.archivePath))
28
+ into "classpath"
29
+ }
30
+ clean { delete 'classpath' }
31
+
32
+ task gem(type: JRubyExec, dependsOn: ["build", "gemspec", "classpath"]) {
33
+ jrubyArgs "-rrubygems/gem_runner", "-eGem::GemRunner.new.run(ARGV)", "build"
34
+ script "build/gemspec"
35
+ doLast { ant.move(file: "${project.name}-${project.version}.gem", todir: "pkg") }
36
+ }
37
+
38
+ task gemspec << { file("build/gemspec").write($/
39
+ Gem::Specification.new do |spec|
40
+ spec.name = "${project.name}"
41
+ spec.version = "${project.version}"
42
+ spec.authors = ["kakusuke"]
43
+ spec.summary = %[PostgreSQL UDF output plugin for Embulk]
44
+ spec.description = %[Dumps records to PostgreSQL via user-defined function.]
45
+ spec.email = ["bananafishmonger+github@gmail.com"]
46
+ spec.licenses = ["MIT"]
47
+ spec.homepage = "https://github.com/kakusuke/embulk-output-postgres-udf"
48
+
49
+ spec.files = `git ls-files`.split("\n") + Dir["classpath/*.jar"]
50
+ spec.test_files = spec.files.grep(%r"^(test|spec)/")
51
+ spec.require_paths = ["lib"]
52
+
53
+ #spec.add_dependency 'YOUR_GEM_DEPENDENCY', ['~> YOUR_GEM_DEPENDENCY_VERSION']
54
+ spec.add_development_dependency 'bundler', ['~> 1.0']
55
+ spec.add_development_dependency 'rake', ['>= 10.0']
56
+ end
57
+ /$)
58
+ }
@@ -0,0 +1,6 @@
1
+ #Wed Feb 04 13:46:12 PST 2015
2
+ distributionBase=GRADLE_USER_HOME
3
+ distributionPath=wrapper/dists
4
+ zipStoreBase=GRADLE_USER_HOME
5
+ zipStorePath=wrapper/dists
6
+ distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-bin.zip
data/gradlew ADDED
@@ -0,0 +1,164 @@
1
+ #!/usr/bin/env bash
2
+
3
+ ##############################################################################
4
+ ##
5
+ ## Gradle start up script for UN*X
6
+ ##
7
+ ##############################################################################
8
+
9
+ # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10
+ DEFAULT_JVM_OPTS=""
11
+
12
+ APP_NAME="Gradle"
13
+ APP_BASE_NAME=`basename "$0"`
14
+
15
+ # Use the maximum available, or set MAX_FD != -1 to use that value.
16
+ MAX_FD="maximum"
17
+
18
+ warn ( ) {
19
+ echo "$*"
20
+ }
21
+
22
+ die ( ) {
23
+ echo
24
+ echo "$*"
25
+ echo
26
+ exit 1
27
+ }
28
+
29
+ # OS specific support (must be 'true' or 'false').
30
+ cygwin=false
31
+ msys=false
32
+ darwin=false
33
+ case "`uname`" in
34
+ CYGWIN* )
35
+ cygwin=true
36
+ ;;
37
+ Darwin* )
38
+ darwin=true
39
+ ;;
40
+ MINGW* )
41
+ msys=true
42
+ ;;
43
+ esac
44
+
45
+ # For Cygwin, ensure paths are in UNIX format before anything is touched.
46
+ if $cygwin ; then
47
+ [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48
+ fi
49
+
50
+ # Attempt to set APP_HOME
51
+ # Resolve links: $0 may be a link
52
+ PRG="$0"
53
+ # Need this for relative symlinks.
54
+ while [ -h "$PRG" ] ; do
55
+ ls=`ls -ld "$PRG"`
56
+ link=`expr "$ls" : '.*-> \(.*\)$'`
57
+ if expr "$link" : '/.*' > /dev/null; then
58
+ PRG="$link"
59
+ else
60
+ PRG=`dirname "$PRG"`"/$link"
61
+ fi
62
+ done
63
+ SAVED="`pwd`"
64
+ cd "`dirname \"$PRG\"`/" >&-
65
+ APP_HOME="`pwd -P`"
66
+ cd "$SAVED" >&-
67
+
68
+ CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69
+
70
+ # Determine the Java command to use to start the JVM.
71
+ if [ -n "$JAVA_HOME" ] ; then
72
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73
+ # IBM's JDK on AIX uses strange locations for the executables
74
+ JAVACMD="$JAVA_HOME/jre/sh/java"
75
+ else
76
+ JAVACMD="$JAVA_HOME/bin/java"
77
+ fi
78
+ if [ ! -x "$JAVACMD" ] ; then
79
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80
+
81
+ Please set the JAVA_HOME variable in your environment to match the
82
+ location of your Java installation."
83
+ fi
84
+ else
85
+ JAVACMD="java"
86
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87
+
88
+ Please set the JAVA_HOME variable in your environment to match the
89
+ location of your Java installation."
90
+ fi
91
+
92
+ # Increase the maximum file descriptors if we can.
93
+ if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94
+ MAX_FD_LIMIT=`ulimit -H -n`
95
+ if [ $? -eq 0 ] ; then
96
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97
+ MAX_FD="$MAX_FD_LIMIT"
98
+ fi
99
+ ulimit -n $MAX_FD
100
+ if [ $? -ne 0 ] ; then
101
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
102
+ fi
103
+ else
104
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105
+ fi
106
+ fi
107
+
108
+ # For Darwin, add options to specify how the application appears in the dock
109
+ if $darwin; then
110
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111
+ fi
112
+
113
+ # For Cygwin, switch paths to Windows format before running java
114
+ if $cygwin ; then
115
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117
+
118
+ # We build the pattern for arguments to be converted via cygpath
119
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120
+ SEP=""
121
+ for dir in $ROOTDIRSRAW ; do
122
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
123
+ SEP="|"
124
+ done
125
+ OURCYGPATTERN="(^($ROOTDIRS))"
126
+ # Add a user-defined pattern to the cygpath arguments
127
+ if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128
+ OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129
+ fi
130
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
131
+ i=0
132
+ for arg in "$@" ; do
133
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134
+ CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135
+
136
+ if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137
+ eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138
+ else
139
+ eval `echo args$i`="\"$arg\""
140
+ fi
141
+ i=$((i+1))
142
+ done
143
+ case $i in
144
+ (0) set -- ;;
145
+ (1) set -- "$args0" ;;
146
+ (2) set -- "$args0" "$args1" ;;
147
+ (3) set -- "$args0" "$args1" "$args2" ;;
148
+ (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149
+ (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150
+ (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151
+ (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152
+ (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153
+ (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154
+ esac
155
+ fi
156
+
157
+ # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158
+ function splitJvmOpts() {
159
+ JVM_OPTS=("$@")
160
+ }
161
+ eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162
+ JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163
+
164
+ exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
@@ -0,0 +1,90 @@
1
+ @if "%DEBUG%" == "" @echo off
2
+ @rem ##########################################################################
3
+ @rem
4
+ @rem Gradle startup script for Windows
5
+ @rem
6
+ @rem ##########################################################################
7
+
8
+ @rem Set local scope for the variables with windows NT shell
9
+ if "%OS%"=="Windows_NT" setlocal
10
+
11
+ @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12
+ set DEFAULT_JVM_OPTS=
13
+
14
+ set DIRNAME=%~dp0
15
+ if "%DIRNAME%" == "" set DIRNAME=.
16
+ set APP_BASE_NAME=%~n0
17
+ set APP_HOME=%DIRNAME%
18
+
19
+ @rem Find java.exe
20
+ if defined JAVA_HOME goto findJavaFromJavaHome
21
+
22
+ set JAVA_EXE=java.exe
23
+ %JAVA_EXE% -version >NUL 2>&1
24
+ if "%ERRORLEVEL%" == "0" goto init
25
+
26
+ echo.
27
+ echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28
+ echo.
29
+ echo Please set the JAVA_HOME variable in your environment to match the
30
+ echo location of your Java installation.
31
+
32
+ goto fail
33
+
34
+ :findJavaFromJavaHome
35
+ set JAVA_HOME=%JAVA_HOME:"=%
36
+ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37
+
38
+ if exist "%JAVA_EXE%" goto init
39
+
40
+ echo.
41
+ echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42
+ echo.
43
+ echo Please set the JAVA_HOME variable in your environment to match the
44
+ echo location of your Java installation.
45
+
46
+ goto fail
47
+
48
+ :init
49
+ @rem Get command-line arguments, handling Windowz variants
50
+
51
+ if not "%OS%" == "Windows_NT" goto win9xME_args
52
+ if "%@eval[2+2]" == "4" goto 4NT_args
53
+
54
+ :win9xME_args
55
+ @rem Slurp the command line arguments.
56
+ set CMD_LINE_ARGS=
57
+ set _SKIP=2
58
+
59
+ :win9xME_args_slurp
60
+ if "x%~1" == "x" goto execute
61
+
62
+ set CMD_LINE_ARGS=%*
63
+ goto execute
64
+
65
+ :4NT_args
66
+ @rem Get arguments from the 4NT Shell from JP Software
67
+ set CMD_LINE_ARGS=%$
68
+
69
+ :execute
70
+ @rem Setup the command line
71
+
72
+ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73
+
74
+ @rem Execute Gradle
75
+ "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76
+
77
+ :end
78
+ @rem End local scope for the variables with windows NT shell
79
+ if "%ERRORLEVEL%"=="0" goto mainEnd
80
+
81
+ :fail
82
+ rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83
+ rem the _cmd.exe /c_ return code!
84
+ if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85
+ exit /b 1
86
+
87
+ :mainEnd
88
+ if "%OS%"=="Windows_NT" endlocal
89
+
90
+ :omega
@@ -0,0 +1,3 @@
1
+ Embulk::JavaPlugin.register_output(
2
+ "postgres_udf", "org.embulk.output.PostgresUDFOutputPlugin",
3
+ File.expand_path('../../../../classpath', __FILE__))
@@ -0,0 +1,109 @@
1
+ package org.embulk.output;
2
+
3
+ import org.embulk.spi.Exec;
4
+ import org.embulk.spi.Schema;
5
+ import org.embulk.spi.time.Timestamp;
6
+ import org.embulk.spi.type.Type;
7
+ import org.slf4j.Logger;
8
+
9
+ import java.sql.Connection;
10
+ import java.sql.PreparedStatement;
11
+ import java.sql.SQLException;
12
+ import java.sql.Statement;
13
+
14
+ /**
15
+ * Created by kakusuke on 2015/03/21.
16
+ */
17
+ public class ConnectionWrapper implements AutoCloseable {
18
+ private final Logger logger = Exec.getLogger(ConnectionWrapper.class);
19
+ private final Connection con;
20
+
21
+ public ConnectionWrapper(Connection con, String schemaName, boolean autoCommit) throws SQLException {
22
+ this.con = con;
23
+ con.setAutoCommit(autoCommit);
24
+ if (schemaName != null) {
25
+ Statement stmt = con.createStatement();
26
+ String sql = "SET search_path TO '" + schemaName + "'";
27
+ logger.info("SQL: " + sql);
28
+ stmt.execute(sql);
29
+ }
30
+ }
31
+
32
+ @Override
33
+ public void close() throws Exception {
34
+ con.close();
35
+ }
36
+
37
+ public void createFunction(String functionName, Schema schema, String query, String language) throws SQLException {
38
+ String sql = String.format("CREATE OR REPLACE FUNCTION \"%s\"(%s)\nRETURNS void AS $$\n%s\n$$ LANGUAGE %s",
39
+ functionName,
40
+ getArgumentsString(schema),
41
+ query,
42
+ language
43
+ );
44
+ logger.info("SQL: " + sql);
45
+ con.createStatement().execute(sql);
46
+ }
47
+
48
+ public void dropFunction(String functionName, Schema schema) throws SQLException {
49
+ String sql = String.format("DROP FUNCTION IF EXISTS \"%s\"(%s)",
50
+ functionName,
51
+ getArgumentsString(schema)
52
+ );
53
+ logger.info("SQL: " + sql);
54
+ con.createStatement().execute(sql);
55
+ }
56
+
57
+ private String getArgumentsString(Schema schema) {
58
+ StringBuilder builder = new StringBuilder();
59
+ for (int i = 0, len = schema.getColumnCount(); i < len; i++) {
60
+ if (i > 0) {
61
+ builder.append(", ");
62
+ }
63
+ builder.append("\"" + schema.getColumnName(i) + "\"");
64
+ builder.append(" ");
65
+ builder.append(getSqlTypeName(schema.getColumnType(i)));
66
+ }
67
+ return builder.toString();
68
+ }
69
+
70
+ public PreparedStatement prepareCall(String functionName, Schema schema) throws SQLException {
71
+ StringBuilder builder = new StringBuilder();
72
+ for (int i = 0, len = schema.getColumnCount(); i < len; i++) {
73
+ if (i > 0) {
74
+ builder.append(", ");
75
+ }
76
+ builder.append("?::" + getSqlTypeName(schema.getColumnType(i)));
77
+ }
78
+ String sql = String.format("{ call \"%s\"(%s) }", functionName, builder.toString());
79
+ logger.info("SQL: " + sql);
80
+ return con.prepareCall(sql);
81
+ }
82
+
83
+ public void commit() throws SQLException {
84
+ con.commit();
85
+ }
86
+
87
+ public void rollback() throws SQLException {
88
+ con.rollback();
89
+ }
90
+
91
+ private String getSqlTypeName(Type columnType) {
92
+ if (columnType.getJavaType().equals(long.class)) {
93
+ return "bigint";
94
+ }
95
+ else if (columnType.getJavaType().equals(double.class)) {
96
+ return "float8";
97
+ }
98
+ else if (columnType.getJavaType().equals(boolean.class)) {
99
+ return "boolean";
100
+ }
101
+ else if (columnType.getJavaType().equals(String.class)) {
102
+ return "text";
103
+ }
104
+ else if (columnType.getJavaType().equals(Timestamp.class)) {
105
+ return "timestamp";
106
+ }
107
+ return "unknown";
108
+ }
109
+ }
@@ -0,0 +1,59 @@
1
+ package org.embulk.output;
2
+
3
+ import com.google.common.base.Throwables;
4
+
5
+ import java.sql.*;
6
+ import java.util.Properties;
7
+
8
+ /**
9
+ * Created by kakusuke on 2015/03/21.
10
+ */
11
+ public class PostgresUDFConnector {
12
+ private static final Driver driver = new org.postgresql.Driver();
13
+
14
+ private final String url;
15
+ private final Properties properties;
16
+ private final String schemaName;
17
+
18
+ public PostgresUDFConnector(String url, Properties properties, String schemaName) {
19
+ this.url = url;
20
+ this.properties = properties;
21
+ this.schemaName = schemaName;
22
+ }
23
+
24
+ public ConnectionWrapper connect(boolean autoCommit) throws SQLException {
25
+ Connection c = createConnection();
26
+ try {
27
+ ConnectionWrapper con = new ConnectionWrapper(c, schemaName, autoCommit);
28
+ c = null;
29
+ return con;
30
+ } finally {
31
+ if (c != null) {
32
+ c.close();
33
+ }
34
+ }
35
+ }
36
+
37
+ private Connection createConnection() throws SQLException {
38
+ SQLException firstException = null;
39
+ int count = 0;
40
+ while(count < 10) {
41
+ count++;
42
+
43
+ try {
44
+ return driver.connect(url, properties);
45
+ }
46
+ catch (SQLRecoverableException | SQLTimeoutException e) {
47
+ if (firstException == null) {
48
+ firstException = e;
49
+ }
50
+ try {
51
+ Thread.sleep(300);
52
+ } catch (InterruptedException e2) {
53
+ throw Throwables.propagate(e2);
54
+ }
55
+ }
56
+ }
57
+ throw firstException;
58
+ }
59
+ }
@@ -0,0 +1,209 @@
1
+ package org.embulk.output;
2
+
3
+ import com.google.common.base.Throwables;
4
+ import org.embulk.config.*;
5
+ import org.embulk.spi.*;
6
+ import org.embulk.spi.time.Timestamp;
7
+ import org.slf4j.Logger;
8
+
9
+ import java.sql.PreparedStatement;
10
+ import java.sql.SQLException;
11
+ import java.util.List;
12
+ import java.util.Properties;
13
+
14
+ public class PostgresUDFOutputPlugin implements OutputPlugin {
15
+ public interface PluginTask extends Task {
16
+ @Config("host")
17
+ public String getHost();
18
+
19
+ @Config("port")
20
+ @ConfigDefault("5432")
21
+ public int getPort();
22
+
23
+ @Config("user")
24
+ public String getUser();
25
+
26
+ @Config("password")
27
+ @ConfigDefault("\"\"")
28
+ public String getPassword();
29
+
30
+ @Config("database")
31
+ public String getDatabase();
32
+
33
+ @Config("schema")
34
+ @ConfigDefault("\"public\"")
35
+ public String getSchema();
36
+
37
+ @Config("function")
38
+ public String getFunction();
39
+
40
+ @Config("language")
41
+ @ConfigDefault("\"plpgsql\"")
42
+ public String getLanguage();
43
+
44
+ /*
45
+ @Config("batch_size")
46
+ public int getBatchSize();
47
+
48
+ @Config("options")
49
+ @ConfigDefault("{}")
50
+ public Properties getOptions();
51
+ */
52
+ }
53
+
54
+ @Override
55
+ public ConfigDiff transaction(ConfigSource config, Schema schema, int taskCount, OutputPlugin.Control control) {
56
+ PluginTask task = config.loadConfig(PluginTask.class);
57
+
58
+ // retryable (idempotent) output:
59
+ // return resume(task.dump(), schema, taskCount, control);
60
+
61
+ // non-retryable (non-idempotent) output:
62
+ try {
63
+ ConnectionWrapper con = getConnector(task).connect(true);
64
+ con.createFunction(getFunctionName(), schema, task.getFunction(), task.getLanguage());
65
+ }
66
+ catch (Exception e) {
67
+ throw Throwables.propagate(e);
68
+ }
69
+ control.run(task.dump());
70
+ return Exec.newConfigDiff();
71
+ }
72
+
73
+ @Override
74
+ public ConfigDiff resume(TaskSource taskSource, Schema schema, int taskCount, OutputPlugin.Control control) {
75
+ throw new UnsupportedOperationException("postgres-udf output plugin does not support resuming");
76
+ }
77
+
78
+ @Override
79
+ public void cleanup(TaskSource taskSource, Schema schema, int taskCount, List<CommitReport> successCommitReports) {
80
+ PluginTask task = taskSource.loadTask(PluginTask.class);
81
+ try {
82
+ ConnectionWrapper con = getConnector(task).connect(true);
83
+ con.dropFunction(getFunctionName(), schema);
84
+ } catch (SQLException e) {
85
+ e.printStackTrace();
86
+ }
87
+ }
88
+
89
+ @Override
90
+ public TransactionalPageOutput open(TaskSource taskSource, Schema schema, int taskIndex) {
91
+ PluginTask task = taskSource.loadTask(PluginTask.class);
92
+ return new PluginPageOutput(task, schema);
93
+ }
94
+
95
+ private static PostgresUDFConnector getConnector(PluginTask task) {
96
+ String url = String.format("jdbc:postgresql://%s:%d/%s",
97
+ task.getHost(), task.getPort(), task.getDatabase());
98
+
99
+ Properties props = new Properties();
100
+ props.setProperty("user", task.getUser());
101
+ props.setProperty("password", task.getPassword());
102
+ props.setProperty("loginTimeout", "300"); // seconds
103
+ props.setProperty("socketTimeout", "28800"); // seconds
104
+ props.setProperty("tcpKeepAlive", "true");
105
+ return new PostgresUDFConnector(url, props, task.getSchema());
106
+ }
107
+
108
+ private static String getFunctionName() {
109
+ Timestamp t = Exec.session().getTransactionTime();
110
+ return String.format("fn_%016x%08x", t.getEpochSecond(), t.getNano());
111
+ }
112
+
113
+ static class PluginPageOutput implements TransactionalPageOutput {
114
+ private final Logger logger;
115
+ private final PluginTask task;
116
+ private final Schema schema;
117
+ private final PageReader pageReader;
118
+ private final ConnectionWrapper connection;
119
+
120
+ PluginPageOutput(PluginTask task, Schema schema) {
121
+ this.logger = Exec.getLogger(PluginPageOutput.class);
122
+ this.task = task;
123
+ this.schema = schema;
124
+ this.pageReader = new PageReader(schema);
125
+ try {
126
+ this.connection = getConnector(task).connect(false);
127
+ }
128
+ catch (SQLException e) {
129
+ throw Throwables.propagate(e);
130
+ }
131
+ }
132
+
133
+ @Override
134
+ public void add(Page page) {
135
+ long startTime = System.currentTimeMillis();
136
+ pageReader.setPage(page);
137
+
138
+ try (PreparedStatement stmt = this.connection.prepareCall(getFunctionName(), schema)) {
139
+ while (pageReader.nextRecord()) {
140
+ for (int i = 0; i < schema.getColumnCount(); i++) {
141
+ Class<?> type = schema.getColumnType(i).getJavaType();
142
+ if (type.equals(boolean.class)) {
143
+ stmt.setBoolean(i + 1, pageReader.getBoolean(i));
144
+ } else if (type.equals(double.class)) {
145
+ stmt.setDouble(i + 1, pageReader.getDouble(i));
146
+ } else if (type.equals(long.class)) {
147
+ stmt.setLong(i + 1, pageReader.getLong(i));
148
+ } else if (type.equals(String.class)) {
149
+ stmt.setString(i + 1, pageReader.getString(i));
150
+ } else if (type.equals(Timestamp.class)) {
151
+ stmt.setTimestamp(i + 1, new java.sql.Timestamp(pageReader.getTimestamp(i).toEpochMilli()));
152
+ } else {
153
+ stmt.setObject(i + 1, null);
154
+ }
155
+ }
156
+ stmt.addBatch();
157
+ }
158
+ stmt.executeBatch();
159
+ long endTime = System.currentTimeMillis();
160
+ logger.info("> {} seconds.", (endTime - startTime) / 1000.0);
161
+ }
162
+ catch (SQLException e) {
163
+ throw Throwables.propagate(e);
164
+ }
165
+ }
166
+
167
+ @Override
168
+ public void finish() {
169
+ try {
170
+ connection.commit();
171
+ }
172
+ catch (Exception e) {
173
+ throw Throwables.propagate(e);
174
+ }
175
+ }
176
+
177
+ @Override
178
+ public void close() {
179
+ try {
180
+ connection.close();
181
+ }
182
+ catch (Exception e) {
183
+ throw Throwables.propagate(e);
184
+ }
185
+ }
186
+
187
+ @Override
188
+ public void abort() {
189
+ try {
190
+ connection.rollback();
191
+ }
192
+ catch (Exception e) {
193
+ throw Throwables.propagate(e);
194
+ }
195
+ }
196
+
197
+ @Override
198
+ public CommitReport commit() {
199
+ try {
200
+ connection.commit();
201
+ }
202
+ catch (Exception e) {
203
+ throw Throwables.propagate(e);
204
+ }
205
+ return Exec.newCommitReport();
206
+ }
207
+
208
+ }
209
+ }
@@ -0,0 +1,5 @@
1
+ package org.embulk.output;
2
+
3
+ public class TestPostgresUDFOutputPlugin
4
+ {
5
+ }
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: embulk-output-postgres-udf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kakusuke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ~>
17
+ - !ruby/object:Gem::Version
18
+ version: '1.0'
19
+ name: bundler
20
+ prerelease: false
21
+ type: :development
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '10.0'
33
+ name: rake
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Dumps records to PostgreSQL via user-defined function.
42
+ email:
43
+ - bananafishmonger+github@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - LICENSE.txt
50
+ - README.md
51
+ - build.gradle
52
+ - gradle/wrapper/gradle-wrapper.jar
53
+ - gradle/wrapper/gradle-wrapper.properties
54
+ - gradlew
55
+ - gradlew.bat
56
+ - lib/embulk/output/postgres_udf.rb
57
+ - src/main/java/org/embulk/output/ConnectionWrapper.java
58
+ - src/main/java/org/embulk/output/PostgresUDFConnector.java
59
+ - src/main/java/org/embulk/output/PostgresUDFOutputPlugin.java
60
+ - src/test/java/org/embulk/output/TestPostgresUDFOutputPlugin.java
61
+ - classpath/embulk-output-postgres-udf-0.1.0.jar
62
+ - classpath/jna-4.1.0.jar
63
+ - classpath/jna-platform-4.1.0.jar
64
+ - classpath/postgresql-9.4-1200-jdbc41.jar
65
+ - classpath/slf4j-simple-1.7.7.jar
66
+ - classpath/waffle-jna-1.7.jar
67
+ homepage: https://github.com/kakusuke/embulk-output-postgres-udf
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.1.9
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: PostgreSQL UDF output plugin for Embulk
91
+ test_files: []