embulk-output-jdbc 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,105 +1,105 @@
1
- package org.embulk.output.jdbc;
2
-
3
- import java.util.concurrent.Callable;
4
- import java.util.concurrent.ExecutionException;
5
-
6
- public class RetryExecutor
7
- {
8
- public static RetryExecutor retryExecutor()
9
- {
10
- return new RetryExecutor();
11
- }
12
-
13
- public static abstract class IdempotentOperation<T> implements Callable<T>
14
- {
15
- public abstract T call() throws Exception;
16
-
17
- public void onRetry(Throwable exception, int retryCount, int retryLimit, int retryWait)
18
- { }
19
-
20
- public void onGiveup(Throwable firstException, Throwable lastException)
21
- { }
22
-
23
- public abstract boolean isRetryableException(Throwable exception);
24
- }
25
-
26
- private int retryLimit = 3;
27
- private int initialRetryWait = 500;
28
- private int maxRetryWait = 30*60*1000;
29
-
30
- private RetryExecutor()
31
- { }
32
-
33
- public RetryExecutor setRetryLimit(int count)
34
- {
35
- this.retryLimit = count;
36
- return this;
37
- }
38
-
39
- public RetryExecutor setInitialRetryWait(int msec)
40
- {
41
- this.initialRetryWait = msec;
42
- return this;
43
- }
44
-
45
- public RetryExecutor setMaxRetryWait(int msec)
46
- {
47
- this.maxRetryWait = msec;
48
- return this;
49
- }
50
-
51
- public <T> T runInterruptible(IdempotentOperation<T> op) throws InterruptedException, ExecutionException
52
- {
53
- return run(op, true);
54
- }
55
-
56
- public <T> T run(IdempotentOperation<T> op) throws ExecutionException
57
- {
58
- try {
59
- return run(op, false);
60
- } catch (InterruptedException ex) {
61
- throw new ExecutionException("Unexpected interruption", ex);
62
- }
63
- }
64
-
65
- private <T> T run(IdempotentOperation<T> op, boolean interruptible)
66
- throws InterruptedException, ExecutionException
67
- {
68
- int retryWait = initialRetryWait;
69
- int retryCount = 0;
70
-
71
- Throwable firstException = null;
72
-
73
- while(true) {
74
- try {
75
- return op.call();
76
- } catch (Throwable exception) {
77
- if (firstException == null) {
78
- firstException = exception;
79
- }
80
- if (!op.isRetryableException(exception) || retryCount >= retryLimit) {
81
- op.onGiveup(firstException, exception);
82
- throw new ExecutionException(firstException);
83
- }
84
-
85
- retryCount++;
86
- op.onRetry(exception, retryCount, retryLimit, retryWait);
87
-
88
- try {
89
- Thread.sleep(retryWait);
90
- } catch (InterruptedException ex) {
91
- if (interruptible) {
92
- throw ex;
93
- }
94
- }
95
-
96
- retryWait *= 2; // exponential back-off
97
-
98
- if (retryWait > maxRetryWait) {
99
- retryWait = maxRetryWait;
100
- }
101
- }
102
- }
103
- }
104
- }
105
-
1
+ package org.embulk.output.jdbc;
2
+
3
+ import java.util.concurrent.Callable;
4
+ import java.util.concurrent.ExecutionException;
5
+
6
+ public class RetryExecutor
7
+ {
8
+ public static RetryExecutor retryExecutor()
9
+ {
10
+ return new RetryExecutor();
11
+ }
12
+
13
+ public static abstract class IdempotentOperation<T> implements Callable<T>
14
+ {
15
+ public abstract T call() throws Exception;
16
+
17
+ public void onRetry(Throwable exception, int retryCount, int retryLimit, int retryWait)
18
+ { }
19
+
20
+ public void onGiveup(Throwable firstException, Throwable lastException)
21
+ { }
22
+
23
+ public abstract boolean isRetryableException(Throwable exception);
24
+ }
25
+
26
+ private int retryLimit = 3;
27
+ private int initialRetryWait = 500;
28
+ private int maxRetryWait = 30*60*1000;
29
+
30
+ private RetryExecutor()
31
+ { }
32
+
33
+ public RetryExecutor setRetryLimit(int count)
34
+ {
35
+ this.retryLimit = count;
36
+ return this;
37
+ }
38
+
39
+ public RetryExecutor setInitialRetryWait(int msec)
40
+ {
41
+ this.initialRetryWait = msec;
42
+ return this;
43
+ }
44
+
45
+ public RetryExecutor setMaxRetryWait(int msec)
46
+ {
47
+ this.maxRetryWait = msec;
48
+ return this;
49
+ }
50
+
51
+ public <T> T runInterruptible(IdempotentOperation<T> op) throws InterruptedException, ExecutionException
52
+ {
53
+ return run(op, true);
54
+ }
55
+
56
+ public <T> T run(IdempotentOperation<T> op) throws ExecutionException
57
+ {
58
+ try {
59
+ return run(op, false);
60
+ } catch (InterruptedException ex) {
61
+ throw new ExecutionException("Unexpected interruption", ex);
62
+ }
63
+ }
64
+
65
+ private <T> T run(IdempotentOperation<T> op, boolean interruptible)
66
+ throws InterruptedException, ExecutionException
67
+ {
68
+ int retryWait = initialRetryWait;
69
+ int retryCount = 0;
70
+
71
+ Throwable firstException = null;
72
+
73
+ while(true) {
74
+ try {
75
+ return op.call();
76
+ } catch (Throwable exception) {
77
+ if (firstException == null) {
78
+ firstException = exception;
79
+ }
80
+ if (!op.isRetryableException(exception) || retryCount >= retryLimit) {
81
+ op.onGiveup(firstException, exception);
82
+ throw new ExecutionException(firstException);
83
+ }
84
+
85
+ retryCount++;
86
+ op.onRetry(exception, retryCount, retryLimit, retryWait);
87
+
88
+ try {
89
+ Thread.sleep(retryWait);
90
+ } catch (InterruptedException ex) {
91
+ if (interruptible) {
92
+ throw ex;
93
+ }
94
+ }
95
+
96
+ retryWait *= 2; // exponential back-off
97
+
98
+ if (retryWait > maxRetryWait) {
99
+ retryWait = maxRetryWait;
100
+ }
101
+ }
102
+ }
103
+ }
104
+ }
105
+
@@ -1,52 +1,52 @@
1
- package org.embulk.output.jdbc.setter;
2
-
3
- import java.io.IOException;
4
- import java.sql.SQLException;
5
- import com.google.common.collect.ImmutableSet;
6
- import org.embulk.spi.PageReader;
7
- import org.embulk.spi.time.Timestamp;
8
- import org.embulk.output.jdbc.JdbcColumn;
9
- import org.embulk.output.jdbc.BatchInsert;
10
-
11
- public class BooleanColumnSetter
12
- extends ColumnSetter
13
- {
14
- private static final ImmutableSet<String> trueStrings =
15
- ImmutableSet.<String>of(
16
- "true", "True", "TRUE",
17
- "yes", "Yes", "YES",
18
- "y", "Y",
19
- "on", "On", "ON",
20
- "1");
21
-
22
- public BooleanColumnSetter(BatchInsert batch, PageReader pageReader,
23
- JdbcColumn column)
24
- {
25
- super(batch, pageReader, column);
26
- }
27
-
28
- protected void booleanValue(boolean v) throws IOException, SQLException
29
- {
30
- batch.setBoolean(v);
31
- }
32
-
33
- protected void longValue(long v) throws IOException, SQLException
34
- {
35
- batch.setBoolean(v > 0);
36
- }
37
-
38
- protected void doubleValue(double v) throws IOException, SQLException
39
- {
40
- batch.setBoolean(v > 0.0);
41
- }
42
-
43
- protected void stringValue(String v) throws IOException, SQLException
44
- {
45
- batch.setBoolean(trueStrings.contains(v));
46
- }
47
-
48
- protected void timestampValue(Timestamp v) throws IOException, SQLException
49
- {
50
- nullValue();
51
- }
52
- }
1
+ package org.embulk.output.jdbc.setter;
2
+
3
+ import java.io.IOException;
4
+ import java.sql.SQLException;
5
+ import com.google.common.collect.ImmutableSet;
6
+ import org.embulk.spi.PageReader;
7
+ import org.embulk.spi.time.Timestamp;
8
+ import org.embulk.output.jdbc.JdbcColumn;
9
+ import org.embulk.output.jdbc.BatchInsert;
10
+
11
+ public class BooleanColumnSetter
12
+ extends ColumnSetter
13
+ {
14
+ private static final ImmutableSet<String> trueStrings =
15
+ ImmutableSet.<String>of(
16
+ "true", "True", "TRUE",
17
+ "yes", "Yes", "YES",
18
+ "y", "Y",
19
+ "on", "On", "ON",
20
+ "1");
21
+
22
+ public BooleanColumnSetter(BatchInsert batch, PageReader pageReader,
23
+ JdbcColumn column)
24
+ {
25
+ super(batch, pageReader, column);
26
+ }
27
+
28
+ protected void booleanValue(boolean v) throws IOException, SQLException
29
+ {
30
+ batch.setBoolean(v);
31
+ }
32
+
33
+ protected void longValue(long v) throws IOException, SQLException
34
+ {
35
+ batch.setBoolean(v > 0);
36
+ }
37
+
38
+ protected void doubleValue(double v) throws IOException, SQLException
39
+ {
40
+ batch.setBoolean(v > 0.0);
41
+ }
42
+
43
+ protected void stringValue(String v) throws IOException, SQLException
44
+ {
45
+ batch.setBoolean(trueStrings.contains(v));
46
+ }
47
+
48
+ protected void timestampValue(Timestamp v) throws IOException, SQLException
49
+ {
50
+ nullValue();
51
+ }
52
+ }
@@ -1,137 +1,137 @@
1
- package org.embulk.output.jdbc.setter;
2
-
3
- import java.sql.Types;
4
- import org.embulk.spi.time.TimestampFormatter;
5
- import org.embulk.spi.PageReader;
6
- import org.embulk.output.jdbc.BatchInsert;
7
- import org.embulk.output.jdbc.JdbcColumn;
8
-
9
- public class ColumnSetterFactory
10
- {
11
- protected final BatchInsert batch;
12
- protected final PageReader pageReader;
13
- protected final TimestampFormatter timestampFormatter;
14
-
15
- public ColumnSetterFactory(BatchInsert batch, PageReader pageReader,
16
- TimestampFormatter timestampFormatter)
17
- {
18
- this.batch = batch;
19
- this.pageReader = pageReader;
20
- this.timestampFormatter = timestampFormatter;
21
- }
22
-
23
- public SkipColumnSetter newSkipColumnSetter()
24
- {
25
- return new SkipColumnSetter(batch, pageReader);
26
- }
27
-
28
- public ColumnSetter newColumnSetter(JdbcColumn column)
29
- {
30
- switch(column.getSqlType()) {
31
- //// TODO
32
- // setByte
33
- //case Types.TINYINT:
34
- // return new ByteColumnSetter(batch, pageReader, column);
35
-
36
- //// TODO
37
- //// setShort
38
- //case Types.SMALLINT:
39
- // return new ShortColumnSetter(batch, pageReader, column);
40
-
41
- //// TODO
42
- //// setInt
43
- //case Types.INTEGER:
44
- // return new IntColumnSetter(batch, pageReader, column);
45
-
46
- // setLong
47
- case Types.BIGINT:
48
- return new LongColumnSetter(batch, pageReader, column);
49
-
50
- // setDouble
51
- case Types.DOUBLE:
52
- case Types.FLOAT:
53
- return new DoubleColumnSetter(batch, pageReader, column);
54
-
55
- // TODO
56
- //// setFloat
57
- //case Types.REAL:
58
- // return new FloatColumnSetter(batch, pageReader, column);
59
-
60
- // setBool
61
- case Types.BOOLEAN:
62
- case Types.BIT: // JDBC BIT is boolean, unlike SQL-92
63
- return new BooleanColumnSetter(batch, pageReader, column);
64
-
65
- // setString, Clob
66
- case Types.CHAR:
67
- case Types.VARCHAR:
68
- case Types.LONGVARCHAR:
69
- case Types.CLOB:
70
- return new StringColumnSetter(batch, pageReader, column, timestampFormatter);
71
-
72
- // TODO
73
- //// setNString, NClob
74
- //case Types.NCHAR:
75
- //case Types.NVARCHAR:
76
- //case Types.LONGNVARCHAR:
77
- // return new NStringColumnSetter(batch, pageReader, column);
78
-
79
- // TODO
80
- //// setBytes Blob
81
- //case Types.BINARY:
82
- //case Types.VARBINARY:
83
- //case Types.LONGVARBINARY:
84
- //case Types.BLOB:
85
- // return new BytesColumnSetter(batch, pageReader, column);
86
-
87
- // Time
88
- //case Types.DATE:
89
- // return new SqlDateColumnSetter(batch, pageReader, column); // TODO
90
- //case Types.TIME:
91
- // return new SqlTimeColumnSetter(batch, pageReader, column); // TODO
92
- case Types.TIMESTAMP:
93
- return new SqlTimestampColumnSetter(batch, pageReader, column);
94
-
95
- // Null
96
- case Types.NULL:
97
- return new NullColumnSetter(batch, pageReader, column);
98
-
99
- // TODO
100
- //// BigDecimal
101
- //case Types.NUMERIC:
102
- //case Types.DECIMAL:
103
- // return new BigDecimalColumnSetter(batch, pageReader, column);
104
-
105
- // others
106
- case Types.ARRAY: // array
107
- case Types.STRUCT: // map
108
- case Types.REF:
109
- case Types.DATALINK:
110
- case Types.SQLXML: // XML
111
- case Types.ROWID:
112
- case Types.DISTINCT:
113
- case Types.JAVA_OBJECT:
114
- case Types.OTHER:
115
- default:
116
- throw unsupportedOperationException(column);
117
- }
118
- }
119
-
120
- //private static String[] UNSUPPORTED = new String[] {
121
- // "ARRAY",
122
- // "STRUCT",
123
- // "REF",
124
- // "DATALINK",
125
- // "SQLXML",
126
- // "ROWID",
127
- // "DISTINCT",
128
- // "OTHER",
129
- //};
130
-
131
- private static UnsupportedOperationException unsupportedOperationException(JdbcColumn column)
132
- {
133
- throw new UnsupportedOperationException(
134
- String.format("Unsupported type %s (sqlType=%d, size=%d, scale=%d)",
135
- column.getTypeName(), column.getSqlType(), column.getSizeTypeParameter(), column.getScaleTypeParameter()));
136
- }
137
- }
1
+ package org.embulk.output.jdbc.setter;
2
+
3
+ import java.sql.Types;
4
+ import org.embulk.spi.time.TimestampFormatter;
5
+ import org.embulk.spi.PageReader;
6
+ import org.embulk.output.jdbc.BatchInsert;
7
+ import org.embulk.output.jdbc.JdbcColumn;
8
+
9
+ public class ColumnSetterFactory
10
+ {
11
+ protected final BatchInsert batch;
12
+ protected final PageReader pageReader;
13
+ protected final TimestampFormatter timestampFormatter;
14
+
15
+ public ColumnSetterFactory(BatchInsert batch, PageReader pageReader,
16
+ TimestampFormatter timestampFormatter)
17
+ {
18
+ this.batch = batch;
19
+ this.pageReader = pageReader;
20
+ this.timestampFormatter = timestampFormatter;
21
+ }
22
+
23
+ public SkipColumnSetter newSkipColumnSetter()
24
+ {
25
+ return new SkipColumnSetter(batch, pageReader);
26
+ }
27
+
28
+ public ColumnSetter newColumnSetter(JdbcColumn column)
29
+ {
30
+ switch(column.getSqlType()) {
31
+ //// TODO
32
+ // setByte
33
+ //case Types.TINYINT:
34
+ // return new ByteColumnSetter(batch, pageReader, column);
35
+
36
+ //// TODO
37
+ //// setShort
38
+ //case Types.SMALLINT:
39
+ // return new ShortColumnSetter(batch, pageReader, column);
40
+
41
+ //// TODO
42
+ //// setInt
43
+ //case Types.INTEGER:
44
+ // return new IntColumnSetter(batch, pageReader, column);
45
+
46
+ // setLong
47
+ case Types.BIGINT:
48
+ return new LongColumnSetter(batch, pageReader, column);
49
+
50
+ // setDouble
51
+ case Types.DOUBLE:
52
+ case Types.FLOAT:
53
+ return new DoubleColumnSetter(batch, pageReader, column);
54
+
55
+ // TODO
56
+ //// setFloat
57
+ //case Types.REAL:
58
+ // return new FloatColumnSetter(batch, pageReader, column);
59
+
60
+ // setBool
61
+ case Types.BOOLEAN:
62
+ case Types.BIT: // JDBC BIT is boolean, unlike SQL-92
63
+ return new BooleanColumnSetter(batch, pageReader, column);
64
+
65
+ // setString, Clob
66
+ case Types.CHAR:
67
+ case Types.VARCHAR:
68
+ case Types.LONGVARCHAR:
69
+ case Types.CLOB:
70
+ return new StringColumnSetter(batch, pageReader, column, timestampFormatter);
71
+
72
+ // TODO
73
+ //// setNString, NClob
74
+ //case Types.NCHAR:
75
+ //case Types.NVARCHAR:
76
+ //case Types.LONGNVARCHAR:
77
+ // return new NStringColumnSetter(batch, pageReader, column);
78
+
79
+ // TODO
80
+ //// setBytes Blob
81
+ //case Types.BINARY:
82
+ //case Types.VARBINARY:
83
+ //case Types.LONGVARBINARY:
84
+ //case Types.BLOB:
85
+ // return new BytesColumnSetter(batch, pageReader, column);
86
+
87
+ // Time
88
+ //case Types.DATE:
89
+ // return new SqlDateColumnSetter(batch, pageReader, column); // TODO
90
+ //case Types.TIME:
91
+ // return new SqlTimeColumnSetter(batch, pageReader, column); // TODO
92
+ case Types.TIMESTAMP:
93
+ return new SqlTimestampColumnSetter(batch, pageReader, column);
94
+
95
+ // Null
96
+ case Types.NULL:
97
+ return new NullColumnSetter(batch, pageReader, column);
98
+
99
+ // TODO
100
+ //// BigDecimal
101
+ //case Types.NUMERIC:
102
+ //case Types.DECIMAL:
103
+ // return new BigDecimalColumnSetter(batch, pageReader, column);
104
+
105
+ // others
106
+ case Types.ARRAY: // array
107
+ case Types.STRUCT: // map
108
+ case Types.REF:
109
+ case Types.DATALINK:
110
+ case Types.SQLXML: // XML
111
+ case Types.ROWID:
112
+ case Types.DISTINCT:
113
+ case Types.JAVA_OBJECT:
114
+ case Types.OTHER:
115
+ default:
116
+ throw unsupportedOperationException(column);
117
+ }
118
+ }
119
+
120
+ //private static String[] UNSUPPORTED = new String[] {
121
+ // "ARRAY",
122
+ // "STRUCT",
123
+ // "REF",
124
+ // "DATALINK",
125
+ // "SQLXML",
126
+ // "ROWID",
127
+ // "DISTINCT",
128
+ // "OTHER",
129
+ //};
130
+
131
+ private static UnsupportedOperationException unsupportedOperationException(JdbcColumn column)
132
+ {
133
+ throw new UnsupportedOperationException(
134
+ String.format("Unsupported type %s (sqlType=%d, size=%d, scale=%d)",
135
+ column.getTypeName(), column.getSqlType(), column.getSizeTypeParameter(), column.getScaleTypeParameter()));
136
+ }
137
+ }