-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathCloudSQLPostgreSQLSource.java
More file actions
178 lines (155 loc) · 6.79 KB
/
CloudSQLPostgreSQLSource.java
File metadata and controls
178 lines (155 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright © 2020 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.cloudsql.postgres;
import com.google.common.base.Strings;
import io.cdap.cdap.api.annotation.Description;
import io.cdap.cdap.api.annotation.Macro;
import io.cdap.cdap.api.annotation.Metadata;
import io.cdap.cdap.api.annotation.MetadataProperty;
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.annotation.Plugin;
import io.cdap.cdap.etl.api.FailureCollector;
import io.cdap.cdap.etl.api.PipelineConfigurer;
import io.cdap.cdap.etl.api.batch.BatchSource;
import io.cdap.cdap.etl.api.batch.BatchSourceContext;
import io.cdap.cdap.etl.api.connector.Connector;
import io.cdap.plugin.common.Asset;
import io.cdap.plugin.common.ConfigUtil;
import io.cdap.plugin.common.LineageRecorder;
import io.cdap.plugin.db.SchemaReader;
import io.cdap.plugin.db.config.AbstractDBSpecificSourceConfig;
import io.cdap.plugin.db.source.AbstractDBSource;
import io.cdap.plugin.postgres.PostgresDBRecord;
import io.cdap.plugin.postgres.PostgresSchemaReader;
import io.cdap.plugin.util.CloudSQLUtil;
import io.cdap.plugin.util.DBUtils;
import org.apache.hadoop.mapreduce.lib.db.DBWritable;
import java.util.Collections;
import java.util.Map;
import javax.annotation.Nullable;
/** Batch source to read from a CloudSQL PostgreSQL instance database. */
@Plugin(type = BatchSource.PLUGIN_TYPE)
@Name(CloudSQLPostgreSQLConstants.PLUGIN_NAME)
@Description(
"Reads from a CloudSQL PostgreSQL database table(s) using a configurable SQL query."
+ " Outputs one record for each row returned by the query.")
@Metadata(properties = {@MetadataProperty(key = Connector.PLUGIN_TYPE, value = CloudSQLPostgreSQLConnector.NAME)})
public class CloudSQLPostgreSQLSource
extends AbstractDBSource<CloudSQLPostgreSQLSource.CloudSQLPostgreSQLSourceConfig> {
private final CloudSQLPostgreSQLSourceConfig cloudsqlPostgresqlSourceConfig;
public CloudSQLPostgreSQLSource(CloudSQLPostgreSQLSourceConfig cloudsqlPostgresqlSourceConfig) {
super(cloudsqlPostgresqlSourceConfig);
this.cloudsqlPostgresqlSourceConfig = cloudsqlPostgresqlSourceConfig;
}
@Override
public void configurePipeline(PipelineConfigurer pipelineConfigurer) {
FailureCollector failureCollector = pipelineConfigurer.getStageConfigurer().getFailureCollector();
if (!cloudsqlPostgresqlSourceConfig.containsMacro(CloudSQLUtil.INSTANCE_TYPE) &&
!cloudsqlPostgresqlSourceConfig.containsMacro(CloudSQLUtil.CONNECTION_NAME)) {
CloudSQLUtil.checkConnectionName(
failureCollector,
cloudsqlPostgresqlSourceConfig.connection.getInstanceType(),
cloudsqlPostgresqlSourceConfig.connection.getConnectionName(),
CloudSQLUtil.CLOUDSQL_POSTGRESQL);
}
super.configurePipeline(pipelineConfigurer);
}
@Override
protected SchemaReader getSchemaReader() {
return new PostgresSchemaReader();
}
@Override
protected Class<? extends DBWritable> getDBRecordType() {
return PostgresDBRecord.class;
}
@Override
protected String getExternalDocumentationLink() {
return DBUtils.CLOUDSQLPOSTGRES_SUPPORTED_DOC_URL;
}
@Override
protected String getErrorDetailsProviderClassName() {
return CloudSQLPostgreSQLErrorDetailsProvider.class.getName();
}
@Override
protected String createConnectionString() {
if (CloudSQLUtil.PRIVATE_INSTANCE.equalsIgnoreCase(
cloudsqlPostgresqlSourceConfig.connection.getInstanceType())) {
return String.format(
CloudSQLPostgreSQLConstants.PRIVATE_CLOUDSQL_POSTGRES_CONNECTION_STRING_FORMAT,
cloudsqlPostgresqlSourceConfig.connection.getConnectionName(),
cloudsqlPostgresqlSourceConfig.connection.getPort(),
cloudsqlPostgresqlSourceConfig.connection.getDatabase());
}
return String.format(
CloudSQLPostgreSQLConstants.PUBLIC_CLOUDSQL_POSTGRES_CONNECTION_STRING_FORMAT,
cloudsqlPostgresqlSourceConfig.connection.getDatabase(),
cloudsqlPostgresqlSourceConfig.connection.getConnectionName());
}
@Override
protected LineageRecorder getLineageRecorder(BatchSourceContext context) {
String host;
String location = "";
if (CloudSQLUtil.PRIVATE_INSTANCE.equalsIgnoreCase(
cloudsqlPostgresqlSourceConfig.getConnection().getInstanceType())) {
// connection is the private IP address
host = cloudsqlPostgresqlSourceConfig.getConnection().getConnectionName();
} else {
// connection is of the form <projectId>:<region>:<instanceName>
String[] connectionParams = cloudsqlPostgresqlSourceConfig.getConnection().getConnectionName().split(":");
host = connectionParams[2];
location = connectionParams[1];
}
String fqn = DBUtils.constructFQN("postgres", host,
cloudsqlPostgresqlSourceConfig.getConnection().getPort(),
cloudsqlPostgresqlSourceConfig.getConnection().getDatabase(),
cloudsqlPostgresqlSourceConfig.getReferenceName());
Asset.Builder assetBuilder = Asset.builder(cloudsqlPostgresqlSourceConfig.getReferenceName()).setFqn(fqn);
if (!Strings.isNullOrEmpty(location)) {
assetBuilder.setLocation(location);
}
return new LineageRecorder(context, assetBuilder.build());
}
/** CloudSQL PostgreSQL source config. */
public static class CloudSQLPostgreSQLSourceConfig extends AbstractDBSpecificSourceConfig {
@Name(ConfigUtil.NAME_USE_CONNECTION)
@Nullable
@Description("Whether to use an existing connection.")
private Boolean useConnection;
@Name(ConfigUtil.NAME_CONNECTION)
@Macro
@Nullable
@Description("The existing connection to use.")
private CloudSQLPostgreSQLConnectorConfig connection;
@Override
protected Map<String, String> getDBSpecificArguments() {
return Collections.emptyMap();
}
@Override
public Integer getFetchSize() {
Integer fetchSize = super.getFetchSize();
return fetchSize == null ? Integer.parseInt(DEFAULT_FETCH_SIZE) : fetchSize;
}
@Override
protected CloudSQLPostgreSQLConnectorConfig getConnection() {
return connection;
}
@Override
public void validate(FailureCollector collector) {
ConfigUtil.validateConnection(this, useConnection, connection, collector);
super.validate(collector);
}
}
}