1717package com .google .edwmigration .dumper .application .dumper .connector .redshift ;
1818
1919import com .google .common .base .Joiner ;
20- import com .google .common .collect .Iterables ;
2120import com .google .edwmigration .dumper .application .dumper .ConnectorArguments ;
22- import com .google .edwmigration .dumper .application .dumper .MetadataDumperUsageException ;
2321import com .google .edwmigration .dumper .application .dumper .annotations .RespectsArgumentDatabaseForConnection ;
2422import com .google .edwmigration .dumper .application .dumper .annotations .RespectsArgumentDriver ;
2523import com .google .edwmigration .dumper .application .dumper .annotations .RespectsArgumentHostUnlessUrl ;
2927import com .google .edwmigration .dumper .application .dumper .connector .AbstractJdbcConnector ;
3028import com .google .edwmigration .dumper .application .dumper .handle .Handle ;
3129import com .google .edwmigration .dumper .application .dumper .handle .JdbcHandle ;
32- import java .io .UnsupportedEncodingException ;
3330import java .sql .Driver ;
31+ import java .sql .SQLException ;
3432import java .time .ZoneOffset ;
3533import java .time .format .DateTimeFormatter ;
3634import java .util .List ;
6967@ RespectsArgumentJDBCUri
7068public abstract class AbstractRedshiftConnector extends AbstractJdbcConnector {
7169
72- @ SuppressWarnings ("UnusedVariable" )
7370 private static final Logger LOG = LoggerFactory .getLogger (AbstractRedshiftConnector .class );
7471
7572 protected static final DateTimeFormatter SQL_FORMAT =
7673 DateTimeFormatter .ISO_OFFSET_DATE_TIME .withZone (ZoneOffset .UTC );
77- public static final int OPT_PORT_DEFAULT = 5439 ;
7874
7975 @ Nonnull
8076 protected static CharSequence newWhereClause (List <String > clauseList , String ... clauseArray ) {
@@ -130,85 +126,15 @@ protected static CharSequence newWhereClause(List<String> clauseList, String...
130126 *
131127 * https://s3.amazonaws.com/redshift-downloads/drivers/jdbc/1.2.43.1067/Amazon+Redshift+JDBC+Driver+Install+Guide.pdf
132128 */
133- // this SHOuLD LAND UP IN THE ARGUMENT CLASS ...
134- @ Nonnull
135- private static String requireNonNull (String val , String msg ) throws MetadataDumperUsageException {
136- if (val != null ) return val ;
137- throw new MetadataDumperUsageException (msg );
138- }
139-
140- @ Nonnull
141- private String makeJdbcUrlPostgresql (ConnectorArguments arguments )
142- throws MetadataDumperUsageException , UnsupportedEncodingException {
143- String password = arguments .getPasswordIfFlagProvided ().orElse (null );
144- return "jdbc:postgresql://"
145- + arguments .getHostOrDefault ()
146- + ":"
147- + arguments .getPort (5439 )
148- + "/"
149- + Iterables .getFirst (arguments .getDatabases (), "" ) //
150- + new JdbcPropBuilder ("?=&" )
151- .propOrWarn ("user" , arguments .getUser (), "--user must be specified" )
152- .propOrWarn ("password" , password , "--password must be specified" )
153- .prop ("ssl" , "true" )
154- .toJdbcPart ();
155- }
156-
157- @ Nonnull
158- private String makeJdbcUrlRedshiftSimple (ConnectorArguments arguments )
159- throws MetadataDumperUsageException , UnsupportedEncodingException {
160- String password = arguments .getPasswordIfFlagProvided ().orElse (null );
161- return "jdbc:redshift://"
162- + arguments .getHostOrDefault ()
163- + ":"
164- + arguments .getPort (5439 )
165- + "/"
166- + Iterables .getFirst (arguments .getDatabases (), "" ) //
167- + new JdbcPropBuilder ("?=&" )
168- .propOrWarn ("UID" , arguments .getUser (), "--user must be specified" )
169- .propOrWarn ("PWD" , password , "--password must be specified" )
170- .toJdbcPart ();
171- }
172-
173- // TODO: [cluster-id]:[region] syntax.
174- // either profile, or key+ secret
175- @ Nonnull
176- private String makeJdbcUrlRedshiftIAM (ConnectorArguments arguments )
177- throws MetadataDumperUsageException , UnsupportedEncodingException {
178- String url =
179- "jdbc:redshift:iam://"
180- + arguments .getHostOrDefault ()
181- + ":"
182- + arguments .getPort (5439 )
183- + "/"
184- + Iterables .getFirst (arguments .getDatabases (), "" );
185-
186- if (arguments .getIAMProfile () != null )
187- url += new JdbcPropBuilder ("?=&" ).prop ("Profile" , arguments .getIAMProfile ()).toJdbcPart ();
188- else if (arguments .getIAMAccessKeyID () != null && arguments .getIAMSecretAccessKey () != null )
189- url +=
190- new JdbcPropBuilder ("?=&" )
191- .prop ("AccessKeyID" , arguments .getIAMAccessKeyID ())
192- .prop ("SecretAccessKey" , arguments .getIAMSecretAccessKey ())
193- .propOrError ("DbUser" , arguments .getUser (), "--user must be specified" )
194- .toJdbcPart ();
195- // Will use the default IAM from ~/.aws/credentials/
196- // throw new MetadataDumperUsageException("Either --iam-profile or
197- // --iam-accesskeyid/--iam-secretaccesskey should be given");
198- return url ;
199- }
200129
201130 @ Override
202- public Handle open (ConnectorArguments arguments ) throws Exception {
131+ @ Nonnull
132+ public Handle open (@ Nonnull ConnectorArguments arguments ) throws Exception {
203133
204134 Driver driver =
205135 newDriver (
206136 arguments .getDriverPaths (), "com.amazon.redshift.jdbc.Driver" , "org.postgresql.Driver" );
207137
208- // LOG.debug("DRIVER IS " + driver.getClass().getCanonicalName());
209- // LOG.debug("DRIVER CAN RS " + driver.acceptsURL("jdbc:redshift://host/db"));
210- // LOG.debug("DRIVER CAN IAM " + driver.acceptsURL("jdbc:redshift:iam://host/db"));
211- // LOG.debug("DRIVER CAN PG " + driver.acceptsURL("jdbc:postgresql://host/db"));
212138 String url = arguments .getUri ();
213139 Optional <String > password = arguments .getPasswordIfFlagProvided ();
214140 if (url == null ) {
@@ -226,11 +152,11 @@ public Handle open(ConnectorArguments arguments) throws Exception {
226152 "The use of IAM authentication also requires the use of a Redshift-specific JDBC driver. Please use --"
227153 + ConnectorArguments .OPT_DRIVER
228154 + " to specify the path to the Redshift JDBC JAR, or use password authentication." );
229- url = makeJdbcUrlPostgresql (arguments );
155+ url = RedshiftUrlUtil . makeJdbcUrlPostgresql (arguments );
230156 } else if (isAuthenticationPassword ) {
231- url = makeJdbcUrlRedshiftSimple (arguments );
157+ url = RedshiftUrlUtil . makeJdbcUrlRedshiftSimple (arguments );
232158 } else {
233- url = makeJdbcUrlRedshiftIAM (arguments );
159+ url = RedshiftUrlUtil . makeJdbcUrlRedshiftIAM (arguments );
234160 }
235161 }
236162
@@ -243,4 +169,11 @@ public Handle open(ConnectorArguments arguments) throws Exception {
243169
244170 return JdbcHandle .newPooledJdbcHandle (dataSource , arguments .getThreadPoolSize ());
245171 }
172+
173+ private static void logDriverInfo (@ Nonnull Driver driver ) throws SQLException {
174+ LOG .debug ("DRIVER IS " + driver .getClass ().getCanonicalName ());
175+ LOG .debug ("DRIVER CAN RS " + driver .acceptsURL ("jdbc:redshift://host/db" ));
176+ LOG .debug ("DRIVER CAN IAM " + driver .acceptsURL ("jdbc:redshift:iam://host/db" ));
177+ LOG .debug ("DRIVER CAN PG " + driver .acceptsURL ("jdbc:postgresql://host/db" ));
178+ }
246179}
0 commit comments