|
| 1 | +/* |
| 2 | + * Copyright © 2020 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.cloudsql.postgres; |
| 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.BatchSink; |
| 25 | +import io.cdap.cdap.etl.api.batch.BatchSource; |
| 26 | +import io.cdap.cdap.etl.api.connector.Connector; |
| 27 | +import io.cdap.cdap.etl.api.connector.ConnectorSpec; |
| 28 | +import io.cdap.cdap.etl.api.connector.ConnectorSpecRequest; |
| 29 | +import io.cdap.cdap.etl.api.connector.PluginSpec; |
| 30 | +import io.cdap.plugin.common.Constants; |
| 31 | +import io.cdap.plugin.common.ReferenceNames; |
| 32 | +import io.cdap.plugin.common.db.DBConnectorPath; |
| 33 | +import io.cdap.plugin.db.ConnectionConfig; |
| 34 | +import io.cdap.plugin.db.SchemaReader; |
| 35 | +import io.cdap.plugin.db.connector.AbstractDBSpecificConnector; |
| 36 | +import io.cdap.plugin.postgres.PostgresDBRecord; |
| 37 | +import io.cdap.plugin.postgres.PostgresSchemaReader; |
| 38 | +import org.apache.hadoop.io.LongWritable; |
| 39 | +import org.apache.hadoop.mapreduce.lib.db.DBWritable; |
| 40 | + |
| 41 | +import java.util.HashMap; |
| 42 | +import java.util.Map; |
| 43 | + |
| 44 | +/** |
| 45 | + * A CloudSQL PostgreSQL Database Connector that connects to CloudSQL PostgreSQL database via JDBC. |
| 46 | + */ |
| 47 | +@Plugin(type = Connector.PLUGIN_TYPE) |
| 48 | +@Name(CloudSQLPostgreSQLConnector.NAME) |
| 49 | +@Description("Connection to access data in CloudSQL PostgreSQL Server databases using JDBC.") |
| 50 | +@Category("Database") |
| 51 | +public class CloudSQLPostgreSQLConnector extends AbstractDBSpecificConnector<PostgresDBRecord> { |
| 52 | + public static final String NAME = CloudSQLPostgreSQLConstants.PLUGIN_NAME; |
| 53 | + private final CloudSQLPostgreSQLConnectorConfig config; |
| 54 | + |
| 55 | + public CloudSQLPostgreSQLConnector(CloudSQLPostgreSQLConnectorConfig config) { |
| 56 | + super(config); |
| 57 | + this.config = config; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean supportSchema() { |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected Class<? extends DBWritable> getDBRecordType() { |
| 67 | + return PostgresDBRecord.class; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public StructuredRecord transform(LongWritable longWritable, PostgresDBRecord postgresDBRecord) { |
| 72 | + return postgresDBRecord.getRecord(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected SchemaReader getSchemaReader() { |
| 77 | + return new PostgresSchemaReader(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected String getTableQuery(DBConnectorPath path) { |
| 82 | + return String.format("SELECT * FROM \"%s\".\"%s\"", path.getSchema(), path.getTable()); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected String getTableQuery(DBConnectorPath path, int limit) { |
| 87 | + return String.format("SELECT * FROM \"%s\".\"%s\" LIMIT %d", path.getSchema(), path.getTable(), limit); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + protected void setConnectorSpec(ConnectorSpecRequest request, DBConnectorPath path, |
| 92 | + ConnectorSpec.Builder builder) { |
| 93 | + Map<String, String> properties = new HashMap<>(); |
| 94 | + setConnectionProperties(properties); |
| 95 | + builder |
| 96 | + .addRelatedPlugin(new PluginSpec(CloudSQLPostgreSQLConstants.PLUGIN_NAME, BatchSource.PLUGIN_TYPE, properties)) |
| 97 | + .addRelatedPlugin(new PluginSpec(CloudSQLPostgreSQLConstants.PLUGIN_NAME, BatchSink.PLUGIN_TYPE, properties)); |
| 98 | + |
| 99 | + String table = path.getTable(); |
| 100 | + if (table == null) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + properties.put(CloudSQLPostgreSQLSource.CloudSQLPostgreSQLSourceConfig.IMPORT_QUERY, getTableQuery(path)); |
| 105 | + properties.put(CloudSQLPostgreSQLSource.CloudSQLPostgreSQLSourceConfig.NUM_SPLITS, "1"); |
| 106 | + properties.put(ConnectionConfig.DATABASE, path.getDatabase()); |
| 107 | + properties.put(Constants.Reference.REFERENCE_NAME, ReferenceNames.cleanseReferenceName(table)); |
| 108 | + properties.put(CloudSQLPostgreSQLSink.CloudSQLPostgreSQLSinkConfig.TABLE_NAME, table); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + protected void setConnectionProperties(Map<String, String> properties) { |
| 113 | + Map<String, String> rawProperties = config.getRawProperties().getProperties(); |
| 114 | + properties.put(ConnectionConfig.JDBC_PLUGIN_NAME, rawProperties.get(ConnectionConfig.JDBC_PLUGIN_NAME)); |
| 115 | + properties.put(ConnectionConfig.USER, rawProperties.get(ConnectionConfig.USER)); |
| 116 | + properties.put(ConnectionConfig.PASSWORD, rawProperties.get(ConnectionConfig.PASSWORD)); |
| 117 | + properties.put(CloudSQLPostgreSQLConstants.CONNECTION_NAME, |
| 118 | + rawProperties.get(CloudSQLPostgreSQLConstants.CONNECTION_NAME)); |
| 119 | + properties.put(CloudSQLPostgreSQLConstants.INSTANCE_TYPE, |
| 120 | + rawProperties.get(CloudSQLPostgreSQLConstants.INSTANCE_TYPE)); |
| 121 | + } |
| 122 | +} |
0 commit comments