Skip to content

Commit

Permalink
Added connection string parser, FQN generation and Lineage Recording …
Browse files Browse the repository at this point in the history
…for Generic DB plugin
  • Loading branch information
apoorvamk committed Sep 27, 2022
1 parent 14a90c2 commit 9c78842
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.cdap.plugin.DBRecord;
import io.cdap.plugin.FieldCase;
import io.cdap.plugin.StructuredRecordUtils;
import io.cdap.plugin.common.Asset;
import io.cdap.plugin.common.LineageRecorder;
import io.cdap.plugin.common.ReferenceBatchSource;
import io.cdap.plugin.common.ReferencePluginConfig;
Expand All @@ -49,6 +50,7 @@
import io.cdap.plugin.common.db.DriverCleanup;
import io.cdap.plugin.db.batch.TransactionIsolationLevel;
import io.cdap.plugin.db.common.DBBaseConfig;
import io.cdap.plugin.db.common.DBURLParser;
import io.cdap.plugin.db.connector.DBConnector;
import io.cdap.plugin.db.connector.DBConnectorConfig;
import org.apache.hadoop.conf.Configuration;
Expand All @@ -59,6 +61,7 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
Expand Down Expand Up @@ -270,6 +273,15 @@ private Connection getConnection() throws SQLException {
return DriverManager.getConnection(sourceConfig.getConnectionString(), properties);
}

protected LineageRecorder getLineageRecorder(BatchSourceContext context) {
// dbtype, host, port, db from the connection string
// table is the reference name
URI uri = DBURLParser.parseURL(sourceConfig.getConnectionString());
String fqn = DBURLParser.constructFQN(uri, sourceConfig.getReferenceName());
Asset asset = Asset.builder(sourceConfig.getReferenceName()).setFqn(fqn).build();
return new LineageRecorder(context, asset);
}

/**
* {@link PluginConfig} for {@link DBSource}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright © 2022 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package io.cdap.plugin.db.common;

import java.net.URI;

/**
* URL Parser for DB
*/

public class DBURLParser {
public static URI parseURL(String connectionString) {
// Remove the 'jdbc:' prefix from the connection string
String cleanURI = connectionString.substring(5);
URI uri = URI.create(cleanURI);
return uri;
}

public static String constructFQN(URI uri, String tableName) {
if (uri.getScheme() == "postgres") {
return "";
} else {
return String.format("%s://%s:%s/%s/%s", uri.getScheme(), uri.getHost(), uri.getPort(), uri.getPath(), tableName);
}
}
}

0 comments on commit 9c78842

Please sign in to comment.