Skip to content

Commit 3048e75

Browse files
committed
Added connection string parser, FQN generation and Lineage Recording for Generic DB plugin
1 parent 14a90c2 commit 3048e75

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

database-plugins/src/main/java/io/cdap/plugin/db/batch/source/DBSource.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import io.cdap.plugin.DBRecord;
4242
import io.cdap.plugin.FieldCase;
4343
import io.cdap.plugin.StructuredRecordUtils;
44+
import io.cdap.plugin.common.Asset;
4445
import io.cdap.plugin.common.LineageRecorder;
4546
import io.cdap.plugin.common.ReferenceBatchSource;
4647
import io.cdap.plugin.common.ReferencePluginConfig;
@@ -49,6 +50,7 @@
4950
import io.cdap.plugin.common.db.DriverCleanup;
5051
import io.cdap.plugin.db.batch.TransactionIsolationLevel;
5152
import io.cdap.plugin.db.common.DBBaseConfig;
53+
import io.cdap.plugin.db.common.DBURLParser;
5254
import io.cdap.plugin.db.connector.DBConnector;
5355
import io.cdap.plugin.db.connector.DBConnectorConfig;
5456
import org.apache.hadoop.conf.Configuration;
@@ -59,6 +61,7 @@
5961
import org.slf4j.LoggerFactory;
6062

6163
import java.io.IOException;
64+
import java.net.URI;
6265
import java.sql.Connection;
6366
import java.sql.Driver;
6467
import java.sql.DriverManager;
@@ -270,6 +273,15 @@ private Connection getConnection() throws SQLException {
270273
return DriverManager.getConnection(sourceConfig.getConnectionString(), properties);
271274
}
272275

276+
protected LineageRecorder getLineageRecorder(BatchSourceContext context) {
277+
// dbtype, host, port, db from the connection string
278+
// table is the reference name
279+
URI uri = DBURLParser.parseURL(sourceConfig.getConnectionString());
280+
String fqn = DBURLParser.constructFQN(uri, sourceConfig.getReferenceName());
281+
Asset asset = Asset.builder(sourceConfig.getReferenceName()).setFqn(fqn).build();
282+
return new LineageRecorder(context, asset);
283+
}
284+
273285
/**
274286
* {@link PluginConfig} for {@link DBSource}
275287
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright © 2022 Cask Data, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package io.cdap.plugin.db.common;
18+
19+
import java.net.URI;
20+
21+
/**
22+
* URL Parser for DB
23+
*/
24+
25+
public class DBURLParser {
26+
public static URI parseURL(String connectionString) {
27+
// Remove the 'jdbc:' prefix from the connection string
28+
String cleanURI = connectionString.substring(5);
29+
URI uri = URI.create(cleanURI);
30+
return uri;
31+
}
32+
33+
public static String constructFQN(URI uri, String tableName) {
34+
if (uri.getScheme() == "postgres") {
35+
return "";
36+
} else {
37+
return String.format("%s://%s:%s/%s/%s", uri.getScheme(), uri.getHost(), uri.getPort(), uri.getPath(), tableName);
38+
}
39+
}
40+
}

google-cloud

Submodule google-cloud updated 104 files

0 commit comments

Comments
 (0)