embulk-output-postgresql 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +90 -89
- data/build.gradle +7 -7
- data/classpath/embulk-output-jdbc-0.4.2.jar +0 -0
- data/classpath/embulk-output-postgresql-0.4.2.jar +0 -0
- data/classpath/postgresql-9.4-1205-jdbc41.jar +0 -0
- data/lib/embulk/output/postgresql.rb +3 -3
- data/src/main/java/org/embulk/output/PostgreSQLOutputPlugin.java +166 -165
- data/src/main/java/org/embulk/output/postgresql/AbstractPostgreSQLCopyBatchInsert.java +242 -245
- data/src/main/java/org/embulk/output/postgresql/PostgreSQLCopyBatchInsert.java +71 -73
- data/src/main/java/org/embulk/output/postgresql/PostgreSQLOutputConnection.java +156 -157
- data/src/main/java/org/embulk/output/postgresql/PostgreSQLOutputConnector.java +39 -40
- metadata +5 -9
- data/classpath/embulk-output-jdbc-0.4.1.jar +0 -0
- data/classpath/embulk-output-postgresql-0.4.1.jar +0 -0
- data/classpath/jna-4.1.0.jar +0 -0
- data/classpath/jna-platform-4.1.0.jar +0 -0
- data/classpath/postgresql-9.4-1200-jdbc41.jar +0 -0
- data/classpath/slf4j-simple-1.7.7.jar +0 -0
- data/classpath/waffle-jna-1.7.jar +0 -0
@@ -1,73 +1,71 @@
|
|
1
|
-
package org.embulk.output.postgresql;
|
2
|
-
|
3
|
-
import java.io.File;
|
4
|
-
import java.io.IOException;
|
5
|
-
import java.io.FileInputStream;
|
6
|
-
import java.sql.
|
7
|
-
import
|
8
|
-
import org.
|
9
|
-
import org.
|
10
|
-
import org.
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
private
|
21
|
-
private
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
this.
|
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
|
-
}
|
1
|
+
package org.embulk.output.postgresql;
|
2
|
+
|
3
|
+
import java.io.File;
|
4
|
+
import java.io.IOException;
|
5
|
+
import java.io.FileInputStream;
|
6
|
+
import java.sql.SQLException;
|
7
|
+
import org.slf4j.Logger;
|
8
|
+
import org.postgresql.copy.CopyManager;
|
9
|
+
import org.embulk.spi.Exec;
|
10
|
+
import org.embulk.output.jdbc.JdbcSchema;
|
11
|
+
|
12
|
+
public class PostgreSQLCopyBatchInsert
|
13
|
+
extends AbstractPostgreSQLCopyBatchInsert
|
14
|
+
{
|
15
|
+
private final Logger logger = Exec.getLogger(PostgreSQLCopyBatchInsert.class);
|
16
|
+
private final PostgreSQLOutputConnector connector;
|
17
|
+
|
18
|
+
private PostgreSQLOutputConnection connection = null;
|
19
|
+
private CopyManager copyManager = null;
|
20
|
+
private String copySql = null;
|
21
|
+
private long totalRows;
|
22
|
+
|
23
|
+
public PostgreSQLCopyBatchInsert(PostgreSQLOutputConnector connector) throws IOException, SQLException
|
24
|
+
{
|
25
|
+
super();
|
26
|
+
this.connector = connector;
|
27
|
+
}
|
28
|
+
|
29
|
+
@Override
|
30
|
+
public void prepare(String loadTable, JdbcSchema insertSchema) throws SQLException
|
31
|
+
{
|
32
|
+
this.connection = connector.connect(true);
|
33
|
+
this.copySql = connection.buildCopySql(loadTable, insertSchema);
|
34
|
+
this.copyManager = connection.newCopyManager();
|
35
|
+
logger.info("Copy SQL: "+copySql);
|
36
|
+
}
|
37
|
+
|
38
|
+
@Override
|
39
|
+
public void flush() throws IOException, SQLException
|
40
|
+
{
|
41
|
+
File file = closeCurrentFile(); // flush buffered data in writer
|
42
|
+
|
43
|
+
logger.info(String.format("Loading %,d rows (%,d bytes)", batchRows, file.length()));
|
44
|
+
long startTime = System.currentTimeMillis();
|
45
|
+
FileInputStream in = new FileInputStream(file);
|
46
|
+
try {
|
47
|
+
// TODO check age of connection and call isValid if it's old and reconnect if it's invalid
|
48
|
+
copyManager.copyIn(copySql, in);
|
49
|
+
} finally {
|
50
|
+
in.close();
|
51
|
+
}
|
52
|
+
double seconds = (System.currentTimeMillis() - startTime) / 1000.0;
|
53
|
+
|
54
|
+
totalRows += batchRows;
|
55
|
+
batchRows = 0;
|
56
|
+
logger.info(String.format("> %.2f seconds (loaded %,d rows in total)", seconds, totalRows));
|
57
|
+
|
58
|
+
openNewFile();
|
59
|
+
file.delete();
|
60
|
+
}
|
61
|
+
|
62
|
+
@Override
|
63
|
+
public void close() throws IOException, SQLException
|
64
|
+
{
|
65
|
+
closeCurrentFile().delete();
|
66
|
+
if (connection != null) {
|
67
|
+
connection.close();
|
68
|
+
connection = null;
|
69
|
+
}
|
70
|
+
}
|
71
|
+
}
|
@@ -1,157 +1,156 @@
|
|
1
|
-
package org.embulk.output.postgresql;
|
2
|
-
|
3
|
-
import java.util.List;
|
4
|
-
import java.sql.Connection;
|
5
|
-
import java.sql.SQLException;
|
6
|
-
import java.sql.Statement;
|
7
|
-
import org.postgresql.copy.CopyManager;
|
8
|
-
import org.postgresql.core.BaseConnection;
|
9
|
-
import org.embulk.
|
10
|
-
import org.embulk.output.jdbc.
|
11
|
-
import org.embulk.output.jdbc.
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
sb
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
sb.append("
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
sb.append("
|
52
|
-
sb
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
sb.
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
sb.
|
70
|
-
|
71
|
-
|
72
|
-
sb.append("
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
sb.
|
78
|
-
|
79
|
-
sb.append("
|
80
|
-
sb.
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
sb.
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
sb
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
sb.append("
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
sb.
|
113
|
-
|
114
|
-
|
115
|
-
sb.append("
|
116
|
-
sb.append("
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
sb.
|
121
|
-
|
122
|
-
sb.append("
|
123
|
-
sb.
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
}
|
1
|
+
package org.embulk.output.postgresql;
|
2
|
+
|
3
|
+
import java.util.List;
|
4
|
+
import java.sql.Connection;
|
5
|
+
import java.sql.SQLException;
|
6
|
+
import java.sql.Statement;
|
7
|
+
import org.postgresql.copy.CopyManager;
|
8
|
+
import org.postgresql.core.BaseConnection;
|
9
|
+
import org.embulk.output.jdbc.JdbcOutputConnection;
|
10
|
+
import org.embulk.output.jdbc.JdbcColumn;
|
11
|
+
import org.embulk.output.jdbc.JdbcSchema;
|
12
|
+
|
13
|
+
public class PostgreSQLOutputConnection
|
14
|
+
extends JdbcOutputConnection
|
15
|
+
{
|
16
|
+
public PostgreSQLOutputConnection(Connection connection, String schemaName, boolean autoCommit)
|
17
|
+
throws SQLException
|
18
|
+
{
|
19
|
+
super(connection, schemaName);
|
20
|
+
connection.setAutoCommit(autoCommit);
|
21
|
+
}
|
22
|
+
|
23
|
+
public String buildCopySql(String toTable, JdbcSchema toTableSchema)
|
24
|
+
{
|
25
|
+
StringBuilder sb = new StringBuilder();
|
26
|
+
|
27
|
+
sb.append("COPY ");
|
28
|
+
quoteIdentifierString(sb, toTable);
|
29
|
+
sb.append(" (");
|
30
|
+
for (int i=0; i < toTableSchema.getCount(); i++) {
|
31
|
+
if (i != 0) { sb.append(", "); }
|
32
|
+
quoteIdentifierString(sb, toTableSchema.getColumnName(i));
|
33
|
+
}
|
34
|
+
sb.append(") ");
|
35
|
+
sb.append("FROM STDIN");
|
36
|
+
|
37
|
+
return sb.toString();
|
38
|
+
}
|
39
|
+
|
40
|
+
public CopyManager newCopyManager() throws SQLException
|
41
|
+
{
|
42
|
+
return new CopyManager((BaseConnection) connection);
|
43
|
+
}
|
44
|
+
|
45
|
+
@Override
|
46
|
+
protected String buildCollectMergeSql(List<String> fromTables, JdbcSchema schema, String toTable, List<String> mergeKeys) throws SQLException
|
47
|
+
{
|
48
|
+
StringBuilder sb = new StringBuilder();
|
49
|
+
|
50
|
+
sb.append("WITH updated AS (");
|
51
|
+
sb.append("UPDATE ");
|
52
|
+
quoteIdentifierString(sb, toTable);
|
53
|
+
sb.append(" SET ");
|
54
|
+
for (int i=0; i < schema.getCount(); i++) {
|
55
|
+
if (i != 0) { sb.append(", "); }
|
56
|
+
quoteIdentifierString(sb, schema.getColumnName(i));
|
57
|
+
sb.append(" = S.");
|
58
|
+
quoteIdentifierString(sb, schema.getColumnName(i));
|
59
|
+
}
|
60
|
+
sb.append(" FROM (");
|
61
|
+
for (int i=0; i < fromTables.size(); i++) {
|
62
|
+
if (i != 0) { sb.append(" UNION ALL "); }
|
63
|
+
sb.append("SELECT ");
|
64
|
+
for(int j=0; j < schema.getCount(); j++) {
|
65
|
+
if (j != 0) { sb.append(", "); }
|
66
|
+
quoteIdentifierString(sb, schema.getColumnName(j));
|
67
|
+
}
|
68
|
+
sb.append(" FROM ");
|
69
|
+
quoteIdentifierString(sb, fromTables.get(i));
|
70
|
+
}
|
71
|
+
sb.append(") S");
|
72
|
+
sb.append(" WHERE ");
|
73
|
+
for (int i=0; i < mergeKeys.size(); i++) {
|
74
|
+
if (i != 0) { sb.append(" AND "); }
|
75
|
+
quoteIdentifierString(sb, toTable);
|
76
|
+
sb.append(".");
|
77
|
+
quoteIdentifierString(sb, mergeKeys.get(i));
|
78
|
+
sb.append(" = ");
|
79
|
+
sb.append("S.");
|
80
|
+
quoteIdentifierString(sb, mergeKeys.get(i));
|
81
|
+
}
|
82
|
+
sb.append(" RETURNING ");
|
83
|
+
for (int i=0; i < mergeKeys.size(); i++) {
|
84
|
+
if (i != 0) { sb.append(", "); }
|
85
|
+
sb.append("S.");
|
86
|
+
quoteIdentifierString(sb, mergeKeys.get(i));
|
87
|
+
}
|
88
|
+
sb.append(") ");
|
89
|
+
|
90
|
+
sb.append("INSERT INTO ");
|
91
|
+
quoteIdentifierString(sb, toTable);
|
92
|
+
sb.append(" (");
|
93
|
+
for (int i=0; i < schema.getCount(); i++) {
|
94
|
+
if (i != 0) { sb.append(", "); }
|
95
|
+
quoteIdentifierString(sb, schema.getColumnName(i));
|
96
|
+
}
|
97
|
+
sb.append(") ");
|
98
|
+
sb.append("SELECT DISTINCT ON (");
|
99
|
+
for (int i=0; i < mergeKeys.size(); i++) {
|
100
|
+
if (i != 0) { sb.append(", "); }
|
101
|
+
quoteIdentifierString(sb, mergeKeys.get(i));
|
102
|
+
}
|
103
|
+
sb.append(") * FROM (");
|
104
|
+
for (int i=0; i < fromTables.size(); i++) {
|
105
|
+
if (i != 0) { sb.append(" UNION ALL "); }
|
106
|
+
sb.append("SELECT ");
|
107
|
+
for(int j=0; j < schema.getCount(); j++) {
|
108
|
+
if (j != 0) { sb.append(", "); }
|
109
|
+
quoteIdentifierString(sb, schema.getColumnName(j));
|
110
|
+
}
|
111
|
+
sb.append(" FROM ");
|
112
|
+
quoteIdentifierString(sb, fromTables.get(i));
|
113
|
+
}
|
114
|
+
sb.append(") S ");
|
115
|
+
sb.append("WHERE NOT EXISTS (");
|
116
|
+
sb.append("SELECT 1 FROM updated WHERE ");
|
117
|
+
for (int i=0; i < mergeKeys.size(); i++) {
|
118
|
+
if (i != 0) { sb.append(" AND "); }
|
119
|
+
sb.append("S.");
|
120
|
+
quoteIdentifierString(sb, mergeKeys.get(i));
|
121
|
+
sb.append(" = ");
|
122
|
+
sb.append("updated.");
|
123
|
+
quoteIdentifierString(sb, mergeKeys.get(i));
|
124
|
+
}
|
125
|
+
sb.append(") ");
|
126
|
+
|
127
|
+
return sb.toString();
|
128
|
+
}
|
129
|
+
|
130
|
+
protected void collectReplaceView(List<String> fromTables, JdbcSchema schema, String toTable) throws SQLException
|
131
|
+
{
|
132
|
+
Statement stmt = connection.createStatement();
|
133
|
+
try {
|
134
|
+
String sql = buildCollectInsertSql(fromTables, schema, toTable);
|
135
|
+
executeUpdate(stmt, sql);
|
136
|
+
commitIfNecessary(connection);
|
137
|
+
} catch (SQLException ex) {
|
138
|
+
throw safeRollback(connection, ex);
|
139
|
+
} finally {
|
140
|
+
stmt.close();
|
141
|
+
}
|
142
|
+
}
|
143
|
+
|
144
|
+
@Override
|
145
|
+
protected String buildColumnTypeName(JdbcColumn c)
|
146
|
+
{
|
147
|
+
switch(c.getSimpleTypeName()) {
|
148
|
+
case "CLOB":
|
149
|
+
return "TEXT";
|
150
|
+
case "BLOB":
|
151
|
+
return "BYTEA";
|
152
|
+
default:
|
153
|
+
return super.buildColumnTypeName(c);
|
154
|
+
}
|
155
|
+
}
|
156
|
+
}
|