|
| 1 | +/* |
| 2 | + * Copyright © 2023 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.amazon.redshift; |
| 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.common.db.DBPath; |
| 33 | +import io.cdap.plugin.db.SchemaReader; |
| 34 | +import io.cdap.plugin.db.connector.AbstractDBSpecificConnector; |
| 35 | +import org.apache.hadoop.io.LongWritable; |
| 36 | +import org.apache.hadoop.mapreduce.lib.db.DBWritable; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.HashMap; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +/** |
| 43 | + * Amazon Redshift Database Connector that connects to Amazon Redshift database via JDBC. |
| 44 | + */ |
| 45 | +@Plugin(type = Connector.PLUGIN_TYPE) |
| 46 | +@Name(RedshiftConnector.NAME) |
| 47 | +@Description("Connection to access data in Amazon Redshift using JDBC.") |
| 48 | +@Category("Database") |
| 49 | +public class RedshiftConnector extends AbstractDBSpecificConnector<io.cdap.plugin.amazon.redshift.RedshiftDBRecord> { |
| 50 | + public static final String NAME = RedshiftConstants.PLUGIN_NAME; |
| 51 | + private final RedshiftConnectorConfig config; |
| 52 | + |
| 53 | + public RedshiftConnector(RedshiftConnectorConfig config) { |
| 54 | + super(config); |
| 55 | + this.config = config; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected DBConnectorPath getDBConnectorPath(String path) throws IOException { |
| 60 | + return new DBPath(path, true); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean supportSchema() { |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected Class<? extends DBWritable> getDBRecordType() { |
| 70 | + return RedshiftDBRecord.class; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public StructuredRecord transform(LongWritable longWritable, RedshiftDBRecord redshiftDBRecord) { |
| 75 | + return redshiftDBRecord.getRecord(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected SchemaReader getSchemaReader(String sessionID) { |
| 80 | + return new RedshiftSchemaReader(sessionID); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected String getTableName(String database, String schema, String table) { |
| 85 | + return String.format("\"%s\".\"%s\"", schema, table); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + protected String getRandomQuery(String tableName, int limit) { |
| 90 | + return String.format("SELECT * FROM %s\n" + |
| 91 | + "TABLESAMPLE BERNOULLI (100.0 * %d / (SELECT COUNT(*) FROM %s))", |
| 92 | + tableName, limit, tableName); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + protected void setConnectorSpec(ConnectorSpecRequest request, DBConnectorPath path, |
| 97 | + ConnectorSpec.Builder builder) { |
| 98 | + Map<String, String> sourceProperties = new HashMap<>(); |
| 99 | + setConnectionProperties(sourceProperties, request); |
| 100 | + builder |
| 101 | + .addRelatedPlugin(new PluginSpec(RedshiftConstants.PLUGIN_NAME, |
| 102 | + BatchSource.PLUGIN_TYPE, sourceProperties)); |
| 103 | + |
| 104 | + String schema = path.getSchema(); |
| 105 | + sourceProperties.put(RedshiftSource.RedshiftSourceConfig.NUM_SPLITS, "1"); |
| 106 | + sourceProperties.put(RedshiftSource.RedshiftSourceConfig.FETCH_SIZE, |
| 107 | + RedshiftSource.RedshiftSourceConfig.DEFAULT_FETCH_SIZE); |
| 108 | + String table = path.getTable(); |
| 109 | + if (table == null) { |
| 110 | + return; |
| 111 | + } |
| 112 | + sourceProperties.put(RedshiftSource.RedshiftSourceConfig.IMPORT_QUERY, |
| 113 | + getTableQuery(path.getDatabase(), schema, table)); |
| 114 | + sourceProperties.put(Constants.Reference.REFERENCE_NAME, ReferenceNames.cleanseReferenceName(table)); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments