@@ -133,10 +133,8 @@ public Schema getSchema(Class<? extends Driver> driverClass) throws IllegalAcces
133133 try {
134134
135135 driverCleanup = loadPluginClassAndGetDriver (driverClass );
136- try (Connection connection = getConnection ()) {
137- executeInitQueries (connection , sourceConfig .getInitQueries ());
138- String query = sourceConfig .getImportQuery ();
139- return loadSchemaFromDB (connection , query );
136+ try {
137+ return getSchema ();
140138 } finally {
141139 driverCleanup .destroy ();
142140 }
@@ -146,6 +144,23 @@ public Schema getSchema(Class<? extends Driver> driverClass) throws IllegalAcces
146144 }
147145 }
148146
147+ /**
148+ * Returns the schema of the importQuery from the database using the SourceConfig details.
149+ *
150+ * @return Schema instance
151+ * @throws SQLException In case of
152+ * 1. Error while creating connection to the database.
153+ * 2. Error while running any init queries.
154+ * 3. Error while running the import query.
155+ */
156+ public Schema getSchema () throws SQLException {
157+ try (Connection connection = getConnection ()) {
158+ executeInitQueries (connection , sourceConfig .getInitQueries ());
159+ String query = sourceConfig .getImportQuery ();
160+ return loadSchemaFromDB (connection , query );
161+ }
162+ }
163+
149164 private Schema loadSchemaFromDB (Connection connection , String query ) throws SQLException {
150165 Statement statement = connection .createStatement ();
151166 statement .setMaxRows (1 );
@@ -229,22 +244,56 @@ public void prepareRun(BatchSourceContext context) throws Exception {
229244 sourceConfig .validate (collector );
230245 collector .getOrThrowException ();
231246
232- String connectionString = sourceConfig .getConnectionString ();
233-
234247 LOG .debug ("pluginType = {}; pluginName = {}; connectionString = {}; importQuery = {}; " +
235248 "boundingQuery = {};" ,
236- ConnectionConfig .JDBC_PLUGIN_TYPE , sourceConfig .getJdbcPluginName (),
237- connectionString ,
238- sourceConfig .getImportQuery (), sourceConfig .getBoundingQuery ());
239- ConnectionConfigAccessor connectionConfigAccessor = new ConnectionConfigAccessor ();
249+ ConnectionConfig .JDBC_PLUGIN_TYPE ,
250+ sourceConfig .getJdbcPluginName (),
251+ sourceConfig .getConnectionString (),
252+ sourceConfig .getImportQuery (),
253+ sourceConfig .getBoundingQuery ());
240254
241255 // Load the plugin class to make sure it is available.
242256 Class <? extends Driver > driverClass = context .loadPluginClass (getJDBCPluginId ());
257+ Schema schemaFromDB = loadSchemaFromDB (driverClass );
258+
259+ ConnectionConfigAccessor connectionConfigAccessor = getConnectionConfigAccessor (
260+ driverClass .getName (),
261+ schemaFromDB ,
262+ collector );
263+
264+ LineageRecorder lineageRecorder = getLineageRecorder (context );
265+ Schema schema = sourceConfig .getSchema () == null ? schemaFromDB : sourceConfig .getSchema ();
266+ lineageRecorder .createExternalDataset (schema );
267+ if (schema != null && schema .getFields () != null ) {
268+ lineageRecorder .recordRead ("Read" , "Read from database plugin" ,
269+ schema .getFields ().stream ().map (Schema .Field ::getName ).collect (Collectors .toList ()));
270+ }
271+ context .setInput (Input .of (sourceConfig .getReferenceName (), new SourceInputFormatProvider (
272+ DataDrivenETLDBInputFormat .class , connectionConfigAccessor .getConfiguration ())));
273+ }
274+
275+ /**
276+ * Returns the ConnectionConfigAccessor object containing the Configuration object for the SourceConfig
277+ * and Schema. The configuration is later used by the InputFormat object for split calculation, reader creation.
278+ *
279+ * @param driverClassName Class name of the driver in use
280+ * @param schemaFromDB Schema object
281+ * @param collector Failure Collector object
282+ * @return ConnectionConfigAccessor instance
283+ * @throws IOException
284+ */
285+ public ConnectionConfigAccessor getConnectionConfigAccessor (String driverClassName ,
286+ Schema schemaFromDB ,
287+ FailureCollector collector ) throws IOException {
288+ ConnectionConfigAccessor connectionConfigAccessor = new ConnectionConfigAccessor ();
289+
243290 if (sourceConfig .getUser () == null && sourceConfig .getPassword () == null ) {
244- DBConfiguration .configureDB (connectionConfigAccessor .getConfiguration (), driverClass .getName (), connectionString );
291+ DBConfiguration .configureDB (connectionConfigAccessor .getConfiguration (),
292+ driverClassName , sourceConfig .getConnectionString ());
245293 } else {
246- DBConfiguration .configureDB (connectionConfigAccessor .getConfiguration (), driverClass .getName (), connectionString ,
247- sourceConfig .getUser (), sourceConfig .getPassword ());
294+ DBConfiguration .configureDB (connectionConfigAccessor .getConfiguration (),
295+ driverClassName , sourceConfig .getConnectionString (),
296+ sourceConfig .getUser (), sourceConfig .getPassword ());
248297 }
249298
250299 if (sourceConfig .getFetchSize () != null ) {
@@ -255,7 +304,6 @@ public void prepareRun(BatchSourceContext context) throws Exception {
255304 sourceConfig .getImportQuery (), sourceConfig .getBoundingQuery (),
256305 false );
257306
258-
259307 if (sourceConfig .getTransactionIsolationLevel () != null ) {
260308 connectionConfigAccessor .setTransactionIsolationLevel (sourceConfig .getTransactionIsolationLevel ());
261309 }
@@ -273,7 +321,6 @@ public void prepareRun(BatchSourceContext context) throws Exception {
273321 connectionConfigAccessor .getConfiguration ().setInt (MRJobConfig .NUM_MAPS , sourceConfig .getNumSplits ());
274322 }
275323
276- Schema schemaFromDB = loadSchemaFromDB (driverClass );
277324 if (sourceConfig .getSchema () != null ) {
278325 sourceConfig .validateSchema (schemaFromDB , collector );
279326 collector .getOrThrowException ();
@@ -283,15 +330,7 @@ public void prepareRun(BatchSourceContext context) throws Exception {
283330 connectionConfigAccessor .setSchema (schemaStr );
284331 }
285332
286- LineageRecorder lineageRecorder = getLineageRecorder (context );
287- Schema schema = sourceConfig .getSchema () == null ? schemaFromDB : sourceConfig .getSchema ();
288- lineageRecorder .createExternalDataset (schema );
289- if (schema != null && schema .getFields () != null ) {
290- lineageRecorder .recordRead ("Read" , "Read from database plugin" ,
291- schema .getFields ().stream ().map (Schema .Field ::getName ).collect (Collectors .toList ()));
292- }
293- context .setInput (Input .of (sourceConfig .getReferenceName (), new SourceInputFormatProvider (
294- DataDrivenETLDBInputFormat .class , connectionConfigAccessor .getConfiguration ())));
333+ return connectionConfigAccessor ;
295334 }
296335
297336 protected LineageRecorder getLineageRecorder (BatchSourceContext context ) {
0 commit comments