|
| 1 | +/* |
| 2 | + * Copyright © 2026 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.databricks; |
| 18 | + |
| 19 | +import io.cdap.cdap.api.annotation.Category; |
| 20 | +import io.cdap.cdap.api.annotation.Description; |
| 21 | +import io.cdap.cdap.api.annotation.Name; |
| 22 | +import io.cdap.cdap.api.annotation.Plugin; |
| 23 | +import io.cdap.cdap.api.data.format.StructuredRecord; |
| 24 | +import io.cdap.cdap.etl.api.batch.BatchSource; |
| 25 | +import io.cdap.cdap.etl.api.connector.Connector; |
| 26 | +import io.cdap.cdap.etl.api.connector.ConnectorSpec; |
| 27 | +import io.cdap.cdap.etl.api.connector.ConnectorSpecRequest; |
| 28 | +import io.cdap.cdap.etl.api.connector.PluginSpec; |
| 29 | +import io.cdap.plugin.common.Constants; |
| 30 | +import io.cdap.plugin.common.ReferenceNames; |
| 31 | +import io.cdap.plugin.common.db.DBConnectorPath; |
| 32 | +import io.cdap.plugin.db.NoOpCommitConnection; |
| 33 | +import io.cdap.plugin.db.SchemaReader; |
| 34 | +import io.cdap.plugin.db.TransactionIsolationLevel; |
| 35 | +import io.cdap.plugin.db.connector.AbstractDBSpecificConnector; |
| 36 | +import io.cdap.plugin.db.connector.DBSpecificPath; |
| 37 | +import org.apache.hadoop.io.LongWritable; |
| 38 | +import org.apache.hadoop.mapreduce.lib.db.DBWritable; |
| 39 | + |
| 40 | +import java.io.IOException; |
| 41 | +import java.sql.Connection; |
| 42 | +import java.sql.SQLException; |
| 43 | +import java.util.HashMap; |
| 44 | +import java.util.Map; |
| 45 | + |
| 46 | +/** |
| 47 | + * Databricks Database Connector that connects to Databricks database via JDBC. |
| 48 | + */ |
| 49 | +@Plugin(type = Connector.PLUGIN_TYPE) |
| 50 | +@Name(DatabricksConstants.PLUGIN_NAME) |
| 51 | +@Description("Connection to access data in Databricks using JDBC.") |
| 52 | +@Category("Database") |
| 53 | +public class DatabricksConnector extends AbstractDBSpecificConnector<DatabricksDBRecord> { |
| 54 | + public static final String NAME = DatabricksConstants.PLUGIN_NAME; |
| 55 | + private final DatabricksConnectorConfig config; |
| 56 | + |
| 57 | + public DatabricksConnector(DatabricksConnectorConfig config) { |
| 58 | + super(config); |
| 59 | + this.config = config; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected DBConnectorPath getDBConnectorPath(String path) throws IOException { |
| 64 | + return DBSpecificPath.of(path, supportSchema()); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected Connection getConnection(DBConnectorPath path) { |
| 69 | + Connection connection = super.getConnection(path); |
| 70 | + try { |
| 71 | + connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); |
| 72 | + } catch (SQLException e) { |
| 73 | + throw new RuntimeException("Failed to set transaction isolation level to READ_UNCOMMITTED", e); |
| 74 | + } |
| 75 | + return new NoOpCommitConnection(connection); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected Connection getConnection() { |
| 80 | + Connection connection = super.getConnection(); |
| 81 | + try { |
| 82 | + connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); |
| 83 | + } catch (SQLException e) { |
| 84 | + throw new RuntimeException("Failed to set transaction isolation level to READ_UNCOMMITTED", e); |
| 85 | + } |
| 86 | + return new NoOpCommitConnection(connection); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public boolean supportSchema() { |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + protected Class<? extends DBWritable> getDBRecordType() { |
| 96 | + return DatabricksDBRecord.class; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public StructuredRecord transform(LongWritable longWritable, DatabricksDBRecord record) { |
| 101 | + return record.getRecord(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected SchemaReader getSchemaReader(String sessionID) { |
| 106 | + return new DatabricksSchemaReader(sessionID); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + protected String getTableName(String database, String schema, String table) { |
| 111 | + if (database == null && schema == null) { |
| 112 | + return String.format("`%s`", table); |
| 113 | + } |
| 114 | + if (database == null) { |
| 115 | + return String.format("`%s`.`%s`", schema, table); |
| 116 | + } |
| 117 | + if (schema == null) { |
| 118 | + return String.format("`%s`.`%s`", database, table); |
| 119 | + } |
| 120 | + return String.format("`%s`.`%s`.`%s`", database, schema, table); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + protected String getRandomQuery(String tableName, int limit) { |
| 125 | + return String.format("SELECT * FROM %s LIMIT %d", tableName, limit); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + protected void setConnectorSpec(ConnectorSpecRequest request, DBConnectorPath path, |
| 130 | + ConnectorSpec.Builder builder) { |
| 131 | + Map<String, String> sourceProperties = new HashMap<>(); |
| 132 | + setConnectionProperties(sourceProperties, request); |
| 133 | + builder.addRelatedPlugin(new PluginSpec(DatabricksConstants.PLUGIN_NAME, |
| 134 | + BatchSource.PLUGIN_TYPE, sourceProperties)); |
| 135 | + |
| 136 | + String schema = path.getSchema(); |
| 137 | + sourceProperties.put(DatabricksSource.DatabricksSourceConfig.NUM_SPLITS, "1"); |
| 138 | + sourceProperties.put(DatabricksSource.DatabricksSourceConfig.FETCH_SIZE, |
| 139 | + DatabricksSource.DatabricksSourceConfig.DEFAULT_FETCH_SIZE); |
| 140 | + String table = path.getTable(); |
| 141 | + if (table == null) { |
| 142 | + return; |
| 143 | + } |
| 144 | + sourceProperties.put(DatabricksSource.DatabricksSourceConfig.IMPORT_QUERY, |
| 145 | + getTableQuery(path.getDatabase(), schema, table)); |
| 146 | + sourceProperties.put(Constants.Reference.REFERENCE_NAME, ReferenceNames.cleanseReferenceName(table)); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + protected boolean isAutoCommitEnabled() { |
| 151 | + return true; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + protected String getTransactionIsolationLevel() { |
| 156 | + return TransactionIsolationLevel.Level.TRANSACTION_READ_UNCOMMITTED.name(); |
| 157 | + } |
| 158 | +} |
0 commit comments