embulk-output-jdbc 0.2.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/build.gradle +2 -2
- data/classpath/embulk-output-jdbc-0.3.0.jar +0 -0
- data/lib/embulk/output/jdbc.rb +3 -3
- data/src/main/java/org/embulk/output/JdbcOutputPlugin.java +138 -120
- data/src/main/java/org/embulk/output/jdbc/AbstractJdbcOutputPlugin.java +964 -755
- data/src/main/java/org/embulk/output/jdbc/BatchInsert.java +54 -54
- data/src/main/java/org/embulk/output/jdbc/JdbcColumn.java +59 -23
- data/src/main/java/org/embulk/output/jdbc/JdbcColumnOption.java +34 -0
- data/src/main/java/org/embulk/output/jdbc/JdbcOutputConnection.java +95 -114
- data/src/main/java/org/embulk/output/jdbc/JdbcOutputConnector.java +8 -8
- data/src/main/java/org/embulk/output/jdbc/JdbcSchema.java +60 -37
- data/src/main/java/org/embulk/output/jdbc/JdbcUtils.java +155 -155
- data/src/main/java/org/embulk/output/jdbc/StandardBatchInsert.java +8 -5
- data/src/main/java/org/embulk/output/jdbc/setter/BigDecimalColumnSetter.java +70 -0
- data/src/main/java/org/embulk/output/jdbc/setter/BooleanColumnSetter.java +54 -52
- data/src/main/java/org/embulk/output/jdbc/setter/ByteColumnSetter.java +76 -0
- data/src/main/java/org/embulk/output/jdbc/setter/ColumnSetter.java +10 -91
- data/src/main/java/org/embulk/output/jdbc/setter/ColumnSetterFactory.java +189 -137
- data/src/main/java/org/embulk/output/jdbc/setter/ColumnSetterVisitor.java +96 -0
- data/src/main/java/org/embulk/output/jdbc/setter/DefaultValueSetter.java +48 -0
- data/src/main/java/org/embulk/output/jdbc/setter/DoubleColumnSetter.java +61 -51
- data/src/main/java/org/embulk/output/jdbc/setter/FloatColumnSetter.java +61 -0
- data/src/main/java/org/embulk/output/jdbc/setter/IntColumnSetter.java +76 -0
- data/src/main/java/org/embulk/output/jdbc/setter/LongColumnSetter.java +72 -62
- data/src/main/java/org/embulk/output/jdbc/setter/NStringColumnSetter.java +59 -0
- data/src/main/java/org/embulk/output/jdbc/setter/NullColumnSetter.java +53 -43
- data/src/main/java/org/embulk/output/jdbc/setter/NullDefaultValueSetter.java +105 -0
- data/src/main/java/org/embulk/output/jdbc/setter/PassThroughColumnSetter.java +56 -0
- data/src/main/java/org/embulk/output/jdbc/setter/ShortColumnSetter.java +76 -0
- data/src/main/java/org/embulk/output/jdbc/setter/SkipColumnSetter.java +43 -38
- data/src/main/java/org/embulk/output/jdbc/setter/SqlDateColumnSetter.java +63 -0
- data/src/main/java/org/embulk/output/jdbc/setter/SqlTimeColumnSetter.java +55 -0
- data/src/main/java/org/embulk/output/jdbc/setter/SqlTimestampColumnSetter.java +55 -48
- data/src/main/java/org/embulk/output/jdbc/setter/StringColumnSetter.java +59 -48
- data/src/test/java/org/embulk/output/TestJdbcOutputPlugin.java +5 -5
- metadata +16 -4
- data/classpath/embulk-output-jdbc-0.2.4.jar +0 -0
- data/src/main/java/org/embulk/output/jdbc/RetryExecutor.java +0 -105
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adf000cfbd28d4fb1c8e33f8c11f9551b34c8c70
|
4
|
+
data.tar.gz: dc37c2dd32bc03ff6b92d16a57da20614bd0dd25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5768c7090977ee2f5d01ca59808ea6eea7698d07cea80daf29d06617f13a2befc4e7354de954019590f2bd1c2813382ddb4f3ba97bfdb7713906d92822334e6
|
7
|
+
data.tar.gz: 8eea4f3829f024e86e661324edd4b948be3a73725826d94b426c3b08ab760c464496349cad2c4948cda09f5f1abb58e76d03b65bf31fe3f90f0221e5596611c9
|
data/build.gradle
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
dependencies {
|
2
|
-
}
|
1
|
+
dependencies {
|
2
|
+
}
|
Binary file
|
data/lib/embulk/output/jdbc.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
Embulk::JavaPlugin.register_output(
|
2
|
-
:jdbc, "org.embulk.output.JdbcOutputPlugin",
|
3
|
-
File.expand_path('../../../../classpath', __FILE__))
|
1
|
+
Embulk::JavaPlugin.register_output(
|
2
|
+
:jdbc, "org.embulk.output.JdbcOutputPlugin",
|
3
|
+
File.expand_path('../../../../classpath', __FILE__))
|
@@ -1,120 +1,138 @@
|
|
1
|
-
package org.embulk.output;
|
2
|
-
|
3
|
-
import java.util.
|
4
|
-
import java.
|
5
|
-
import java.
|
6
|
-
import java.sql.
|
7
|
-
import java.
|
8
|
-
import
|
9
|
-
import
|
10
|
-
import
|
11
|
-
import
|
12
|
-
import
|
13
|
-
import org.embulk.
|
14
|
-
import org.embulk.
|
15
|
-
import org.embulk.output.jdbc.
|
16
|
-
import org.embulk.output.jdbc.
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
@
|
28
|
-
public String
|
29
|
-
|
30
|
-
@Config("
|
31
|
-
public String
|
32
|
-
|
33
|
-
@Config("
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
@
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
@
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
{
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
1
|
+
package org.embulk.output;
|
2
|
+
|
3
|
+
import java.util.List;
|
4
|
+
import java.util.Set;
|
5
|
+
import java.util.Properties;
|
6
|
+
import java.sql.Driver;
|
7
|
+
import java.io.IOException;
|
8
|
+
import java.sql.Connection;
|
9
|
+
import java.sql.SQLException;
|
10
|
+
import com.google.common.base.Optional;
|
11
|
+
import com.google.common.base.Throwables;
|
12
|
+
import com.google.common.collect.ImmutableSet;
|
13
|
+
import org.embulk.config.Config;
|
14
|
+
import org.embulk.config.ConfigDefault;
|
15
|
+
import org.embulk.output.jdbc.AbstractJdbcOutputPlugin;
|
16
|
+
import org.embulk.output.jdbc.BatchInsert;
|
17
|
+
import org.embulk.output.jdbc.StandardBatchInsert;
|
18
|
+
import org.embulk.output.jdbc.JdbcOutputConnector;
|
19
|
+
import org.embulk.output.jdbc.JdbcOutputConnection;
|
20
|
+
|
21
|
+
public class JdbcOutputPlugin
|
22
|
+
extends AbstractJdbcOutputPlugin
|
23
|
+
{
|
24
|
+
public interface GenericPluginTask extends PluginTask
|
25
|
+
{
|
26
|
+
@Config("driver_path")
|
27
|
+
@ConfigDefault("null")
|
28
|
+
public Optional<String> getDriverPath();
|
29
|
+
|
30
|
+
@Config("driver_class")
|
31
|
+
public String getDriverClass();
|
32
|
+
|
33
|
+
@Config("url")
|
34
|
+
public String getUrl();
|
35
|
+
|
36
|
+
@Config("user")
|
37
|
+
@ConfigDefault("null")
|
38
|
+
public Optional<String> getUser();
|
39
|
+
|
40
|
+
@Config("password")
|
41
|
+
@ConfigDefault("null")
|
42
|
+
public Optional<String> getPassword();
|
43
|
+
|
44
|
+
@Config("schema")
|
45
|
+
@ConfigDefault("null")
|
46
|
+
public Optional<String> getSchema();
|
47
|
+
|
48
|
+
@Config("max_table_name_length")
|
49
|
+
@ConfigDefault("30")
|
50
|
+
public int getMaxTableNameLength();
|
51
|
+
}
|
52
|
+
|
53
|
+
@Override
|
54
|
+
protected Class<? extends PluginTask> getTaskClass()
|
55
|
+
{
|
56
|
+
return GenericPluginTask.class;
|
57
|
+
}
|
58
|
+
|
59
|
+
@Override
|
60
|
+
protected Features getFeatures(PluginTask task)
|
61
|
+
{
|
62
|
+
GenericPluginTask t = (GenericPluginTask) task;
|
63
|
+
return new Features()
|
64
|
+
.setMaxTableNameLength(t.getMaxTableNameLength())
|
65
|
+
.setSupportedModes(ImmutableSet.of(Mode.INSERT, Mode.INSERT_DIRECT, Mode.TRUNCATE_INSERT, Mode.REPLACE));
|
66
|
+
}
|
67
|
+
|
68
|
+
@Override
|
69
|
+
protected GenericOutputConnector getConnector(PluginTask task, boolean retryableMetadataOperation)
|
70
|
+
{
|
71
|
+
GenericPluginTask t = (GenericPluginTask) task;
|
72
|
+
|
73
|
+
if (t.getDriverPath().isPresent()) {
|
74
|
+
loadDriverJar(t.getDriverPath().get());
|
75
|
+
}
|
76
|
+
|
77
|
+
Properties props = new Properties();
|
78
|
+
|
79
|
+
props.putAll(t.getOptions());
|
80
|
+
|
81
|
+
if (t.getUser().isPresent()) {
|
82
|
+
props.setProperty("user", t.getUser().get());
|
83
|
+
}
|
84
|
+
logger.info("Connecting to {} options {}", t.getUrl(), props);
|
85
|
+
if (t.getPassword().isPresent()) {
|
86
|
+
props.setProperty("password", t.getPassword().get());
|
87
|
+
}
|
88
|
+
|
89
|
+
return new GenericOutputConnector(t.getUrl(), props, t.getDriverClass(),
|
90
|
+
t.getSchema().orNull());
|
91
|
+
}
|
92
|
+
|
93
|
+
private static class GenericOutputConnector
|
94
|
+
implements JdbcOutputConnector
|
95
|
+
{
|
96
|
+
private final Driver driver;
|
97
|
+
private final String url;
|
98
|
+
private final Properties properties;
|
99
|
+
private final String schemaName;
|
100
|
+
|
101
|
+
public GenericOutputConnector(String url, Properties properties, String driverClass,
|
102
|
+
String schemaName)
|
103
|
+
{
|
104
|
+
try {
|
105
|
+
// TODO check Class.forName(driverClass) is a Driver before newInstance
|
106
|
+
// for security
|
107
|
+
this.driver = (Driver) Class.forName(driverClass).newInstance();
|
108
|
+
} catch (Exception ex) {
|
109
|
+
throw Throwables.propagate(ex);
|
110
|
+
}
|
111
|
+
this.url = url;
|
112
|
+
this.properties = properties;
|
113
|
+
this.schemaName = schemaName;
|
114
|
+
}
|
115
|
+
|
116
|
+
@Override
|
117
|
+
public JdbcOutputConnection connect(boolean autoCommit) throws SQLException
|
118
|
+
{
|
119
|
+
Connection c = driver.connect(url, properties);
|
120
|
+
try {
|
121
|
+
c.setAutoCommit(autoCommit);
|
122
|
+
JdbcOutputConnection con = new JdbcOutputConnection(c, schemaName);
|
123
|
+
c = null;
|
124
|
+
return con;
|
125
|
+
} finally {
|
126
|
+
if (c != null) {
|
127
|
+
c.close();
|
128
|
+
}
|
129
|
+
}
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
@Override
|
134
|
+
protected BatchInsert newBatchInsert(PluginTask task, Optional<List<String>> mergeKeys) throws IOException, SQLException
|
135
|
+
{
|
136
|
+
return new StandardBatchInsert(getConnector(task, true), mergeKeys);
|
137
|
+
}
|
138
|
+
}
|
@@ -1,755 +1,964 @@
|
|
1
|
-
package org.embulk.output.jdbc;
|
2
|
-
|
3
|
-
import java.util.HashSet;
|
4
|
-
import java.util.List;
|
5
|
-
import java.util.
|
6
|
-
import java.util.
|
7
|
-
import java.util.
|
8
|
-
import java.util.
|
9
|
-
import java.
|
10
|
-
import java.
|
11
|
-
import java.
|
12
|
-
import java.
|
13
|
-
import java.sql.
|
14
|
-
import java.sql.
|
15
|
-
|
16
|
-
import
|
17
|
-
|
18
|
-
import
|
19
|
-
import com.
|
20
|
-
import com.
|
21
|
-
|
22
|
-
import
|
23
|
-
import
|
24
|
-
import
|
25
|
-
import
|
26
|
-
import
|
27
|
-
import
|
28
|
-
import
|
29
|
-
import org.embulk.config.
|
30
|
-
import org.embulk.
|
31
|
-
import org.embulk.
|
32
|
-
import org.embulk.
|
33
|
-
import org.embulk.
|
34
|
-
import org.embulk.
|
35
|
-
import org.embulk.
|
36
|
-
import org.embulk.
|
37
|
-
import org.embulk.spi.
|
38
|
-
import org.embulk.spi.
|
39
|
-
import org.embulk.spi.
|
40
|
-
import org.embulk.spi.
|
41
|
-
import org.embulk.
|
42
|
-
import org.embulk.
|
43
|
-
import org.embulk.
|
44
|
-
|
45
|
-
import
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
@Config("
|
68
|
-
@ConfigDefault("
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
public
|
73
|
-
|
74
|
-
|
75
|
-
public
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
public
|
123
|
-
{
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
public boolean
|
143
|
-
{
|
144
|
-
return
|
145
|
-
}
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
}
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
}
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
}
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
if (task.
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
}
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
}
|
482
|
-
}
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
}
|
516
|
-
}
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
}
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
{
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
1
|
+
package org.embulk.output.jdbc;
|
2
|
+
|
3
|
+
import java.util.HashSet;
|
4
|
+
import java.util.List;
|
5
|
+
import java.util.Map;
|
6
|
+
import java.util.Locale;
|
7
|
+
import java.util.Properties;
|
8
|
+
import java.util.Set;
|
9
|
+
import java.util.concurrent.ExecutionException;
|
10
|
+
import java.io.IOException;
|
11
|
+
import java.nio.charset.Charset;
|
12
|
+
import java.nio.file.Paths;
|
13
|
+
import java.sql.Types;
|
14
|
+
import java.sql.ResultSet;
|
15
|
+
import java.sql.DatabaseMetaData;
|
16
|
+
import java.sql.SQLException;
|
17
|
+
import org.slf4j.Logger;
|
18
|
+
import org.joda.time.DateTimeZone;
|
19
|
+
import com.fasterxml.jackson.annotation.JsonCreator;
|
20
|
+
import com.fasterxml.jackson.annotation.JsonValue;
|
21
|
+
import com.fasterxml.jackson.annotation.JsonProperty;
|
22
|
+
import com.google.common.base.Optional;
|
23
|
+
import com.google.common.base.Function;
|
24
|
+
import com.google.common.base.Supplier;
|
25
|
+
import com.google.common.base.Throwables;
|
26
|
+
import com.google.common.collect.Lists;
|
27
|
+
import com.google.common.collect.ImmutableList;
|
28
|
+
import com.google.common.collect.ImmutableSet;
|
29
|
+
import org.embulk.config.CommitReport;
|
30
|
+
import org.embulk.config.Config;
|
31
|
+
import org.embulk.config.ConfigDefault;
|
32
|
+
import org.embulk.config.ConfigDiff;
|
33
|
+
import org.embulk.config.ConfigException;
|
34
|
+
import org.embulk.config.ConfigSource;
|
35
|
+
import org.embulk.config.Task;
|
36
|
+
import org.embulk.config.TaskSource;
|
37
|
+
import org.embulk.spi.Exec;
|
38
|
+
import org.embulk.spi.Column;
|
39
|
+
import org.embulk.spi.ColumnVisitor;
|
40
|
+
import org.embulk.spi.OutputPlugin;
|
41
|
+
import org.embulk.spi.PluginClassLoader;
|
42
|
+
import org.embulk.spi.Schema;
|
43
|
+
import org.embulk.spi.TransactionalPageOutput;
|
44
|
+
import org.embulk.spi.Page;
|
45
|
+
import org.embulk.spi.PageReader;
|
46
|
+
import org.embulk.spi.type.StringType;
|
47
|
+
import org.embulk.spi.time.Timestamp;
|
48
|
+
import org.embulk.spi.time.TimestampFormat;
|
49
|
+
import org.embulk.spi.time.TimestampFormatter;
|
50
|
+
import org.embulk.output.jdbc.setter.ColumnSetter;
|
51
|
+
import org.embulk.output.jdbc.setter.ColumnSetterFactory;
|
52
|
+
import org.embulk.output.jdbc.setter.ColumnSetterVisitor;
|
53
|
+
import org.embulk.spi.util.RetryExecutor.Retryable;
|
54
|
+
import static org.embulk.spi.util.RetryExecutor.retryExecutor;
|
55
|
+
import static org.embulk.output.jdbc.JdbcSchema.filterSkipColumns;
|
56
|
+
|
57
|
+
public abstract class AbstractJdbcOutputPlugin
|
58
|
+
implements OutputPlugin
|
59
|
+
{
|
60
|
+
private final static Set<String> loadedJarGlobs = new HashSet<String>();
|
61
|
+
|
62
|
+
protected final Logger logger = Exec.getLogger(getClass());
|
63
|
+
|
64
|
+
public interface PluginTask
|
65
|
+
extends Task
|
66
|
+
{
|
67
|
+
@Config("options")
|
68
|
+
@ConfigDefault("{}")
|
69
|
+
public Properties getOptions();
|
70
|
+
|
71
|
+
@Config("table")
|
72
|
+
public String getTable();
|
73
|
+
|
74
|
+
@Config("mode")
|
75
|
+
public Mode getMode();
|
76
|
+
|
77
|
+
@Config("batch_size")
|
78
|
+
@ConfigDefault("16777216")
|
79
|
+
// TODO set minimum number
|
80
|
+
public int getBatchSize();
|
81
|
+
|
82
|
+
@Config("merge_keys")
|
83
|
+
@ConfigDefault("null")
|
84
|
+
public Optional<List<String>> getMergeKeys();
|
85
|
+
|
86
|
+
@Config("column_options")
|
87
|
+
@ConfigDefault("{}")
|
88
|
+
public Map<String, JdbcColumnOption> getColumnOptions();
|
89
|
+
|
90
|
+
@Config("default_timezone")
|
91
|
+
@ConfigDefault("\"UTC\"")
|
92
|
+
public DateTimeZone getDefaultTimeZone();
|
93
|
+
|
94
|
+
public void setMergeKeys(Optional<List<String>> keys);
|
95
|
+
|
96
|
+
public void setFeatures(Features features);
|
97
|
+
public Features getFeatures();
|
98
|
+
|
99
|
+
public JdbcSchema getTargetTableSchema();
|
100
|
+
public void setTargetTableSchema(JdbcSchema schema);
|
101
|
+
|
102
|
+
public Optional<List<String>> getIntermediateTables();
|
103
|
+
public void setIntermediateTables(Optional<List<String>> names);
|
104
|
+
}
|
105
|
+
|
106
|
+
public static class Features
|
107
|
+
{
|
108
|
+
private int maxTableNameLength = 64;
|
109
|
+
private Set<Mode> supportedModes = ImmutableSet.copyOf(Mode.values());
|
110
|
+
private boolean ignoreMergeKeys = false;
|
111
|
+
|
112
|
+
public Features()
|
113
|
+
{ }
|
114
|
+
|
115
|
+
@JsonProperty
|
116
|
+
public int getMaxTableNameLength()
|
117
|
+
{
|
118
|
+
return maxTableNameLength;
|
119
|
+
}
|
120
|
+
|
121
|
+
@JsonProperty
|
122
|
+
public Features setMaxTableNameLength(int bytes)
|
123
|
+
{
|
124
|
+
this.maxTableNameLength = bytes;
|
125
|
+
return this;
|
126
|
+
}
|
127
|
+
|
128
|
+
@JsonProperty
|
129
|
+
public Set<Mode> getSupportedModes()
|
130
|
+
{
|
131
|
+
return supportedModes;
|
132
|
+
}
|
133
|
+
|
134
|
+
@JsonProperty
|
135
|
+
public Features setSupportedModes(Set<Mode> modes)
|
136
|
+
{
|
137
|
+
this.supportedModes = modes;
|
138
|
+
return this;
|
139
|
+
}
|
140
|
+
|
141
|
+
@JsonProperty
|
142
|
+
public boolean getIgnoreMergeKeys()
|
143
|
+
{
|
144
|
+
return ignoreMergeKeys;
|
145
|
+
}
|
146
|
+
|
147
|
+
@JsonProperty
|
148
|
+
public Features setIgnoreMergeKeys(boolean value)
|
149
|
+
{
|
150
|
+
this.ignoreMergeKeys = value;
|
151
|
+
return this;
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
protected void loadDriverJar(String glob)
|
156
|
+
{
|
157
|
+
synchronized (loadedJarGlobs) {
|
158
|
+
if (!loadedJarGlobs.contains(glob)) {
|
159
|
+
// TODO match glob
|
160
|
+
PluginClassLoader loader = (PluginClassLoader) getClass().getClassLoader();
|
161
|
+
loader.addPath(Paths.get(glob));
|
162
|
+
loadedJarGlobs.add(glob);
|
163
|
+
}
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
// for subclasses to add @Config
|
168
|
+
protected Class<? extends PluginTask> getTaskClass()
|
169
|
+
{
|
170
|
+
return PluginTask.class;
|
171
|
+
}
|
172
|
+
|
173
|
+
protected abstract Features getFeatures(PluginTask task);
|
174
|
+
|
175
|
+
protected abstract JdbcOutputConnector getConnector(PluginTask task, boolean retryableMetadataOperation);
|
176
|
+
|
177
|
+
protected abstract BatchInsert newBatchInsert(PluginTask task, Optional<List<String>> mergeKeys) throws IOException, SQLException;
|
178
|
+
|
179
|
+
protected JdbcOutputConnection newConnection(PluginTask task, boolean retryableMetadataOperation,
|
180
|
+
boolean autoCommit) throws SQLException
|
181
|
+
{
|
182
|
+
return getConnector(task, retryableMetadataOperation).connect(autoCommit);
|
183
|
+
}
|
184
|
+
|
185
|
+
public enum Mode {
|
186
|
+
INSERT,
|
187
|
+
INSERT_DIRECT,
|
188
|
+
MERGE,
|
189
|
+
MERGE_DIRECT,
|
190
|
+
TRUNCATE_INSERT,
|
191
|
+
REPLACE;
|
192
|
+
|
193
|
+
@JsonValue
|
194
|
+
@Override
|
195
|
+
public String toString()
|
196
|
+
{
|
197
|
+
return name().toLowerCase(Locale.ENGLISH);
|
198
|
+
}
|
199
|
+
|
200
|
+
@JsonCreator
|
201
|
+
public static Mode fromString(String value)
|
202
|
+
{
|
203
|
+
switch(value) {
|
204
|
+
case "insert":
|
205
|
+
return INSERT;
|
206
|
+
case "insert_direct":
|
207
|
+
return INSERT_DIRECT;
|
208
|
+
case "merge":
|
209
|
+
return MERGE;
|
210
|
+
case "merge_direct":
|
211
|
+
return MERGE_DIRECT;
|
212
|
+
case "truncate_insert":
|
213
|
+
return TRUNCATE_INSERT;
|
214
|
+
case "replace":
|
215
|
+
return REPLACE;
|
216
|
+
default:
|
217
|
+
throw new ConfigException(String.format("Unknown mode '%s'. Supported modes are insert, insert_direct, merge, merge_direct, truncate_insert, replace", value));
|
218
|
+
}
|
219
|
+
}
|
220
|
+
|
221
|
+
/**
|
222
|
+
* True if this mode directly modifies the target table without creating intermediate tables.
|
223
|
+
*/
|
224
|
+
public boolean isDirectModify()
|
225
|
+
{
|
226
|
+
return this == INSERT_DIRECT || this == MERGE_DIRECT;
|
227
|
+
}
|
228
|
+
|
229
|
+
/**
|
230
|
+
* True if this mode merges records on unique keys
|
231
|
+
*/
|
232
|
+
public boolean isMerge()
|
233
|
+
{
|
234
|
+
return this == MERGE || this == MERGE_DIRECT;
|
235
|
+
}
|
236
|
+
|
237
|
+
/**
|
238
|
+
* True if this mode creates intermediate table for each tasks.
|
239
|
+
*/
|
240
|
+
public boolean tempTablePerTask()
|
241
|
+
{
|
242
|
+
return this == INSERT || this == MERGE || this == TRUNCATE_INSERT /*this == REPLACE_VIEW*/;
|
243
|
+
}
|
244
|
+
|
245
|
+
/**
|
246
|
+
* True if this mode truncates the target table before committing intermediate tables
|
247
|
+
*/
|
248
|
+
public boolean truncateBeforeCommit()
|
249
|
+
{
|
250
|
+
return this == TRUNCATE_INSERT;
|
251
|
+
}
|
252
|
+
|
253
|
+
/**
|
254
|
+
* True if this mode uses MERGE statement to commit intermediate tables to the target table
|
255
|
+
*/
|
256
|
+
public boolean commitByMerge()
|
257
|
+
{
|
258
|
+
return this == MERGE;
|
259
|
+
}
|
260
|
+
|
261
|
+
/**
|
262
|
+
* True if this mode overwrites schema of the target tables
|
263
|
+
*/
|
264
|
+
public boolean ignoreTargetTableSchema()
|
265
|
+
{
|
266
|
+
return this == REPLACE /*|| this == REPLACE_VIEW*/;
|
267
|
+
}
|
268
|
+
|
269
|
+
/**
|
270
|
+
* True if this mode swaps the target tables with intermediate tables to commit
|
271
|
+
*/
|
272
|
+
public boolean commitBySwapTable()
|
273
|
+
{
|
274
|
+
return this == REPLACE;
|
275
|
+
}
|
276
|
+
}
|
277
|
+
|
278
|
+
public ConfigDiff transaction(ConfigSource config,
|
279
|
+
Schema schema, int taskCount,
|
280
|
+
OutputPlugin.Control control)
|
281
|
+
{
|
282
|
+
PluginTask task = config.loadConfig(getTaskClass());
|
283
|
+
Features features = getFeatures(task);
|
284
|
+
task.setFeatures(features);
|
285
|
+
|
286
|
+
if (!features.getSupportedModes().contains(task.getMode())) {
|
287
|
+
throw new ConfigException(String.format("This output type doesn't support '%s'. Supported modes are: %s", task.getMode(), features.getSupportedModes()));
|
288
|
+
}
|
289
|
+
|
290
|
+
task = begin(task, schema, taskCount);
|
291
|
+
control.run(task.dump());
|
292
|
+
return commit(task, schema, taskCount);
|
293
|
+
}
|
294
|
+
|
295
|
+
public ConfigDiff resume(TaskSource taskSource,
|
296
|
+
Schema schema, int taskCount,
|
297
|
+
OutputPlugin.Control control)
|
298
|
+
{
|
299
|
+
PluginTask task = taskSource.loadTask(getTaskClass());
|
300
|
+
|
301
|
+
if (!task.getMode().tempTablePerTask()) {
|
302
|
+
throw new UnsupportedOperationException("inplace mode is not resumable. You need to delete partially-loaded records from the database and restart the entire transaction.");
|
303
|
+
}
|
304
|
+
|
305
|
+
task = begin(task, schema, taskCount);
|
306
|
+
control.run(task.dump());
|
307
|
+
return commit(task, schema, taskCount);
|
308
|
+
}
|
309
|
+
|
310
|
+
protected String getTransactionUniqueName()
|
311
|
+
{
|
312
|
+
// TODO use uuid?
|
313
|
+
Timestamp t = Exec.session().getTransactionTime();
|
314
|
+
return String.format("%016x%08x", t.getEpochSecond(), t.getNano());
|
315
|
+
}
|
316
|
+
|
317
|
+
private PluginTask begin(final PluginTask task,
|
318
|
+
final Schema schema, final int taskCount)
|
319
|
+
{
|
320
|
+
try {
|
321
|
+
withRetry(new IdempotentSqlRunnable() { // no intermediate data if isDirectModify == true
|
322
|
+
public void run() throws SQLException
|
323
|
+
{
|
324
|
+
JdbcOutputConnection con = newConnection(task, true, false);
|
325
|
+
try {
|
326
|
+
doBegin(con, task, schema, taskCount);
|
327
|
+
} finally {
|
328
|
+
con.close();
|
329
|
+
}
|
330
|
+
}
|
331
|
+
});
|
332
|
+
} catch (SQLException | InterruptedException ex) {
|
333
|
+
throw new RuntimeException(ex);
|
334
|
+
}
|
335
|
+
return task;
|
336
|
+
}
|
337
|
+
|
338
|
+
private ConfigDiff commit(final PluginTask task,
|
339
|
+
Schema schema, final int taskCount)
|
340
|
+
{
|
341
|
+
if (!task.getMode().isDirectModify()) { // no intermediate data if isDirectModify == true
|
342
|
+
try {
|
343
|
+
withRetry(new IdempotentSqlRunnable() {
|
344
|
+
public void run() throws SQLException
|
345
|
+
{
|
346
|
+
JdbcOutputConnection con = newConnection(task, false, false);
|
347
|
+
try {
|
348
|
+
doCommit(con, task, taskCount);
|
349
|
+
} finally {
|
350
|
+
con.close();
|
351
|
+
}
|
352
|
+
}
|
353
|
+
});
|
354
|
+
} catch (SQLException | InterruptedException ex) {
|
355
|
+
throw new RuntimeException(ex);
|
356
|
+
}
|
357
|
+
}
|
358
|
+
return Exec.newConfigDiff();
|
359
|
+
}
|
360
|
+
|
361
|
+
public void cleanup(TaskSource taskSource,
|
362
|
+
Schema schema, final int taskCount,
|
363
|
+
final List<CommitReport> successCommitReports)
|
364
|
+
{
|
365
|
+
final PluginTask task = taskSource.loadTask(getTaskClass());
|
366
|
+
|
367
|
+
if (!task.getMode().isDirectModify()) { // no intermediate data if isDirectModify == true
|
368
|
+
try {
|
369
|
+
withRetry(new IdempotentSqlRunnable() {
|
370
|
+
public void run() throws SQLException
|
371
|
+
{
|
372
|
+
JdbcOutputConnection con = newConnection(task, true, true);
|
373
|
+
try {
|
374
|
+
doCleanup(con, task, taskCount, successCommitReports);
|
375
|
+
} finally {
|
376
|
+
con.close();
|
377
|
+
}
|
378
|
+
}
|
379
|
+
});
|
380
|
+
} catch (SQLException | InterruptedException ex) {
|
381
|
+
throw new RuntimeException(ex);
|
382
|
+
}
|
383
|
+
}
|
384
|
+
}
|
385
|
+
|
386
|
+
protected void doBegin(JdbcOutputConnection con,
|
387
|
+
PluginTask task, final Schema schema, int taskCount) throws SQLException
|
388
|
+
{
|
389
|
+
if (schema.getColumnCount() == 0) {
|
390
|
+
throw new ConfigException("task count == 0 is not supported");
|
391
|
+
}
|
392
|
+
|
393
|
+
Mode mode = task.getMode();
|
394
|
+
logger.info("Using {} mode", mode);
|
395
|
+
|
396
|
+
Optional<JdbcSchema> initialTargetTableSchema = newJdbcSchemaFromTableIfExists(con, task.getTable());
|
397
|
+
|
398
|
+
// TODO get CREATE TABLE statement from task if set
|
399
|
+
JdbcSchema newTableSchema = applyColumnOptionsToNewTableSchema(
|
400
|
+
initialTargetTableSchema.or(new Supplier<JdbcSchema>() {
|
401
|
+
public JdbcSchema get()
|
402
|
+
{
|
403
|
+
return newJdbcSchemaForNewTable(schema);
|
404
|
+
}
|
405
|
+
}),
|
406
|
+
task.getColumnOptions());
|
407
|
+
|
408
|
+
// create intermediate tables
|
409
|
+
if (!mode.isDirectModify()) {
|
410
|
+
// direct modify mode doesn't need intermediate tables.
|
411
|
+
ImmutableList.Builder<String> intermTableNames = ImmutableList.builder();
|
412
|
+
if (mode.tempTablePerTask()) {
|
413
|
+
String namePrefix = generateIntermediateTableNamePrefix(task.getTable(), con, 3, task.getFeatures().getMaxTableNameLength());
|
414
|
+
for (int i=0; i < taskCount; i++) {
|
415
|
+
intermTableNames.add(namePrefix + String.format("%03d", i));
|
416
|
+
}
|
417
|
+
} else {
|
418
|
+
String name = generateIntermediateTableNamePrefix(task.getTable(), con, 0, task.getFeatures().getMaxTableNameLength());
|
419
|
+
intermTableNames.add(name);
|
420
|
+
}
|
421
|
+
// create the intermediate tables here
|
422
|
+
task.setIntermediateTables(Optional.<List<String>>of(intermTableNames.build()));
|
423
|
+
for (String name : task.getIntermediateTables().get()) {
|
424
|
+
// DROP TABLE IF EXISTS xyz__0000000054d92dee1e452158_bulk_load_temp
|
425
|
+
con.dropTableIfExists(name);
|
426
|
+
// CREATE TABLE IF NOT EXISTS xyz__0000000054d92dee1e452158_bulk_load_temp
|
427
|
+
con.createTableIfNotExists(name, newTableSchema);
|
428
|
+
}
|
429
|
+
} else {
|
430
|
+
task.setIntermediateTables(Optional.<List<String>>absent());
|
431
|
+
}
|
432
|
+
|
433
|
+
// build JdbcSchema from a table
|
434
|
+
JdbcSchema targetTableSchema;
|
435
|
+
if (mode.ignoreTargetTableSchema() && taskCount != 0) {
|
436
|
+
String firstItermTable = task.getIntermediateTables().get().get(0);
|
437
|
+
targetTableSchema = newJdbcSchemaFromTableIfExists(con, firstItermTable).get();
|
438
|
+
} else if (initialTargetTableSchema.isPresent()) {
|
439
|
+
targetTableSchema = initialTargetTableSchema.get();
|
440
|
+
} else {
|
441
|
+
// also create the target table if not exists
|
442
|
+
// CREATE TABLE IF NOT EXISTS xyz
|
443
|
+
con.createTableIfNotExists(task.getTable(), newTableSchema);
|
444
|
+
targetTableSchema = newJdbcSchemaFromTableIfExists(con, task.getTable()).get();
|
445
|
+
}
|
446
|
+
task.setTargetTableSchema(matchSchemaByColumnNames(schema, targetTableSchema));
|
447
|
+
|
448
|
+
// validate column_options
|
449
|
+
newColumnSetters(
|
450
|
+
new ColumnSetterFactory(null, task.getDefaultTimeZone()), // TODO create a dummy BatchInsert
|
451
|
+
task.getTargetTableSchema(), schema,
|
452
|
+
task.getColumnOptions());
|
453
|
+
|
454
|
+
// normalize merge_key parameter for merge modes
|
455
|
+
if (mode.isMerge()) {
|
456
|
+
Optional<List<String>> mergeKeys = task.getMergeKeys();
|
457
|
+
if (task.getFeatures().getIgnoreMergeKeys()) {
|
458
|
+
if (mergeKeys.isPresent()) {
|
459
|
+
throw new ConfigException("This output type does not accept 'merge_key' option.");
|
460
|
+
}
|
461
|
+
task.setMergeKeys(Optional.<List<String>>of(ImmutableList.<String>of()));
|
462
|
+
} else if (mergeKeys.isPresent()) {
|
463
|
+
if (task.getMergeKeys().get().isEmpty()) {
|
464
|
+
throw new ConfigException("Empty 'merge_keys' option is invalid.");
|
465
|
+
}
|
466
|
+
for (String key : mergeKeys.get()) {
|
467
|
+
if (!targetTableSchema.findColumn(key).isPresent()) {
|
468
|
+
throw new ConfigException(String.format("Merge key '%s' does not exist in the target table.", key));
|
469
|
+
}
|
470
|
+
}
|
471
|
+
} else {
|
472
|
+
ImmutableList.Builder<String> builder = ImmutableList.builder();
|
473
|
+
for (JdbcColumn column : targetTableSchema.getColumns()) {
|
474
|
+
if (column.isUniqueKey()) {
|
475
|
+
builder.add(column.getName());
|
476
|
+
}
|
477
|
+
}
|
478
|
+
task.setMergeKeys(Optional.<List<String>>of(builder.build()));
|
479
|
+
if (task.getMergeKeys().get().isEmpty()) {
|
480
|
+
throw new ConfigException("Merging mode is used but the target table does not have primary keys. Please set merge_keys option.");
|
481
|
+
}
|
482
|
+
}
|
483
|
+
logger.info("Using merge keys: {}", task.getMergeKeys().get());
|
484
|
+
} else {
|
485
|
+
task.setMergeKeys(Optional.<List<String>>absent());
|
486
|
+
}
|
487
|
+
}
|
488
|
+
|
489
|
+
protected String generateIntermediateTableNamePrefix(String baseTableName, JdbcOutputConnection con, int suffixLength, int maxLength) throws SQLException
|
490
|
+
{
|
491
|
+
Charset tableNameCharset = con.getTableNameCharset();
|
492
|
+
String tableName = baseTableName;
|
493
|
+
String suffix = "_bl_tmp";
|
494
|
+
String uniqueSuffix = getTransactionUniqueName() + suffix;
|
495
|
+
|
496
|
+
// way to count length of table name varies by DBMSs (bytes or characters),
|
497
|
+
// so truncate swap table name by one character.
|
498
|
+
while (!checkTableNameLength(tableName + "_" + uniqueSuffix, tableNameCharset, suffixLength, maxLength)) {
|
499
|
+
if (uniqueSuffix.length() > 8 + suffix.length()) {
|
500
|
+
// truncate transaction unique name
|
501
|
+
// (include 8 characters of the transaction name at least)
|
502
|
+
uniqueSuffix = uniqueSuffix.substring(1);
|
503
|
+
} else {
|
504
|
+
if (tableName.isEmpty()) {
|
505
|
+
throw new ConfigException("Table name is too long to generate temporary table name");
|
506
|
+
}
|
507
|
+
// truncate table name
|
508
|
+
tableName = tableName.substring(0, tableName.length() - 1);
|
509
|
+
//if (!connection.tableExists(tableName)) {
|
510
|
+
// TODO this doesn't help. Rather than truncating more characters,
|
511
|
+
// here needs to replace characters with random characters. But
|
512
|
+
// to make the result deterministic. So, an idea is replacing
|
513
|
+
// the last character to the first (second, third, ... for each loop)
|
514
|
+
// of md5(original table name).
|
515
|
+
//}
|
516
|
+
}
|
517
|
+
|
518
|
+
}
|
519
|
+
return tableName + "_" + uniqueSuffix;
|
520
|
+
}
|
521
|
+
|
522
|
+
private static JdbcSchema applyColumnOptionsToNewTableSchema(JdbcSchema schema, final Map<String, JdbcColumnOption> columnOptions)
|
523
|
+
{
|
524
|
+
return new JdbcSchema(Lists.transform(schema.getColumns(), new Function<JdbcColumn, JdbcColumn>() {
|
525
|
+
public JdbcColumn apply(JdbcColumn c)
|
526
|
+
{
|
527
|
+
JdbcColumnOption option = columnOptionOf(columnOptions, c);
|
528
|
+
if (option.getType().isPresent()) {
|
529
|
+
return JdbcColumn.newTypeDeclaredColumn(
|
530
|
+
c.getName(), Types.OTHER, // sqlType, isNotNull, and isUniqueKey are ignored
|
531
|
+
option.getType().get(), false, false);
|
532
|
+
}
|
533
|
+
return c;
|
534
|
+
}
|
535
|
+
}));
|
536
|
+
}
|
537
|
+
|
538
|
+
protected static List<ColumnSetter> newColumnSetters(ColumnSetterFactory factory,
|
539
|
+
JdbcSchema targetTableSchema, Schema inputValueSchema,
|
540
|
+
Map<String, JdbcColumnOption> columnOptions)
|
541
|
+
{
|
542
|
+
ImmutableList.Builder<ColumnSetter> builder = ImmutableList.builder();
|
543
|
+
int schemaColumnIndex = 0;
|
544
|
+
for (JdbcColumn targetColumn : targetTableSchema.getColumns()) {
|
545
|
+
if (targetColumn.isSkipColumn()) {
|
546
|
+
builder.add(factory.newSkipColumnSetter());
|
547
|
+
} else {
|
548
|
+
//String columnOptionKey = inputValueSchema.getColumn(schemaColumnIndex).getName();
|
549
|
+
JdbcColumnOption option = columnOptionOf(columnOptions, targetColumn);
|
550
|
+
builder.add(factory.newColumnSetter(targetColumn, option));
|
551
|
+
schemaColumnIndex++;
|
552
|
+
}
|
553
|
+
}
|
554
|
+
return builder.build();
|
555
|
+
}
|
556
|
+
|
557
|
+
private static JdbcColumnOption columnOptionOf(Map<String, JdbcColumnOption> columnOptions, JdbcColumn targetColumn)
|
558
|
+
{
|
559
|
+
return Optional.fromNullable(columnOptions.get(targetColumn.getName())).or(
|
560
|
+
// default column option
|
561
|
+
new Supplier<JdbcColumnOption>()
|
562
|
+
{
|
563
|
+
public JdbcColumnOption get()
|
564
|
+
{
|
565
|
+
return Exec.newConfigSource().loadConfig(JdbcColumnOption.class);
|
566
|
+
}
|
567
|
+
});
|
568
|
+
}
|
569
|
+
|
570
|
+
private boolean checkTableNameLength(String tableName, Charset tableNameCharset, int suffixLength, int maxLength)
|
571
|
+
{
|
572
|
+
return tableNameCharset.encode(tableName).remaining() + suffixLength <= maxLength;
|
573
|
+
}
|
574
|
+
|
575
|
+
protected void doCommit(JdbcOutputConnection con, PluginTask task, int taskCount)
|
576
|
+
throws SQLException
|
577
|
+
{
|
578
|
+
JdbcSchema schema = filterSkipColumns(task.getTargetTableSchema());
|
579
|
+
|
580
|
+
switch (task.getMode()) {
|
581
|
+
case INSERT_DIRECT:
|
582
|
+
case MERGE_DIRECT:
|
583
|
+
// already done
|
584
|
+
break;
|
585
|
+
|
586
|
+
case INSERT:
|
587
|
+
// aggregate insert into target
|
588
|
+
con.collectInsert(task.getIntermediateTables().get(), schema, task.getTable(), false);
|
589
|
+
break;
|
590
|
+
|
591
|
+
case TRUNCATE_INSERT:
|
592
|
+
// truncate & aggregate insert into target
|
593
|
+
con.collectInsert(task.getIntermediateTables().get(), schema, task.getTable(), true);
|
594
|
+
break;
|
595
|
+
|
596
|
+
case MERGE:
|
597
|
+
// aggregate merge into target
|
598
|
+
con.collectMerge(task.getIntermediateTables().get(), schema, task.getTable(), task.getMergeKeys().get());
|
599
|
+
break;
|
600
|
+
|
601
|
+
case REPLACE:
|
602
|
+
// swap table
|
603
|
+
con.replaceTable(task.getIntermediateTables().get().get(0), schema, task.getTable());
|
604
|
+
break;
|
605
|
+
}
|
606
|
+
}
|
607
|
+
|
608
|
+
protected void doCleanup(JdbcOutputConnection con, PluginTask task, int taskCount,
|
609
|
+
List<CommitReport> successCommitReports)
|
610
|
+
throws SQLException
|
611
|
+
{
|
612
|
+
if (task.getIntermediateTables().isPresent()) {
|
613
|
+
for (String intermTable : task.getIntermediateTables().get()) {
|
614
|
+
con.dropTableIfExists(intermTable);
|
615
|
+
}
|
616
|
+
}
|
617
|
+
}
|
618
|
+
|
619
|
+
protected JdbcSchema newJdbcSchemaForNewTable(Schema schema)
|
620
|
+
{
|
621
|
+
final ImmutableList.Builder<JdbcColumn> columns = ImmutableList.builder();
|
622
|
+
for (Column c : schema.getColumns()) {
|
623
|
+
final String columnName = c.getName();
|
624
|
+
c.visit(new ColumnVisitor() {
|
625
|
+
public void booleanColumn(Column column)
|
626
|
+
{
|
627
|
+
columns.add(JdbcColumn.newGenericTypeColumn(
|
628
|
+
columnName, Types.BOOLEAN, "BOOLEAN",
|
629
|
+
1, 0, false, false));
|
630
|
+
}
|
631
|
+
|
632
|
+
public void longColumn(Column column)
|
633
|
+
{
|
634
|
+
columns.add(JdbcColumn.newGenericTypeColumn(
|
635
|
+
columnName, Types.BIGINT, "BIGINT",
|
636
|
+
22, 0, false, false));
|
637
|
+
}
|
638
|
+
|
639
|
+
public void doubleColumn(Column column)
|
640
|
+
{
|
641
|
+
columns.add(JdbcColumn.newGenericTypeColumn(
|
642
|
+
columnName, Types.FLOAT, "DOUBLE PRECISION",
|
643
|
+
24, 0, false, false));
|
644
|
+
}
|
645
|
+
|
646
|
+
public void stringColumn(Column column)
|
647
|
+
{
|
648
|
+
columns.add(JdbcColumn.newGenericTypeColumn(
|
649
|
+
columnName, Types.CLOB, "CLOB",
|
650
|
+
4000, 0, false, false)); // TODO size type param
|
651
|
+
}
|
652
|
+
|
653
|
+
public void timestampColumn(Column column)
|
654
|
+
{
|
655
|
+
columns.add(JdbcColumn.newGenericTypeColumn(
|
656
|
+
columnName, Types.TIMESTAMP, "TIMESTAMP",
|
657
|
+
26, 0, false, false)); // size type param is from postgresql
|
658
|
+
}
|
659
|
+
});
|
660
|
+
}
|
661
|
+
return new JdbcSchema(columns.build());
|
662
|
+
}
|
663
|
+
|
664
|
+
public Optional<JdbcSchema> newJdbcSchemaFromTableIfExists(JdbcOutputConnection connection,
|
665
|
+
String tableName) throws SQLException
|
666
|
+
{
|
667
|
+
DatabaseMetaData dbm = connection.getMetaData();
|
668
|
+
String escape = dbm.getSearchStringEscape();
|
669
|
+
|
670
|
+
ResultSet rs = dbm.getPrimaryKeys(null, connection.getSchemaName(), tableName);
|
671
|
+
ImmutableSet.Builder<String> primaryKeysBuilder = ImmutableSet.builder();
|
672
|
+
try {
|
673
|
+
while(rs.next()) {
|
674
|
+
primaryKeysBuilder.add(rs.getString("COLUMN_NAME"));
|
675
|
+
}
|
676
|
+
} finally {
|
677
|
+
rs.close();
|
678
|
+
}
|
679
|
+
ImmutableSet<String> primaryKeys = primaryKeysBuilder.build();
|
680
|
+
|
681
|
+
ImmutableList.Builder<JdbcColumn> builder = ImmutableList.builder();
|
682
|
+
rs = dbm.getColumns(null,
|
683
|
+
JdbcUtils.escapeSearchString(connection.getSchemaName(), escape),
|
684
|
+
JdbcUtils.escapeSearchString(tableName, escape),
|
685
|
+
null);
|
686
|
+
try {
|
687
|
+
while (rs.next()) {
|
688
|
+
String columnName = rs.getString("COLUMN_NAME");
|
689
|
+
String simpleTypeName = rs.getString("TYPE_NAME").toUpperCase(Locale.ENGLISH);
|
690
|
+
boolean isUniqueKey = primaryKeys.contains(columnName);
|
691
|
+
int sqlType = rs.getInt("DATA_TYPE");
|
692
|
+
int colSize = rs.getInt("COLUMN_SIZE");
|
693
|
+
int decDigit = rs.getInt("DECIMAL_DIGITS");
|
694
|
+
if (rs.wasNull()) {
|
695
|
+
decDigit = -1;
|
696
|
+
}
|
697
|
+
boolean isNotNull = "NO".equals(rs.getString("IS_NULLABLE"));
|
698
|
+
//rs.getString("COLUMN_DEF") // or null // TODO
|
699
|
+
builder.add(JdbcColumn.newGenericTypeColumn(
|
700
|
+
columnName, sqlType, simpleTypeName, colSize, decDigit, isNotNull, isUniqueKey));
|
701
|
+
// We can't get declared column name using JDBC API.
|
702
|
+
// Subclasses need to overwrite it.
|
703
|
+
}
|
704
|
+
} finally {
|
705
|
+
rs.close();
|
706
|
+
}
|
707
|
+
List<JdbcColumn> columns = builder.build();
|
708
|
+
if (columns.isEmpty()) {
|
709
|
+
return Optional.absent();
|
710
|
+
} else {
|
711
|
+
return Optional.of(new JdbcSchema(columns));
|
712
|
+
}
|
713
|
+
}
|
714
|
+
|
715
|
+
private JdbcSchema matchSchemaByColumnNames(Schema inputSchema, JdbcSchema targetTableSchema)
|
716
|
+
{
|
717
|
+
ImmutableList.Builder<JdbcColumn> jdbcColumns = ImmutableList.builder();
|
718
|
+
|
719
|
+
for (Column column : inputSchema.getColumns()) {
|
720
|
+
Optional<JdbcColumn> c = targetTableSchema.findColumn(column.getName());
|
721
|
+
jdbcColumns.add(c.or(JdbcColumn.skipColumn()));
|
722
|
+
}
|
723
|
+
|
724
|
+
return new JdbcSchema(jdbcColumns.build());
|
725
|
+
}
|
726
|
+
|
727
|
+
public TransactionalPageOutput open(TaskSource taskSource, Schema schema, final int taskIndex)
|
728
|
+
{
|
729
|
+
final PluginTask task = taskSource.loadTask(getTaskClass());
|
730
|
+
final Mode mode = task.getMode();
|
731
|
+
|
732
|
+
// instantiate BatchInsert without table name
|
733
|
+
BatchInsert batch = null;
|
734
|
+
try {
|
735
|
+
batch = newBatchInsert(task,
|
736
|
+
task.getMode() == Mode.MERGE_DIRECT ?
|
737
|
+
task.getMergeKeys() :
|
738
|
+
Optional.<List<String>>absent());
|
739
|
+
} catch (IOException | SQLException ex) {
|
740
|
+
throw new RuntimeException(ex);
|
741
|
+
}
|
742
|
+
|
743
|
+
try {
|
744
|
+
// configure PageReader -> BatchInsert
|
745
|
+
PageReader reader = new PageReader(schema);
|
746
|
+
|
747
|
+
List<ColumnSetter> columnSetters = newColumnSetters(
|
748
|
+
new ColumnSetterFactory(batch, task.getDefaultTimeZone()),
|
749
|
+
task.getTargetTableSchema(), schema,
|
750
|
+
task.getColumnOptions());
|
751
|
+
JdbcSchema insertIntoSchema = filterSkipColumns(task.getTargetTableSchema());
|
752
|
+
|
753
|
+
// configure BatchInsert -> an intermediate table (!isDirectModify) or the target table (isDirectModify)
|
754
|
+
String destTable;
|
755
|
+
if (mode.tempTablePerTask()) {
|
756
|
+
destTable = task.getIntermediateTables().get().get(taskIndex);
|
757
|
+
} else if (mode.isDirectModify()) {
|
758
|
+
destTable = task.getTable();
|
759
|
+
} else {
|
760
|
+
destTable = task.getIntermediateTables().get().get(0);
|
761
|
+
}
|
762
|
+
batch.prepare(destTable, insertIntoSchema);
|
763
|
+
|
764
|
+
PluginPageOutput output = new PluginPageOutput(reader, batch, columnSetters, task.getBatchSize());
|
765
|
+
batch = null;
|
766
|
+
return output;
|
767
|
+
|
768
|
+
} catch (SQLException ex) {
|
769
|
+
throw new RuntimeException(ex);
|
770
|
+
|
771
|
+
} finally {
|
772
|
+
if (batch != null) {
|
773
|
+
try {
|
774
|
+
batch.close();
|
775
|
+
} catch (IOException | SQLException ex) {
|
776
|
+
throw new RuntimeException(ex);
|
777
|
+
}
|
778
|
+
}
|
779
|
+
}
|
780
|
+
}
|
781
|
+
|
782
|
+
public static class PluginPageOutput
|
783
|
+
implements TransactionalPageOutput
|
784
|
+
{
|
785
|
+
protected final List<Column> columns;
|
786
|
+
protected final List<ColumnSetterVisitor> columnVisitors;
|
787
|
+
private final PageReader pageReader;
|
788
|
+
private final BatchInsert batch;
|
789
|
+
private final int batchSize;
|
790
|
+
private final int forceBatchFlushSize;
|
791
|
+
|
792
|
+
public PluginPageOutput(final PageReader pageReader,
|
793
|
+
BatchInsert batch, List<ColumnSetter> columnSetters,
|
794
|
+
int batchSize)
|
795
|
+
{
|
796
|
+
this.pageReader = pageReader;
|
797
|
+
this.batch = batch;
|
798
|
+
this.columns = pageReader.getSchema().getColumns();
|
799
|
+
this.columnVisitors = ImmutableList.copyOf(Lists.transform(
|
800
|
+
columnSetters, new Function<ColumnSetter, ColumnSetterVisitor>() {
|
801
|
+
public ColumnSetterVisitor apply(ColumnSetter setter)
|
802
|
+
{
|
803
|
+
return new ColumnSetterVisitor(pageReader, setter);
|
804
|
+
}
|
805
|
+
}));
|
806
|
+
this.batchSize = batchSize;
|
807
|
+
this.forceBatchFlushSize = batchSize * 2;
|
808
|
+
}
|
809
|
+
|
810
|
+
@Override
|
811
|
+
public void add(Page page)
|
812
|
+
{
|
813
|
+
try {
|
814
|
+
pageReader.setPage(page);
|
815
|
+
while (pageReader.nextRecord()) {
|
816
|
+
if (batch.getBatchWeight() > forceBatchFlushSize) {
|
817
|
+
batch.flush();
|
818
|
+
}
|
819
|
+
handleColumnsSetters();
|
820
|
+
batch.add();
|
821
|
+
}
|
822
|
+
if (batch.getBatchWeight() > batchSize) {
|
823
|
+
batch.flush();
|
824
|
+
}
|
825
|
+
} catch (IOException | SQLException ex) {
|
826
|
+
throw new RuntimeException(ex);
|
827
|
+
}
|
828
|
+
}
|
829
|
+
|
830
|
+
@Override
|
831
|
+
public void finish()
|
832
|
+
{
|
833
|
+
try {
|
834
|
+
batch.finish();
|
835
|
+
} catch (IOException | SQLException ex) {
|
836
|
+
throw new RuntimeException(ex);
|
837
|
+
}
|
838
|
+
}
|
839
|
+
|
840
|
+
@Override
|
841
|
+
public void close()
|
842
|
+
{
|
843
|
+
try {
|
844
|
+
batch.close();
|
845
|
+
} catch (IOException | SQLException ex) {
|
846
|
+
throw new RuntimeException(ex);
|
847
|
+
}
|
848
|
+
}
|
849
|
+
|
850
|
+
@Override
|
851
|
+
public void abort()
|
852
|
+
{
|
853
|
+
}
|
854
|
+
|
855
|
+
@Override
|
856
|
+
public CommitReport commit()
|
857
|
+
{
|
858
|
+
return Exec.newCommitReport();
|
859
|
+
}
|
860
|
+
|
861
|
+
protected void handleColumnsSetters()
|
862
|
+
{
|
863
|
+
int size = columnVisitors.size();
|
864
|
+
for (int i=0; i < size; i++) {
|
865
|
+
columns.get(i).visit(columnVisitors.get(i));
|
866
|
+
}
|
867
|
+
}
|
868
|
+
}
|
869
|
+
|
870
|
+
public static interface IdempotentSqlRunnable
|
871
|
+
{
|
872
|
+
public void run() throws SQLException;
|
873
|
+
}
|
874
|
+
|
875
|
+
protected void withRetry(IdempotentSqlRunnable op)
|
876
|
+
throws SQLException, InterruptedException
|
877
|
+
{
|
878
|
+
withRetry(op, "Operation failed");
|
879
|
+
}
|
880
|
+
|
881
|
+
protected void withRetry(final IdempotentSqlRunnable op, final String errorMessage)
|
882
|
+
throws SQLException, InterruptedException
|
883
|
+
{
|
884
|
+
try {
|
885
|
+
retryExecutor()
|
886
|
+
.withRetryLimit(12)
|
887
|
+
.withInitialRetryWait(1000)
|
888
|
+
.withMaxRetryWait(30 * 60 * 1000)
|
889
|
+
.runInterruptible(new Retryable<Void>() {
|
890
|
+
public Void call() throws Exception
|
891
|
+
{
|
892
|
+
op.run();
|
893
|
+
return null;
|
894
|
+
}
|
895
|
+
|
896
|
+
public void onRetry(Exception exception, int retryCount, int retryLimit, int retryWait)
|
897
|
+
{
|
898
|
+
if (exception instanceof SQLException) {
|
899
|
+
SQLException ex = (SQLException) exception;
|
900
|
+
String sqlState = ex.getSQLState();
|
901
|
+
int errorCode = ex.getErrorCode();
|
902
|
+
logger.warn("{} ({}:{}), retrying {}/{} after {} seconds. Message: {}",
|
903
|
+
errorMessage, errorCode, sqlState, retryCount, retryLimit, retryWait/1000,
|
904
|
+
buildExceptionMessage(exception));
|
905
|
+
} else {
|
906
|
+
logger.warn("{}, retrying {}/{} after {} seconds. Message: {}",
|
907
|
+
errorMessage, retryCount, retryLimit, retryWait/1000,
|
908
|
+
buildExceptionMessage(exception));
|
909
|
+
}
|
910
|
+
if (retryCount % 3 == 0) {
|
911
|
+
logger.info("Error details:", exception);
|
912
|
+
}
|
913
|
+
}
|
914
|
+
|
915
|
+
public void onGiveup(Exception firstException, Exception lastException)
|
916
|
+
{
|
917
|
+
if (firstException instanceof SQLException) {
|
918
|
+
SQLException ex = (SQLException) firstException;
|
919
|
+
String sqlState = ex.getSQLState();
|
920
|
+
int errorCode = ex.getErrorCode();
|
921
|
+
logger.error("{} ({}:{})", errorMessage, errorCode, sqlState);
|
922
|
+
}
|
923
|
+
}
|
924
|
+
|
925
|
+
public boolean isRetryableException(Exception exception)
|
926
|
+
{
|
927
|
+
//if (exception instanceof SQLException) {
|
928
|
+
// SQLException ex = (SQLException) exception;
|
929
|
+
// String sqlState = ex.getSQLState();
|
930
|
+
// int errorCode = ex.getErrorCode();
|
931
|
+
// return isRetryableSQLException(ex);
|
932
|
+
//}
|
933
|
+
return false; // TODO
|
934
|
+
}
|
935
|
+
});
|
936
|
+
|
937
|
+
} catch (ExecutionException ex) {
|
938
|
+
Throwable cause = ex.getCause();
|
939
|
+
Throwables.propagateIfInstanceOf(cause, SQLException.class);
|
940
|
+
throw Throwables.propagate(cause);
|
941
|
+
}
|
942
|
+
}
|
943
|
+
|
944
|
+
private String buildExceptionMessage(Throwable ex) {
|
945
|
+
StringBuilder sb = new StringBuilder();
|
946
|
+
sb.append(ex.getMessage());
|
947
|
+
if (ex.getCause() != null) {
|
948
|
+
buildExceptionMessageCont(sb, ex.getCause(), ex.getMessage());
|
949
|
+
}
|
950
|
+
return sb.toString();
|
951
|
+
}
|
952
|
+
|
953
|
+
private void buildExceptionMessageCont(StringBuilder sb, Throwable ex, String lastMessage) {
|
954
|
+
if (!lastMessage.equals(ex.getMessage())) {
|
955
|
+
// suppress same messages
|
956
|
+
sb.append(" < ");
|
957
|
+
sb.append(ex.getMessage());
|
958
|
+
}
|
959
|
+
if (ex.getCause() == null) {
|
960
|
+
return;
|
961
|
+
}
|
962
|
+
buildExceptionMessageCont(sb, ex.getCause(), ex.getMessage());
|
963
|
+
}
|
964
|
+
}
|