embulk-output-teradata 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.DS_Store +0 -0
  3. data/README.md +0 -8
  4. data/build.gradle +5 -2
  5. data/embulk-output-teradata.iml +12 -0
  6. data/gradle/wrapper/gradle-wrapper.properties +2 -2
  7. data/gradlew +0 -0
  8. data/lib/embulk-output-jdbc-0.7.11.jar +0 -0
  9. data/src/main/java/org/embulk/output/teradata/TeradataOutputPlugin.java +13 -52
  10. data/src/main/java/org/embulk/output/teradata/jdbc/TeradataBatchInsert.java +37 -0
  11. data/src/main/java/org/embulk/output/teradata/jdbc/TeradataOutputConnection.java +307 -0
  12. data/src/main/java/org/embulk/output/teradata/jdbc/TeradataOutputConnector.java +46 -0
  13. metadata +10 -40
  14. data/src/main/java/org/embulk/output/teradata/JdbcOutputPlugin.java +0 -134
  15. data/src/main/java/org/embulk/output/teradata/TeradataOutputConnection.java +0 -19
  16. data/src/main/java/org/embulk/output/teradata/jdbc/AbstractJdbcOutputPlugin.java +0 -1144
  17. data/src/main/java/org/embulk/output/teradata/jdbc/BatchInsert.java +0 -52
  18. data/src/main/java/org/embulk/output/teradata/jdbc/JdbcColumn.java +0 -134
  19. data/src/main/java/org/embulk/output/teradata/jdbc/JdbcColumnOption.java +0 -34
  20. data/src/main/java/org/embulk/output/teradata/jdbc/JdbcOutputConnection.java +0 -549
  21. data/src/main/java/org/embulk/output/teradata/jdbc/JdbcOutputConnector.java +0 -8
  22. data/src/main/java/org/embulk/output/teradata/jdbc/JdbcSchema.java +0 -79
  23. data/src/main/java/org/embulk/output/teradata/jdbc/JdbcUtils.java +0 -153
  24. data/src/main/java/org/embulk/output/teradata/jdbc/MergeConfig.java +0 -23
  25. data/src/main/java/org/embulk/output/teradata/jdbc/StandardBatchInsert.java +0 -201
  26. data/src/main/java/org/embulk/output/teradata/jdbc/TimestampFormat.java +0 -37
  27. data/src/main/java/org/embulk/output/teradata/jdbc/ToString.java +0 -54
  28. data/src/main/java/org/embulk/output/teradata/jdbc/ToStringMap.java +0 -34
  29. data/src/main/java/org/embulk/output/teradata/jdbc/setter/BigDecimalColumnSetter.java +0 -76
  30. data/src/main/java/org/embulk/output/teradata/jdbc/setter/BooleanColumnSetter.java +0 -61
  31. data/src/main/java/org/embulk/output/teradata/jdbc/setter/ByteColumnSetter.java +0 -84
  32. data/src/main/java/org/embulk/output/teradata/jdbc/setter/ColumnSetter.java +0 -48
  33. data/src/main/java/org/embulk/output/teradata/jdbc/setter/ColumnSetterFactory.java +0 -199
  34. data/src/main/java/org/embulk/output/teradata/jdbc/setter/ColumnSetterVisitor.java +0 -110
  35. data/src/main/java/org/embulk/output/teradata/jdbc/setter/DefaultValueSetter.java +0 -50
  36. data/src/main/java/org/embulk/output/teradata/jdbc/setter/DoubleColumnSetter.java +0 -68
  37. data/src/main/java/org/embulk/output/teradata/jdbc/setter/FloatColumnSetter.java +0 -68
  38. data/src/main/java/org/embulk/output/teradata/jdbc/setter/IntColumnSetter.java +0 -84
  39. data/src/main/java/org/embulk/output/teradata/jdbc/setter/JsonColumnSetter.java +0 -61
  40. data/src/main/java/org/embulk/output/teradata/jdbc/setter/LongColumnSetter.java +0 -80
  41. data/src/main/java/org/embulk/output/teradata/jdbc/setter/NStringColumnSetter.java +0 -66
  42. data/src/main/java/org/embulk/output/teradata/jdbc/setter/NullColumnSetter.java +0 -61
  43. data/src/main/java/org/embulk/output/teradata/jdbc/setter/NullDefaultValueSetter.java +0 -111
  44. data/src/main/java/org/embulk/output/teradata/jdbc/setter/PassThroughColumnSetter.java +0 -66
  45. data/src/main/java/org/embulk/output/teradata/jdbc/setter/ShortColumnSetter.java +0 -84
  46. data/src/main/java/org/embulk/output/teradata/jdbc/setter/SkipColumnSetter.java +0 -49
  47. data/src/main/java/org/embulk/output/teradata/jdbc/setter/SqlDateColumnSetter.java +0 -66
  48. data/src/main/java/org/embulk/output/teradata/jdbc/setter/SqlTimeColumnSetter.java +0 -66
  49. data/src/main/java/org/embulk/output/teradata/jdbc/setter/SqlTimestampColumnSetter.java +0 -66
  50. data/src/main/java/org/embulk/output/teradata/jdbc/setter/StringColumnSetter.java +0 -66
@@ -1,8 +0,0 @@
1
- package org.embulk.output.jdbc;
2
-
3
- import java.sql.SQLException;
4
-
5
- public interface JdbcOutputConnector
6
- {
7
- public JdbcOutputConnection connect(boolean autoCommit) throws SQLException;
8
- }
@@ -1,79 +0,0 @@
1
- package org.embulk.output.jdbc;
2
-
3
- import java.util.List;
4
-
5
- import org.embulk.config.ConfigException;
6
-
7
- import com.google.common.base.Optional;
8
- import com.google.common.collect.ImmutableList;
9
- import com.fasterxml.jackson.annotation.JsonCreator;
10
- import com.fasterxml.jackson.annotation.JsonValue;
11
-
12
- public class JdbcSchema
13
- {
14
- private List<JdbcColumn> columns;
15
-
16
- @JsonCreator
17
- public JdbcSchema(List<JdbcColumn> columns)
18
- {
19
- this.columns = columns;
20
- }
21
-
22
- @JsonValue
23
- public List<JdbcColumn> getColumns()
24
- {
25
- return columns;
26
- }
27
-
28
- public Optional<JdbcColumn> findColumn(String name)
29
- {
30
- // because both upper case column and lower case column may exist, search twice
31
- for (JdbcColumn column : columns) {
32
- if (column.getName().equals(name)) {
33
- return Optional.of(column);
34
- }
35
- }
36
-
37
- JdbcColumn foundColumn = null;
38
- for (JdbcColumn column : columns) {
39
- if (column.getName().equalsIgnoreCase(name)) {
40
- if (foundColumn != null) {
41
- throw new ConfigException(String.format("Cannot specify column '%s' because both '%s' and '%s' exist.",
42
- name, foundColumn.getName(), column.getName()));
43
- }
44
- foundColumn = column;
45
- }
46
- }
47
-
48
- if (foundColumn != null) {
49
- return Optional.of(foundColumn);
50
- }
51
- return Optional.absent();
52
- }
53
-
54
- public int getCount()
55
- {
56
- return columns.size();
57
- }
58
-
59
- public JdbcColumn getColumn(int i)
60
- {
61
- return columns.get(i);
62
- }
63
-
64
- public String getColumnName(int i)
65
- {
66
- return columns.get(i).getName();
67
- }
68
-
69
- public static JdbcSchema filterSkipColumns(JdbcSchema schema)
70
- {
71
- ImmutableList.Builder<JdbcColumn> builder = ImmutableList.builder();
72
- for (JdbcColumn c : schema.getColumns()) {
73
- if (!c.isSkipColumn()) {
74
- builder.add(c);
75
- }
76
- }
77
- return new JdbcSchema(builder.build());
78
- }
79
- }
@@ -1,153 +0,0 @@
1
- package org.embulk.output.jdbc;
2
-
3
- import java.sql.SQLException;
4
- import java.sql.Statement;
5
- import java.util.regex.Matcher;
6
- import java.util.regex.Pattern;
7
- import org.slf4j.Logger;
8
- import org.embulk.spi.Exec;
9
-
10
- public class JdbcUtils
11
- {
12
- public final Logger logger = Exec.getLogger(JdbcUtils.class.getName());
13
-
14
- private static String[] SEARCH_STRING_SPECIAL_CHARS = new String[] { "_", "%" };
15
-
16
- public static String escapeSearchString(String searchString, String escapeString)
17
- {
18
- if (searchString != null && escapeString != null) {
19
- // First of all, escape escapeString '\' itself: '\' -> '\\'
20
- searchString = searchString.replaceAll(Pattern.quote(escapeString),
21
- Matcher.quoteReplacement(escapeString + escapeString));
22
- for (String specialChar : SEARCH_STRING_SPECIAL_CHARS) {
23
- if (specialChar.equals(escapeString)) {
24
- throw new IllegalArgumentException("Special char " + specialChar + " cannot be an escape char");
25
- }
26
- searchString = searchString.replaceAll(Pattern.quote(specialChar),
27
- Matcher.quoteReplacement(escapeString + specialChar));
28
- }
29
- }
30
- return searchString;
31
- }
32
-
33
- private Class<?> connectionClass;
34
-
35
- // Connection.isValid() is available from Java 1.6 + JDBC4.
36
- //private boolean hasIsValid;
37
-
38
- // Connection.setNetworkTimeout() is available from Java 1.7 + JDBC4.
39
- //private boolean hasSetNetworkTimeout;
40
- //private Method setNetworkTimeoutMethod;
41
-
42
- public JdbcUtils(Class<?> connectionClass)
43
- {
44
- this.connectionClass = connectionClass;
45
-
46
- //configureSetNetworkTimeout();
47
- }
48
-
49
- public int executeUpdateWithSqlLogging(Statement stmt, String sql) throws SQLException
50
- {
51
- logger.info("SQL: " + sql);
52
- long startTime = System.currentTimeMillis();
53
- int count = stmt.executeUpdate(sql);
54
- double seconds = (System.currentTimeMillis() - startTime) / 1000.0;
55
- if (count == 0) {
56
- logger.info(String.format("> %.2f seconds", seconds));
57
- } else {
58
- logger.info(String.format("> %.2f seconds (%,d rows)", seconds, count));
59
- }
60
- return count;
61
- }
62
-
63
- //private void configureSetNetworkTimeout() {
64
- // try {
65
- // Method m = connectionClass.getMethod("setNetworkTimeout", Executor.class, int.class);
66
- // if (isCallableMethod(m)) {
67
- // setNetworkTimeoutMethod = m;
68
- // hasSetNetworkTimeout = true;
69
- // }
70
- // } catch (SecurityException ex) {
71
- // } catch (NoSuchMethodException ex) {
72
- // }
73
- //}
74
-
75
- //private boolean isCallableMethod(Method m) {
76
- // int modifiers = m.getModifiers();
77
- // if (Modifier.isAbstract(modifiers)) {
78
- // // Method.invoke throws java.lang.AbstractMethodError if it's an
79
- // // abstract method. Applications can't catch AbstractMethodError.
80
- // return false;
81
- // }
82
- // if (!Modifier.isPublic(modifiers)) {
83
- // // we can only call public methods
84
- // return false;
85
- // }
86
- // return true;
87
- //}
88
-
89
- // PostgreSQL JDBC driver implements isValid() method. But the
90
- // implementation throws following exception:
91
- // "java.io.IOException: Method org.postgresql.jdbc4.Jdbc4Connection.isValid(int) is not yet implemented."
92
- //
93
- // So, checking mechanism doesn't work at all.
94
- // Thus here just runs "SELECT 1" to check connectivity.
95
- //
96
- //public boolean isValidConnection(Connection connection, int timeout) throws SQLException
97
- //{
98
- // Statement stmt = connection.createStatement();
99
- // try {
100
- // stmt.executeQuery("SELECT 1").close();
101
- // return true;
102
- // } catch (SQLException ex) {
103
- // return false;
104
- // } finally {
105
- // stmt.close();
106
- // }
107
- //}
108
-
109
- //public void setNetworkTimeout(Connection connection,
110
- // Executor executor, int milliseconds) throws SQLException {
111
- // Throwable exception = null;
112
- // if (hasSetNetworkTimeout) {
113
- // try {
114
- // setNetworkTimeoutMethod.invoke(connection, executor, milliseconds);
115
- // return;
116
- //
117
- // } catch (IllegalArgumentException ex) {
118
- // // ignore error
119
- // LOG.warn("Connection.setNetworkTimeout failed due to IllegalArgumentException.");
120
- // exception = ex;
121
- //
122
- // } catch (IllegalAccessException ex) {
123
- // // ignore error
124
- // LOG.warn("Connection.setNetworkTimeout failed due to IllegalAccessException.");
125
- // exception = ex;
126
- //
127
- // } catch (InvocationTargetException ex) {
128
- // //Throwable cause = ex.getTargetException();
129
- // //if (cause instanceof SQLException) {
130
- // // throw (SQLException) cause;
131
- // //} else if (cause instanceof RuntimeException) {
132
- // // throw (RuntimeException) cause;
133
- // //} else if (cause instanceof Error) {
134
- // // throw (Error) cause;
135
- // //} else {
136
- // // throw new SQLException(cause);
137
- // //}
138
- // exception = ex.getTargetException();
139
- // // It's safe to ignore exceptions.
140
- // }
141
- //
142
- // hasSetNetworkTimeout = false;
143
- // }
144
- //
145
- // if (exception != null) {
146
- // LOG.warn("Connection.setNetworkTimeout is not available: "+exception);
147
- // } else {
148
- // LOG.warn("Connection.setNetworkTimeout is not available.");
149
- // }
150
- // // TODO any substitute implementations?
151
- //}
152
- }
153
-
@@ -1,23 +0,0 @@
1
- package org.embulk.output.jdbc;
2
-
3
- import com.google.common.base.Optional;
4
-
5
- import java.util.List;
6
-
7
- public class MergeConfig {
8
- private final List<String> mergeKeys;
9
- private final Optional<List<String>> mergeRule;
10
-
11
- public MergeConfig(List<String> mergeKeys, Optional<List<String>> mergeRule) {
12
- this.mergeKeys = mergeKeys;
13
- this.mergeRule = mergeRule;
14
- }
15
-
16
- public List<String> getMergeKeys() {
17
- return mergeKeys;
18
- }
19
-
20
- public Optional<List<String>> getMergeRule() {
21
- return mergeRule;
22
- }
23
- }
@@ -1,201 +0,0 @@
1
- package org.embulk.output.jdbc;
2
-
3
- import java.util.Calendar;
4
- import java.io.IOException;
5
- import java.math.BigDecimal;
6
- import java.sql.PreparedStatement;
7
- import java.sql.SQLException;
8
- import java.sql.Date;
9
- import java.sql.Time;
10
- import com.google.common.base.Optional;
11
- import org.slf4j.Logger;
12
- import org.embulk.spi.time.Timestamp;
13
- import org.embulk.spi.Exec;
14
-
15
- public class StandardBatchInsert
16
- implements BatchInsert
17
- {
18
- private final Logger logger = Exec.getLogger(StandardBatchInsert.class);
19
-
20
- private final JdbcOutputConnector connector;
21
- private final Optional<MergeConfig> mergeConfig;
22
-
23
- private JdbcOutputConnection connection;
24
- private PreparedStatement batch;
25
- private int index;
26
- private int batchWeight;
27
- private int batchRows;
28
- private long totalRows;
29
-
30
- public StandardBatchInsert(JdbcOutputConnector connector, Optional<MergeConfig> mergeConfig) throws IOException, SQLException
31
- {
32
- this.connector = connector;
33
- this.mergeConfig = mergeConfig;
34
- }
35
-
36
- public void prepare(String loadTable, JdbcSchema insertSchema) throws SQLException
37
- {
38
- this.connection = connector.connect(true);
39
- this.index = 1; // PreparedStatement index begings from 1
40
- this.batchRows = 0;
41
- this.totalRows = 0;
42
- this.batch = prepareStatement(loadTable, insertSchema);
43
- batch.clearBatch();
44
- }
45
-
46
- protected PreparedStatement prepareStatement(String loadTable, JdbcSchema insertSchema) throws SQLException
47
- {
48
- return connection.prepareBatchInsertStatement(loadTable, insertSchema, mergeConfig);
49
- }
50
-
51
- public int getBatchWeight()
52
- {
53
- return batchWeight;
54
- }
55
-
56
- public void add() throws IOException, SQLException
57
- {
58
- batch.addBatch();
59
- index = 1; // PreparedStatement index begins from 1
60
- batchRows++;
61
- batchWeight += 32; // add weight as overhead of each rows
62
- }
63
-
64
- public void close() throws IOException, SQLException
65
- {
66
- if (connection != null) {
67
- connection.close();
68
- }
69
- }
70
-
71
- public void flush() throws IOException, SQLException
72
- {
73
- logger.info(String.format("Loading %,d rows", batchRows));
74
- long startTime = System.currentTimeMillis();
75
- batch.executeBatch(); // here can't use returned value because MySQL Connector/J returns SUCCESS_NO_INFO as a batch result
76
- double seconds = (System.currentTimeMillis() - startTime) / 1000.0;
77
-
78
- totalRows += batchRows;
79
- logger.info(String.format("> %.2f seconds (loaded %,d rows in total)", seconds, totalRows));
80
- batch.clearBatch();
81
- batchRows = 0;
82
- batchWeight = 0;
83
- }
84
-
85
- public void finish() throws IOException, SQLException
86
- {
87
- if (getBatchWeight() != 0) {
88
- flush();
89
- }
90
- }
91
-
92
- public void setNull(int sqlType) throws IOException, SQLException
93
- {
94
- batch.setNull(index, sqlType);
95
- nextColumn(0);
96
- }
97
-
98
- public void setBoolean(boolean v) throws IOException, SQLException
99
- {
100
- batch.setBoolean(index, v);
101
- nextColumn(1);
102
- }
103
-
104
- public void setByte(byte v) throws IOException, SQLException
105
- {
106
- batch.setByte(index, v);
107
- nextColumn(1);
108
- }
109
-
110
- public void setShort(short v) throws IOException, SQLException
111
- {
112
- batch.setShort(index, v);
113
- nextColumn(2);
114
- }
115
-
116
- public void setInt(int v) throws IOException, SQLException
117
- {
118
- batch.setInt(index, v);
119
- nextColumn(4);
120
- }
121
-
122
- public void setLong(long v) throws IOException, SQLException
123
- {
124
- batch.setLong(index, v);
125
- nextColumn(8);
126
- }
127
-
128
- public void setFloat(float v) throws IOException, SQLException
129
- {
130
- batch.setFloat(index, v);
131
- nextColumn(4);
132
- }
133
-
134
- public void setDouble(double v) throws IOException, SQLException
135
- {
136
- batch.setDouble(index, v);
137
- nextColumn(8);
138
- }
139
-
140
- public void setBigDecimal(BigDecimal v) throws IOException, SQLException
141
- {
142
- // use estimated number of necessary bytes + 8 byte for the weight
143
- // assuming one place needs 4 bits. ceil(v.precision() / 2.0) + 8
144
- batch.setBigDecimal(index, v);
145
- nextColumn((v.precision() & ~2) / 2 + 8);
146
- }
147
-
148
- public void setString(String v) throws IOException, SQLException
149
- {
150
- batch.setString(index, v);
151
- // estimate all chracters use 2 bytes; almost enough for the worst case
152
- nextColumn(v.length() * 2 + 4);
153
- }
154
-
155
- public void setNString(String v) throws IOException, SQLException
156
- {
157
- batch.setNString(index, v);
158
- // estimate all chracters use 2 bytes; almost enough for the worst case
159
- nextColumn(v.length() * 2 + 4);
160
- }
161
-
162
- public void setBytes(byte[] v) throws IOException, SQLException
163
- {
164
- batch.setBytes(index, v);
165
- nextColumn(v.length + 4);
166
- }
167
-
168
- public void setSqlDate(Timestamp v, Calendar cal) throws IOException, SQLException
169
- {
170
- // JavaDoc of java.sql.Time says:
171
- // >> To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.
172
- cal.setTimeInMillis(v.getEpochSecond() * 1000);
173
- cal.set(Calendar.SECOND, 0);
174
- cal.set(Calendar.MINUTE, 0);
175
- cal.set(Calendar.HOUR_OF_DAY, 0);
176
- Date normalized = new Date(cal.getTimeInMillis());
177
- batch.setDate(index, normalized, cal);
178
- nextColumn(32);
179
- }
180
-
181
- public void setSqlTime(Timestamp v, Calendar cal) throws IOException, SQLException
182
- {
183
- Time t = new Time(v.toEpochMilli());
184
- batch.setTime(index, t, cal);
185
- nextColumn(32);
186
- }
187
-
188
- public void setSqlTimestamp(Timestamp v, Calendar cal) throws IOException, SQLException
189
- {
190
- java.sql.Timestamp t = new java.sql.Timestamp(v.toEpochMilli());
191
- t.setNanos(v.getNano());
192
- batch.setTimestamp(index, t, cal);
193
- nextColumn(32);
194
- }
195
-
196
- private void nextColumn(int weight)
197
- {
198
- index++;
199
- batchWeight += weight + 4; // add weight as overhead of each columns
200
- }
201
- }