embulk-output-snowflake 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ #Wed Jun 24 17:19:08 JST 2020
2
+ distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
3
+ distributionBase=GRADLE_USER_HOME
4
+ distributionPath=wrapper/dists
5
+ zipStorePath=wrapper/dists
6
+ zipStoreBase=GRADLE_USER_HOME
data/gradlew ADDED
@@ -0,0 +1,172 @@
1
+ #!/usr/bin/env sh
2
+
3
+ ##############################################################################
4
+ ##
5
+ ## Gradle start up script for UN*X
6
+ ##
7
+ ##############################################################################
8
+
9
+ # Attempt to set APP_HOME
10
+ # Resolve links: $0 may be a link
11
+ PRG="$0"
12
+ # Need this for relative symlinks.
13
+ while [ -h "$PRG" ] ; do
14
+ ls=`ls -ld "$PRG"`
15
+ link=`expr "$ls" : '.*-> \(.*\)$'`
16
+ if expr "$link" : '/.*' > /dev/null; then
17
+ PRG="$link"
18
+ else
19
+ PRG=`dirname "$PRG"`"/$link"
20
+ fi
21
+ done
22
+ SAVED="`pwd`"
23
+ cd "`dirname \"$PRG\"`/" >/dev/null
24
+ APP_HOME="`pwd -P`"
25
+ cd "$SAVED" >/dev/null
26
+
27
+ APP_NAME="Gradle"
28
+ APP_BASE_NAME=`basename "$0"`
29
+
30
+ # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31
+ DEFAULT_JVM_OPTS=""
32
+
33
+ # Use the maximum available, or set MAX_FD != -1 to use that value.
34
+ MAX_FD="maximum"
35
+
36
+ warn () {
37
+ echo "$*"
38
+ }
39
+
40
+ die () {
41
+ echo
42
+ echo "$*"
43
+ echo
44
+ exit 1
45
+ }
46
+
47
+ # OS specific support (must be 'true' or 'false').
48
+ cygwin=false
49
+ msys=false
50
+ darwin=false
51
+ nonstop=false
52
+ case "`uname`" in
53
+ CYGWIN* )
54
+ cygwin=true
55
+ ;;
56
+ Darwin* )
57
+ darwin=true
58
+ ;;
59
+ MINGW* )
60
+ msys=true
61
+ ;;
62
+ NONSTOP* )
63
+ nonstop=true
64
+ ;;
65
+ esac
66
+
67
+ CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68
+
69
+ # Determine the Java command to use to start the JVM.
70
+ if [ -n "$JAVA_HOME" ] ; then
71
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72
+ # IBM's JDK on AIX uses strange locations for the executables
73
+ JAVACMD="$JAVA_HOME/jre/sh/java"
74
+ else
75
+ JAVACMD="$JAVA_HOME/bin/java"
76
+ fi
77
+ if [ ! -x "$JAVACMD" ] ; then
78
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79
+
80
+ Please set the JAVA_HOME variable in your environment to match the
81
+ location of your Java installation."
82
+ fi
83
+ else
84
+ JAVACMD="java"
85
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86
+
87
+ Please set the JAVA_HOME variable in your environment to match the
88
+ location of your Java installation."
89
+ fi
90
+
91
+ # Increase the maximum file descriptors if we can.
92
+ if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93
+ MAX_FD_LIMIT=`ulimit -H -n`
94
+ if [ $? -eq 0 ] ; then
95
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96
+ MAX_FD="$MAX_FD_LIMIT"
97
+ fi
98
+ ulimit -n $MAX_FD
99
+ if [ $? -ne 0 ] ; then
100
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
101
+ fi
102
+ else
103
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104
+ fi
105
+ fi
106
+
107
+ # For Darwin, add options to specify how the application appears in the dock
108
+ if $darwin; then
109
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110
+ fi
111
+
112
+ # For Cygwin, switch paths to Windows format before running java
113
+ if $cygwin ; then
114
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116
+ JAVACMD=`cygpath --unix "$JAVACMD"`
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
+ # Escape application args
158
+ save () {
159
+ for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160
+ echo " "
161
+ }
162
+ APP_ARGS=$(save "$@")
163
+
164
+ # Collect all arguments for the java command, following the shell quoting and substitution rules
165
+ eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166
+
167
+ # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168
+ if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169
+ cd "$(dirname "$0")"
170
+ fi
171
+
172
+ exec "$JAVACMD" "$@"
@@ -0,0 +1,84 @@
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
+ set DIRNAME=%~dp0
12
+ if "%DIRNAME%" == "" set DIRNAME=.
13
+ set APP_BASE_NAME=%~n0
14
+ set APP_HOME=%DIRNAME%
15
+
16
+ @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17
+ set DEFAULT_JVM_OPTS=
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 Windows variants
50
+
51
+ if not "%OS%" == "Windows_NT" goto win9xME_args
52
+
53
+ :win9xME_args
54
+ @rem Slurp the command line arguments.
55
+ set CMD_LINE_ARGS=
56
+ set _SKIP=2
57
+
58
+ :win9xME_args_slurp
59
+ if "x%~1" == "x" goto execute
60
+
61
+ set CMD_LINE_ARGS=%*
62
+
63
+ :execute
64
+ @rem Setup the command line
65
+
66
+ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67
+
68
+ @rem Execute Gradle
69
+ "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70
+
71
+ :end
72
+ @rem End local scope for the variables with windows NT shell
73
+ if "%ERRORLEVEL%"=="0" goto mainEnd
74
+
75
+ :fail
76
+ rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77
+ rem the _cmd.exe /c_ return code!
78
+ if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79
+ exit /b 1
80
+
81
+ :mainEnd
82
+ if "%OS%"=="Windows_NT" endlocal
83
+
84
+ :omega
@@ -1,3 +1,3 @@
1
1
  Embulk::JavaPlugin.register_output(
2
- :snowflake, "org.embulk.output.SnowflakeOutputPlugin",
2
+ "snowflake", "org.embulk.output.SnowflakeOutputPlugin",
3
3
  File.expand_path('../../../../classpath', __FILE__))
@@ -1,165 +1,137 @@
1
1
  package org.embulk.output;
2
2
 
3
- import java.sql.*;
4
- import java.util.Properties;
5
- import java.util.List;
6
3
  import java.io.IOException;
7
- import java.util.Locale;
4
+ import java.sql.SQLException;
5
+ import java.util.Properties;
8
6
 
9
7
  import com.google.common.base.Optional;
10
8
  import com.google.common.collect.ImmutableSet;
11
- import com.google.common.collect.ImmutableList;
12
-
13
-
14
- import org.embulk.config.Config;
15
- import org.embulk.config.ConfigDefault;
9
+ import org.embulk.config.*;
16
10
  import org.embulk.output.jdbc.*;
17
- import org.embulk.output.snowflake.SnowflakeOutputConnector;
11
+ import org.embulk.output.snowflake.*;
12
+ import org.embulk.spi.OutputPlugin;
18
13
  import org.embulk.spi.Schema;
19
- import org.embulk.output.snowflake.SnowflakeOutputConnection;
20
14
 
21
15
  public class SnowflakeOutputPlugin
22
16
  extends AbstractJdbcOutputPlugin
23
17
  {
24
- public interface GenericPluginTask extends PluginTask
25
- {
18
+ private StageIdentifier stageIdentifier;
19
+
20
+ public interface SnowflakePluginTask extends PluginTask {
26
21
  @Config("driver_path")
27
22
  @ConfigDefault("null")
28
23
  public Optional<String> getDriverPath();
29
24
 
30
- @Config("url")
31
- public String getUrl();
25
+ @Config("host")
26
+ public String getHost();
32
27
 
33
28
  @Config("user")
34
- @ConfigDefault("null")
35
- public Optional<String> getUser();
29
+ @ConfigDefault("\"KEN\"")
30
+ public String getUser();
36
31
 
37
32
  @Config("password")
38
- @ConfigDefault("null")
39
- public Optional<String> getPassword();
33
+ @ConfigDefault("\"\"")
34
+ public String getPassword();
35
+
36
+ @Config("database")
37
+ public String getDatabase();
38
+
39
+ @Config("warehouse")
40
+ public String getWarehouse();
40
41
 
41
42
  @Config("schema")
42
- @ConfigDefault("null")
43
- public Optional<String> getSchema();
43
+ @ConfigDefault("\"public\"")
44
+ public String getSchema();
44
45
 
45
- @Config("max_table_name_length")
46
- @ConfigDefault("30")
47
- public int getMaxTableNameLength();
46
+ @Config("delete_stage")
47
+ @ConfigDefault("false")
48
+ public boolean getDeleteStage();
48
49
  }
49
50
 
50
51
  @Override
51
52
  protected Class<? extends PluginTask> getTaskClass()
52
53
  {
53
- return GenericPluginTask.class;
54
+ return SnowflakePluginTask.class;
54
55
  }
55
56
 
56
57
  @Override
57
58
  protected Features getFeatures(PluginTask task)
58
59
  {
59
- GenericPluginTask t = (GenericPluginTask) task;
60
60
  return new Features()
61
- .setMaxTableNameLength(t.getMaxTableNameLength())
62
- .setSupportedModes(ImmutableSet.of(Mode.INSERT, Mode.INSERT_DIRECT, Mode.TRUNCATE_INSERT, Mode.REPLACE));
61
+ .setMaxTableNameLength(127)
62
+ .setSupportedModes(ImmutableSet.of(Mode.INSERT, Mode.INSERT_DIRECT, Mode.TRUNCATE_INSERT, Mode.REPLACE))
63
+ .setIgnoreMergeKeys(false);
63
64
  }
64
-
65
65
  @Override
66
- protected SnowflakeOutputConnector getConnector(PluginTask task, boolean retryableMetadataOperation)
67
- {
68
- GenericPluginTask t = (GenericPluginTask) task;
66
+ protected JdbcOutputConnector getConnector(PluginTask task, boolean retryableMetadataOperation) {
67
+ SnowflakePluginTask t = (SnowflakePluginTask) task;
69
68
 
70
- if (t.getDriverPath().isPresent()) {
71
- addDriverJarToClasspath(t.getDriverPath().get());
72
- }
69
+ loadDriver("net.snowflake.client.jdbc.SnowflakeDriver", t.getDriverPath());
70
+
71
+ String url = String.format("jdbc:snowflake://%s", t.getHost());
73
72
 
74
73
  Properties props = new Properties();
75
74
 
75
+ props.setProperty("user", t.getUser());
76
+ props.setProperty("password", t.getPassword());
77
+ props.setProperty("warehouse", t.getWarehouse());
78
+ props.setProperty("db", t.getDatabase());
79
+ props.setProperty("schema", t.getSchema());
80
+
76
81
  props.putAll(t.getOptions());
77
82
 
78
- if (t.getUser().isPresent()) {
79
- props.setProperty("user", t.getUser().get());
80
- }
81
- if (t.getPassword().isPresent()) {
82
- props.setProperty("password", t.getPassword().get());
83
- }
84
- logConnectionProperties(t.getUrl(), props);
83
+ logConnectionProperties(url, props);
85
84
 
86
- return new SnowflakeOutputConnector(t.getUrl(), props,
87
- t.getSchema().orNull(), t.getTransactionIsolation());
85
+ return new SnowflakeOutputConnector(url, props, t.getTransactionIsolation());
88
86
  }
89
87
 
90
88
  @Override
91
- protected BatchInsert newBatchInsert(PluginTask task, Optional<MergeConfig> mergeConfig) throws IOException, SQLException
89
+ public ConfigDiff resume(TaskSource taskSource,
90
+ Schema schema, int taskCount,
91
+ OutputPlugin.Control control)
92
92
  {
93
- return new StandardBatchInsert(getConnector(task, true), mergeConfig);
93
+ throw new UnsupportedOperationException("snowflake output plugin does not support resuming");
94
94
  }
95
95
 
96
96
  @Override
97
97
  protected void doBegin(JdbcOutputConnection con,
98
98
  PluginTask task, final Schema schema, int taskCount) throws SQLException
99
99
  {
100
+ super.doBegin(con,task,schema,taskCount);
101
+ }
102
+
100
103
 
104
+ @Override
105
+ protected void doCommit(JdbcOutputConnection con, PluginTask task, int taskCount)
106
+ throws SQLException {
107
+ super.doCommit(con, task, taskCount);
101
108
  SnowflakeOutputConnection snowflakeCon = (SnowflakeOutputConnection)con;
102
109
 
103
- super.doBegin(snowflakeCon, task, schema, taskCount);
110
+ SnowflakePluginTask t = (SnowflakePluginTask) task;
111
+ if (this.stageIdentifier == null) {
112
+ this.stageIdentifier = StageIdentifierHolder.getStageIdentifier(t);
113
+ }
114
+
115
+ if (t.getDeleteStage()){
116
+ snowflakeCon.runDropStage(this.stageIdentifier);
117
+ }
104
118
  }
105
119
 
106
120
  @Override
107
- public Optional<JdbcSchema> newJdbcSchemaFromTableIfExists(JdbcOutputConnection connection,
108
- TableIdentifier table) throws SQLException
121
+ protected BatchInsert newBatchInsert(PluginTask task, Optional<MergeConfig> mergeConfig) throws IOException, SQLException
109
122
  {
110
- if (!connection.tableExists(table)) {
111
- // DatabaseMetaData.getPrimaryKeys fails if table does not exist
112
- return Optional.absent();
123
+ if (mergeConfig.isPresent()) {
124
+ throw new UnsupportedOperationException("Snowflake output plugin doesn't support 'merge_direct' mode.");
113
125
  }
114
126
 
115
- DatabaseMetaData dbm = connection.getMetaData();
116
- String escape = dbm.getSearchStringEscape();
117
- String catalog = dbm.getConnection().getCatalog();
118
-
119
- ResultSet rs = dbm.getPrimaryKeys(catalog, table.getSchemaName(), table.getTableName());
120
- ImmutableSet.Builder<String> primaryKeysBuilder = ImmutableSet.builder();
121
- try {
122
- while(rs.next()) {
123
- primaryKeysBuilder.add(rs.getString("COLUMN_NAME"));
124
- }
125
- } finally {
126
- rs.close();
127
- }
128
- ImmutableSet<String> primaryKeys = primaryKeysBuilder.build();
129
-
130
- ImmutableList.Builder<JdbcColumn> builder = ImmutableList.builder();
131
- rs = dbm.getColumns(
132
- catalog,
133
- JdbcUtils.escapeSearchString(table.getSchemaName(), escape),
134
- JdbcUtils.escapeSearchString(table.getTableName(), escape),
135
- null);
136
- try {
137
- while (rs.next()) {
138
- String columnName = rs.getString("COLUMN_NAME");
139
- String simpleTypeName = rs.getString("TYPE_NAME").toUpperCase(Locale.ENGLISH);
140
- boolean isUniqueKey = primaryKeys.contains(columnName);
141
- int sqlType = rs.getInt("DATA_TYPE");
142
- int colSize = rs.getInt("COLUMN_SIZE");
143
- int decDigit = rs.getInt("DECIMAL_DIGITS");
144
- if (rs.wasNull()) {
145
- decDigit = -1;
146
- }
147
- int charOctetLength = rs.getInt("CHAR_OCTET_LENGTH");
148
- boolean isNotNull = "NO".equals(rs.getString("IS_NULLABLE"));
149
- //rs.getString("COLUMN_DEF") // or null // TODO
150
- builder.add(JdbcColumn.newGenericTypeColumn(
151
- columnName, sqlType, simpleTypeName, colSize, decDigit, charOctetLength, isNotNull, isUniqueKey));
152
- // We can't get declared column name using JDBC API.
153
- // Subclasses need to overwrite it.
154
- }
155
- } finally {
156
- rs.close();
157
- }
158
- List<JdbcColumn> columns = builder.build();
159
- if (columns.isEmpty()) {
160
- return Optional.absent();
161
- } else {
162
- return Optional.of(new JdbcSchema(columns));
127
+ SnowflakePluginTask t = (SnowflakePluginTask) task;
128
+ // TODO: put some where executes once
129
+ if (this.stageIdentifier == null){
130
+ SnowflakeOutputConnection snowflakeCon = (SnowflakeOutputConnection) getConnector(task, true).connect(true);
131
+ this.stageIdentifier = StageIdentifierHolder.getStageIdentifier(t);
132
+ snowflakeCon.runCreateStage(this.stageIdentifier);
163
133
  }
134
+
135
+ return new SnowflakeCopyBatchInsert(getConnector(task, true), this.stageIdentifier,false);
164
136
  }
165
137
  }