embulk-output-redshift 0.7.0 → 0.7.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0360975892302617a8f7098d1689d40e732dd4d
4
- data.tar.gz: 1f1848b67129240480e2653b1f219a50a769ada4
3
+ metadata.gz: 47ca8a15eaed456d7a006c1dfdb43b2ae85b9013
4
+ data.tar.gz: 1fa61b84e89e32e81c7fb748d8d2edcbb4bf4481
5
5
  SHA512:
6
- metadata.gz: fb8e9fb240aa369a79e6ba0785c67bbc350ef0edb15fbceeb4d81b5ce4d00b9b184f40bb6865005f5ca36db859b9bc60d433b2804fe37c65832474787951f315
7
- data.tar.gz: 137a53934742fcf0511a83ebcf785edb3fc3c1c8dcf397fbf927262f7188f25ce52cc45ab397f7a9cd3976b04b8270a338088f7d3d37e8d9ad67ae7d825bd7dc
6
+ metadata.gz: b9652025ca46d67558454940362e804477d7cfa1a93bd5f19accc8a42b6bbdb11f2930bf514398b51c3bd68d9053e6258aa80badff2dd0b01329af25de6d1f12
7
+ data.tar.gz: 503904556c128353ad4ef366f7b0ed31300a2f9ea5c93eb0688c418a7c08a266d2ed48e65e6a93dce4fcb3f78cfb78e29e196b2241563a949a8863100a70bb34
data/README.md CHANGED
@@ -36,7 +36,7 @@ Redshift output plugin for Embulk loads records to Redshift.
36
36
  - **value_type**: This plugin converts input column type (embulk type) into a database type to build a INSERT statement. This value_type option controls the type of the value in a INSERT statement. (string, default: depends on the sql type of the column. Available values options are: `byte`, `short`, `int`, `long`, `double`, `float`, `boolean`, `string`, `nstring`, `date`, `time`, `timestamp`, `decimal`, `json`, `null`, `pass`)
37
37
  - **timestamp_format**: If input column type (embulk type) is timestamp and value_type is `string` or `nstring`, this plugin needs to format the timestamp value into a string. This timestamp_format option is used to control the format of the timestamp. (string, default: `%Y-%m-%d %H:%M:%S.%6N`)
38
38
  - **timezone**: If input column type (embulk type) is timestamp, this plugin needs to format the timestamp value into a SQL string. In this cases, this timezone option is used to control the timezone. (string, value of default_timezone option is used by default)
39
-
39
+ - **after_load**: if set, this SQL will be executed after loading all records.
40
40
 
41
41
  ### Modes
42
42
 
@@ -1,16 +1,19 @@
1
1
  package org.embulk.output.redshift;
2
2
 
3
- import java.util.List;
4
3
  import java.sql.Connection;
5
4
  import java.sql.SQLException;
6
5
  import java.sql.Statement;
6
+ import java.util.ArrayList;
7
+ import java.util.List;
7
8
 
8
- import org.embulk.output.jdbc.MergeConfig;
9
- import org.slf4j.Logger;
10
- import org.embulk.spi.Exec;
11
- import org.embulk.output.jdbc.JdbcOutputConnection;
12
9
  import org.embulk.output.jdbc.JdbcColumn;
10
+ import org.embulk.output.jdbc.JdbcOutputConnection;
13
11
  import org.embulk.output.jdbc.JdbcSchema;
12
+ import org.embulk.output.jdbc.MergeConfig;
13
+ import org.embulk.spi.Exec;
14
+ import org.slf4j.Logger;
15
+
16
+ import com.google.common.base.Optional;
14
17
 
15
18
  public class RedshiftOutputConnection
16
19
  extends JdbcOutputConnection
@@ -46,7 +49,7 @@ public class RedshiftOutputConnection
46
49
  // Redshift does not support DROP TABLE IF EXISTS.
47
50
  // Dropping part runs DROP TABLE and ignores errors.
48
51
  @Override
49
- public void replaceTable(String fromTable, JdbcSchema schema, String toTable) throws SQLException
52
+ public void replaceTable(String fromTable, JdbcSchema schema, String toTable, Optional<String> additionalSql) throws SQLException
50
53
  {
51
54
  Statement stmt = connection.createStatement();
52
55
  try {
@@ -73,6 +76,10 @@ public class RedshiftOutputConnection
73
76
  executeUpdate(stmt, sql);
74
77
  }
75
78
 
79
+ if (additionalSql.isPresent()) {
80
+ executeUpdate(stmt, additionalSql.get());
81
+ }
82
+
76
83
  commitIfNecessary(connection);
77
84
  } catch (SQLException ex) {
78
85
  throw safeRollback(connection, ex);
@@ -128,18 +135,41 @@ public class RedshiftOutputConnection
128
135
  {
129
136
  StringBuilder sb = new StringBuilder();
130
137
 
138
+ List<String> mergeKeys = mergeConfig.getMergeKeys();
139
+
140
+ List<String> updateKeys = new ArrayList<String>();
141
+ for (int i = 0; i < schema.getCount(); i++) {
142
+ String updateKey = schema.getColumnName(i);
143
+ if (!mergeKeys.contains(updateKey)) {
144
+ updateKeys.add(updateKey);
145
+ }
146
+ }
147
+
131
148
  sb.append("BEGIN TRANSACTION;");
132
149
 
133
- sb.append("DELETE FROM ");
150
+ sb.append("UPDATE ");
134
151
  quoteIdentifierString(sb, toTable);
135
- sb.append(" USING (");
152
+ sb.append(" SET ");
153
+ for (int i = 0; i < updateKeys.size(); i++) {
154
+ if (i != 0) { sb.append(", "); }
155
+ quoteIdentifierString(sb, updateKeys.get(i).toString());
156
+ sb.append(" = ");
157
+ sb.append("S.");
158
+ quoteIdentifierString(sb, updateKeys.get(i).toString());
159
+ }
160
+ sb.append(" FROM ( ");
136
161
  for (int i = 0; i < fromTables.size(); i++) {
137
162
  if (i != 0) { sb.append(" UNION ALL "); }
138
- sb.append("SELECT * FROM ");
163
+ sb.append(" SELECT ");
164
+ for (int j = 0; j < schema.getCount(); j++) {
165
+ if (j != 0) { sb.append(", "); }
166
+ quoteIdentifierString(sb, schema.getColumnName(j));
167
+ }
168
+ sb.append(" FROM ");
139
169
  quoteIdentifierString(sb, fromTables.get(i));
140
170
  }
141
- sb.append(") S WHERE (");
142
- List<String> mergeKeys = mergeConfig.getMergeKeys();
171
+ sb.append(" ) S WHERE ");
172
+
143
173
  for (int i = 0; i < mergeKeys.size(); i++) {
144
174
  if (i != 0) { sb.append(" AND "); }
145
175
  sb.append("S.");
@@ -149,7 +179,7 @@ public class RedshiftOutputConnection
149
179
  sb.append(".");
150
180
  quoteIdentifierString(sb, mergeKeys.get(i));
151
181
  }
152
- sb.append(");");
182
+ sb.append(";");
153
183
 
154
184
  sb.append("INSERT INTO ");
155
185
  quoteIdentifierString(sb, toTable);
@@ -160,7 +190,7 @@ public class RedshiftOutputConnection
160
190
  }
161
191
  sb.append(") (");
162
192
  for (int i = 0; i < fromTables.size(); i++) {
163
- if (i != 0) { sb.append(" UNION ALL "); }
193
+ if (i != 0) { sb.append(" UNION ALL ("); }
164
194
  sb.append("SELECT ");
165
195
  for (int j = 0; j < schema.getCount(); j++) {
166
196
  if (j != 0) { sb.append(", "); }
@@ -168,12 +198,28 @@ public class RedshiftOutputConnection
168
198
  }
169
199
  sb.append(" FROM ");
170
200
  quoteIdentifierString(sb, fromTables.get(i));
201
+ sb.append(" WHERE NOT EXISTS (SELECT 1 FROM ");
202
+ quoteIdentifierString(sb, toTable);
203
+ sb.append(" WHERE ");
204
+
205
+ for (int k = 0; k < mergeKeys.size(); k++) {
206
+ if (k != 0) { sb.append(" AND "); }
207
+ quoteIdentifierString(sb, fromTables.get(i));
208
+ sb.append(".");
209
+ quoteIdentifierString(sb, mergeKeys.get(k));
210
+ sb.append(" = ");
211
+ quoteIdentifierString(sb, toTable);
212
+ sb.append(".");
213
+ quoteIdentifierString(sb, mergeKeys.get(k));
214
+ }
215
+ sb.append(")) ");
171
216
  }
172
- sb.append(");");
217
+ sb.append(";");
173
218
 
174
219
  sb.append("END TRANSACTION;");
175
220
 
176
221
  return sb.toString();
222
+
177
223
  }
178
224
 
179
- }
225
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-output-redshift
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sadayuki Furuhashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-26 00:00:00.000000000 Z
11
+ date: 2016-11-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Inserts or updates records to a table.
14
14
  email:
@@ -25,9 +25,9 @@ files:
25
25
  - classpath/aws-java-sdk-sts-1.10.33.jar
26
26
  - classpath/commons-codec-1.6.jar
27
27
  - classpath/commons-logging-1.1.3.jar
28
- - classpath/embulk-output-jdbc-0.7.0.jar
29
- - classpath/embulk-output-postgresql-0.7.0.jar
30
- - classpath/embulk-output-redshift-0.7.0.jar
28
+ - classpath/embulk-output-jdbc-0.7.1.jar
29
+ - classpath/embulk-output-postgresql-0.7.1.jar
30
+ - classpath/embulk-output-redshift-0.7.1.jar
31
31
  - classpath/httpclient-4.3.6.jar
32
32
  - classpath/httpcore-4.3.3.jar
33
33
  - classpath/postgresql-9.4-1205-jdbc41.jar