embulk-output-postgresql 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,245 +1,242 @@
1
- package org.embulk.output.postgresql;
2
-
3
- import java.util.Calendar;
4
- import java.util.Locale;
5
- import java.io.File;
6
- import java.io.FileOutputStream;
7
- import java.io.Writer;
8
- import java.io.BufferedWriter;
9
- import java.io.OutputStreamWriter;
10
- import java.io.IOException;
11
- import java.nio.charset.Charset;
12
- import java.math.BigDecimal;
13
- import java.sql.Date;
14
- import java.sql.Time;
15
- import java.sql.SQLException;
16
- import org.embulk.spi.time.Timestamp;
17
- import org.embulk.output.jdbc.BatchInsert;
18
-
19
- public abstract class AbstractPostgreSQLCopyBatchInsert
20
- implements BatchInsert
21
- {
22
- protected static final Charset FILE_CHARSET = Charset.forName("UTF-8");
23
-
24
- protected static final String nullString = "\\N";
25
- protected static final String newLineString = "\n";
26
- protected static final String delimiterString = "\t";
27
-
28
- protected File currentFile;
29
- protected BufferedWriter writer;
30
- protected int index;
31
- protected int batchRows;
32
-
33
- protected AbstractPostgreSQLCopyBatchInsert() throws IOException
34
- {
35
- this.index = 0;
36
- openNewFile();
37
- }
38
-
39
- private File createTempFile() throws IOException
40
- {
41
- return File.createTempFile("embulk-output-postgres-copy-", ".tsv.tmp"); // TODO configurable temporary file path
42
- }
43
-
44
- protected File openNewFile() throws IOException
45
- {
46
- File newFile = createTempFile();
47
- File oldFile = closeCurrentFile();
48
- this.writer = openWriter(newFile);
49
- currentFile = newFile;
50
- return oldFile;
51
- }
52
-
53
- protected File closeCurrentFile() throws IOException
54
- {
55
- if(writer != null) {
56
- writer.close();
57
- writer = null;
58
- }
59
- return currentFile;
60
- }
61
-
62
- protected BufferedWriter openWriter(File newFile) throws IOException
63
- {
64
- return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), FILE_CHARSET));
65
- }
66
-
67
- public int getBatchWeight()
68
- {
69
- long fsize = currentFile.length();
70
- if (fsize > Integer.MAX_VALUE) {
71
- return Integer.MAX_VALUE;
72
- } else {
73
- return (int) fsize;
74
- }
75
- }
76
-
77
- public void finish() throws IOException, SQLException
78
- {
79
- closeCurrentFile(); // this is necessary to make getBatchWeight() work
80
- if (getBatchWeight() != 0) {
81
- flush();
82
- }
83
- }
84
-
85
- public void add() throws IOException
86
- {
87
- writer.write(newLineString);
88
- batchRows++;
89
- index = 0;
90
- }
91
-
92
- private void appendDelimiter() throws IOException
93
- {
94
- if(index != 0) {
95
- writer.write(delimiterString);
96
- }
97
- index++;
98
- }
99
-
100
- public void setNull(int sqlType) throws IOException
101
- {
102
- appendDelimiter();
103
- writer.write(nullString);
104
- }
105
-
106
- public void setBoolean(boolean v) throws IOException
107
- {
108
- appendDelimiter();
109
- writer.write(String.valueOf(v));
110
- }
111
-
112
- public void setByte(byte v) throws IOException
113
- {
114
- appendDelimiter();
115
- setEscapedString(String.valueOf(v));
116
- }
117
-
118
- public void setShort(short v) throws IOException
119
- {
120
- appendDelimiter();
121
- writer.write(String.valueOf(v));
122
- }
123
-
124
- public void setInt(int v) throws IOException
125
- {
126
- appendDelimiter();
127
- writer.write(String.valueOf(v));
128
- }
129
-
130
- public void setLong(long v) throws IOException
131
- {
132
- appendDelimiter();
133
- writer.write(String.valueOf(v));
134
- }
135
-
136
- public void setFloat(float v) throws IOException
137
- {
138
- appendDelimiter();
139
- writer.write(String.valueOf(v));
140
- }
141
-
142
- public void setDouble(double v) throws IOException
143
- {
144
- appendDelimiter();
145
- writer.write(String.valueOf(v));
146
- }
147
-
148
- public void setBigDecimal(BigDecimal v) throws IOException
149
- {
150
- appendDelimiter();
151
- writer.write(String.valueOf(v));
152
- }
153
-
154
- public void setString(String v) throws IOException
155
- {
156
- appendDelimiter();
157
- setEscapedString(v);
158
- }
159
-
160
- public void setNString(String v) throws IOException
161
- {
162
- appendDelimiter();
163
- setEscapedString(v);
164
- }
165
-
166
- public void setBytes(byte[] v) throws IOException
167
- {
168
- appendDelimiter();
169
- setEscapedString(String.valueOf(v));
170
- }
171
-
172
- public void setSqlDate(Timestamp v, Calendar cal) throws IOException
173
- {
174
- appendDelimiter();
175
- cal.setTimeInMillis(v.getEpochSecond() * 1000);
176
- String f = String.format(Locale.ENGLISH, "%02d-%02d-%02d",
177
- cal.get(Calendar.YEAR),
178
- cal.get(Calendar.MONTH) + 1,
179
- cal.get(Calendar.DAY_OF_MONTH));
180
- writer.write(f);
181
- }
182
-
183
- public void setSqlTime(Timestamp v, Calendar cal) throws IOException
184
- {
185
- appendDelimiter();
186
- cal.setTimeInMillis(v.getEpochSecond() * 1000);
187
- String f = String.format(Locale.ENGLISH, "%02d:%02d:%02d.%06d",
188
- cal.get(Calendar.HOUR_OF_DAY),
189
- cal.get(Calendar.MINUTE),
190
- cal.get(Calendar.SECOND),
191
- v.getNano() / 1000);
192
- writer.write(f);
193
- }
194
-
195
- public void setSqlTimestamp(Timestamp v, Calendar cal) throws IOException
196
- {
197
- appendDelimiter();
198
- cal.setTimeInMillis(v.getEpochSecond() * 1000);
199
- int zoneOffset = cal.get(Calendar.ZONE_OFFSET) / 1000 / 60; // zone offset considering DST in minute
200
- String offset;
201
- if (zoneOffset >= 0) {
202
- offset = String.format(Locale.ENGLISH, "+%02d%02d", zoneOffset / 60, zoneOffset % 60);
203
- } else {
204
- offset = String.format(Locale.ENGLISH, "-%02d%02d", -zoneOffset / 60, -zoneOffset % 60);
205
- }
206
- String f = String.format(Locale.ENGLISH, "%d-%02d-%02d %02d:%02d:%02d.%06d%s",
207
- cal.get(Calendar.YEAR),
208
- cal.get(Calendar.MONTH) + 1,
209
- cal.get(Calendar.DAY_OF_MONTH),
210
- cal.get(Calendar.HOUR_OF_DAY),
211
- cal.get(Calendar.MINUTE),
212
- cal.get(Calendar.SECOND),
213
- v.getNano() / 1000,
214
- offset);
215
- writer.write(f);
216
- }
217
-
218
- // Escape \, \n, \t, \r
219
- // Remove \0
220
- private void setEscapedString(String v) throws IOException{
221
- for (char c : v.toCharArray()) {
222
- String s;
223
- switch (c) {
224
- case '\\':
225
- s = "\\\\";
226
- break;
227
- case '\n':
228
- s = "\\n";
229
- break;
230
- case '\t':
231
- s = "\\t";
232
- break;
233
- case '\r':
234
- s = "\\r";
235
- break;
236
- case 0:
237
- s = "";
238
- break;
239
- default:
240
- s = String.valueOf(c);
241
- }
242
- writer.write(s);
243
- }
244
- }
245
- }
1
+ package org.embulk.output.postgresql;
2
+
3
+ import java.util.Calendar;
4
+ import java.util.Locale;
5
+ import java.io.File;
6
+ import java.io.FileOutputStream;
7
+ import java.io.BufferedWriter;
8
+ import java.io.OutputStreamWriter;
9
+ import java.io.IOException;
10
+ import java.nio.charset.Charset;
11
+ import java.math.BigDecimal;
12
+ import java.sql.SQLException;
13
+ import org.embulk.spi.time.Timestamp;
14
+ import org.embulk.output.jdbc.BatchInsert;
15
+
16
+ public abstract class AbstractPostgreSQLCopyBatchInsert
17
+ implements BatchInsert
18
+ {
19
+ protected static final Charset FILE_CHARSET = Charset.forName("UTF-8");
20
+
21
+ protected static final String nullString = "\\N";
22
+ protected static final String newLineString = "\n";
23
+ protected static final String delimiterString = "\t";
24
+
25
+ protected File currentFile;
26
+ protected BufferedWriter writer;
27
+ protected int index;
28
+ protected int batchRows;
29
+
30
+ protected AbstractPostgreSQLCopyBatchInsert() throws IOException
31
+ {
32
+ this.index = 0;
33
+ openNewFile();
34
+ }
35
+
36
+ private File createTempFile() throws IOException
37
+ {
38
+ return File.createTempFile("embulk-output-postgres-copy-", ".tsv.tmp"); // TODO configurable temporary file path
39
+ }
40
+
41
+ protected File openNewFile() throws IOException
42
+ {
43
+ File newFile = createTempFile();
44
+ File oldFile = closeCurrentFile();
45
+ this.writer = openWriter(newFile);
46
+ currentFile = newFile;
47
+ return oldFile;
48
+ }
49
+
50
+ protected File closeCurrentFile() throws IOException
51
+ {
52
+ if(writer != null) {
53
+ writer.close();
54
+ writer = null;
55
+ }
56
+ return currentFile;
57
+ }
58
+
59
+ protected BufferedWriter openWriter(File newFile) throws IOException
60
+ {
61
+ return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), FILE_CHARSET));
62
+ }
63
+
64
+ public int getBatchWeight()
65
+ {
66
+ long fsize = currentFile.length();
67
+ if (fsize > Integer.MAX_VALUE) {
68
+ return Integer.MAX_VALUE;
69
+ } else {
70
+ return (int) fsize;
71
+ }
72
+ }
73
+
74
+ public void finish() throws IOException, SQLException
75
+ {
76
+ closeCurrentFile(); // this is necessary to make getBatchWeight() work
77
+ if (getBatchWeight() != 0) {
78
+ flush();
79
+ }
80
+ }
81
+
82
+ public void add() throws IOException
83
+ {
84
+ writer.write(newLineString);
85
+ batchRows++;
86
+ index = 0;
87
+ }
88
+
89
+ private void appendDelimiter() throws IOException
90
+ {
91
+ if(index != 0) {
92
+ writer.write(delimiterString);
93
+ }
94
+ index++;
95
+ }
96
+
97
+ public void setNull(int sqlType) throws IOException
98
+ {
99
+ appendDelimiter();
100
+ writer.write(nullString);
101
+ }
102
+
103
+ public void setBoolean(boolean v) throws IOException
104
+ {
105
+ appendDelimiter();
106
+ writer.write(String.valueOf(v));
107
+ }
108
+
109
+ public void setByte(byte v) throws IOException
110
+ {
111
+ appendDelimiter();
112
+ setEscapedString(String.valueOf(v));
113
+ }
114
+
115
+ public void setShort(short v) throws IOException
116
+ {
117
+ appendDelimiter();
118
+ writer.write(String.valueOf(v));
119
+ }
120
+
121
+ public void setInt(int v) throws IOException
122
+ {
123
+ appendDelimiter();
124
+ writer.write(String.valueOf(v));
125
+ }
126
+
127
+ public void setLong(long v) throws IOException
128
+ {
129
+ appendDelimiter();
130
+ writer.write(String.valueOf(v));
131
+ }
132
+
133
+ public void setFloat(float v) throws IOException
134
+ {
135
+ appendDelimiter();
136
+ writer.write(String.valueOf(v));
137
+ }
138
+
139
+ public void setDouble(double v) throws IOException
140
+ {
141
+ appendDelimiter();
142
+ writer.write(String.valueOf(v));
143
+ }
144
+
145
+ public void setBigDecimal(BigDecimal v) throws IOException
146
+ {
147
+ appendDelimiter();
148
+ writer.write(String.valueOf(v));
149
+ }
150
+
151
+ public void setString(String v) throws IOException
152
+ {
153
+ appendDelimiter();
154
+ setEscapedString(v);
155
+ }
156
+
157
+ public void setNString(String v) throws IOException
158
+ {
159
+ appendDelimiter();
160
+ setEscapedString(v);
161
+ }
162
+
163
+ public void setBytes(byte[] v) throws IOException
164
+ {
165
+ appendDelimiter();
166
+ setEscapedString(String.valueOf(v));
167
+ }
168
+
169
+ public void setSqlDate(Timestamp v, Calendar cal) throws IOException
170
+ {
171
+ appendDelimiter();
172
+ cal.setTimeInMillis(v.getEpochSecond() * 1000);
173
+ String f = String.format(Locale.ENGLISH, "%02d-%02d-%02d",
174
+ cal.get(Calendar.YEAR),
175
+ cal.get(Calendar.MONTH) + 1,
176
+ cal.get(Calendar.DAY_OF_MONTH));
177
+ writer.write(f);
178
+ }
179
+
180
+ public void setSqlTime(Timestamp v, Calendar cal) throws IOException
181
+ {
182
+ appendDelimiter();
183
+ cal.setTimeInMillis(v.getEpochSecond() * 1000);
184
+ String f = String.format(Locale.ENGLISH, "%02d:%02d:%02d.%06d",
185
+ cal.get(Calendar.HOUR_OF_DAY),
186
+ cal.get(Calendar.MINUTE),
187
+ cal.get(Calendar.SECOND),
188
+ v.getNano() / 1000);
189
+ writer.write(f);
190
+ }
191
+
192
+ public void setSqlTimestamp(Timestamp v, Calendar cal) throws IOException
193
+ {
194
+ appendDelimiter();
195
+ cal.setTimeInMillis(v.getEpochSecond() * 1000);
196
+ int zoneOffset = cal.get(Calendar.ZONE_OFFSET) / 1000 / 60; // zone offset considering DST in minute
197
+ String offset;
198
+ if (zoneOffset >= 0) {
199
+ offset = String.format(Locale.ENGLISH, "+%02d%02d", zoneOffset / 60, zoneOffset % 60);
200
+ } else {
201
+ offset = String.format(Locale.ENGLISH, "-%02d%02d", -zoneOffset / 60, -zoneOffset % 60);
202
+ }
203
+ String f = String.format(Locale.ENGLISH, "%d-%02d-%02d %02d:%02d:%02d.%06d%s",
204
+ cal.get(Calendar.YEAR),
205
+ cal.get(Calendar.MONTH) + 1,
206
+ cal.get(Calendar.DAY_OF_MONTH),
207
+ cal.get(Calendar.HOUR_OF_DAY),
208
+ cal.get(Calendar.MINUTE),
209
+ cal.get(Calendar.SECOND),
210
+ v.getNano() / 1000,
211
+ offset);
212
+ writer.write(f);
213
+ }
214
+
215
+ // Escape \, \n, \t, \r
216
+ // Remove \0
217
+ private void setEscapedString(String v) throws IOException{
218
+ for (char c : v.toCharArray()) {
219
+ String s;
220
+ switch (c) {
221
+ case '\\':
222
+ s = "\\\\";
223
+ break;
224
+ case '\n':
225
+ s = "\\n";
226
+ break;
227
+ case '\t':
228
+ s = "\\t";
229
+ break;
230
+ case '\r':
231
+ s = "\\r";
232
+ break;
233
+ case 0:
234
+ s = "";
235
+ break;
236
+ default:
237
+ s = String.valueOf(c);
238
+ }
239
+ writer.write(s);
240
+ }
241
+ }
242
+ }