-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathCloudSQLMySQLSource.java
More file actions
160 lines (141 loc) · 6.22 KB
/
CloudSQLMySQLSource.java
File metadata and controls
160 lines (141 loc) · 6.22 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
/*
* 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.mysql;
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.config.AbstractDBSpecificSourceConfig;
import io.cdap.plugin.db.source.AbstractDBSource;
import io.cdap.plugin.mysql.MysqlDBRecord;
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.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
/** Batch source to read from CloudSQL MySQL. */
@Plugin(type = BatchSource.PLUGIN_TYPE)
@Name(CloudSQLMySQLConstants.PLUGIN_NAME)
@Description(
"Reads from a CloudSQL database table using a configurable SQL query."
+ " Outputs one record for each row returned by the query.")
@Metadata(properties = {@MetadataProperty(key = Connector.PLUGIN_TYPE, value = CloudSQLMySQLConnector.NAME)})
public class CloudSQLMySQLSource extends AbstractDBSource<CloudSQLMySQLSource.CloudSQLMySQLSourceConfig> {
private final CloudSQLMySQLSourceConfig cloudsqlMysqlSourceConfig;
public CloudSQLMySQLSource(CloudSQLMySQLSourceConfig cloudsqlMysqlSourceConfig) {
super(cloudsqlMysqlSourceConfig);
this.cloudsqlMysqlSourceConfig = cloudsqlMysqlSourceConfig;
}
@Override
public void configurePipeline(PipelineConfigurer pipelineConfigurer) {
FailureCollector failureCollector = pipelineConfigurer.getStageConfigurer().getFailureCollector();
if (!cloudsqlMysqlSourceConfig.containsMacro(CloudSQLUtil.INSTANCE_TYPE) &&
!cloudsqlMysqlSourceConfig.containsMacro(CloudSQLUtil.CONNECTION_NAME)) {
CloudSQLUtil.checkConnectionName(
failureCollector,
cloudsqlMysqlSourceConfig.connection.getInstanceType(),
cloudsqlMysqlSourceConfig.connection.getConnectionName());
}
super.configurePipeline(pipelineConfigurer);
}
@Override
protected Class<? extends DBWritable> getDBRecordType() {
return MysqlDBRecord.class;
}
@Override
protected String createConnectionString() {
if (CloudSQLUtil.PRIVATE_INSTANCE.equalsIgnoreCase(
cloudsqlMysqlSourceConfig.connection.getInstanceType())) {
return String.format(
CloudSQLMySQLConstants.PRIVATE_CLOUDSQL_MYSQL_CONNECTION_STRING_FORMAT,
cloudsqlMysqlSourceConfig.connection.getConnectionName(),
cloudsqlMysqlSourceConfig.connection.getPort(),
cloudsqlMysqlSourceConfig.connection.getDatabase());
}
return String.format(
CloudSQLMySQLConstants.PUBLIC_CLOUDSQL_MYSQL_CONNECTION_STRING_FORMAT,
cloudsqlMysqlSourceConfig.connection.getDatabase(),
cloudsqlMysqlSourceConfig.connection.getConnectionName());
}
@Override
protected LineageRecorder getLineageRecorder(BatchSourceContext context) {
String host;
String location = "";
if (CloudSQLUtil.PRIVATE_INSTANCE.equalsIgnoreCase(cloudsqlMysqlSourceConfig.getConnection().getInstanceType())) {
// connection is the private IP address
host = cloudsqlMysqlSourceConfig.getConnection().getConnectionName();
} else {
// connection is of the form <projectId>:<region>:<instanceName>
String[] connectionParams = cloudsqlMysqlSourceConfig.getConnection().getConnectionName().split(":");
host = connectionParams[2];
location = connectionParams[1];
}
String fqn = DBUtils.constructFQN("mysql", host,
cloudsqlMysqlSourceConfig.getConnection().getPort(),
cloudsqlMysqlSourceConfig.getConnection().getDatabase(),
cloudsqlMysqlSourceConfig.getReferenceName());
Asset.Builder assetBuilder = Asset.builder(cloudsqlMysqlSourceConfig.getReferenceName()).setFqn(fqn);
if (!Strings.isNullOrEmpty(location)) {
assetBuilder.setLocation(location);
}
return new LineageRecorder(context, assetBuilder.build());
}
/** CloudSQL MySQL source config. */
public static class CloudSQLMySQLSourceConfig 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 CloudSQLMySQLConnectorConfig connection;
@Override
protected Map<String, String> getDBSpecificArguments() {
if (getFetchSize() == null || getFetchSize() <= 0) {
return Collections.emptyMap();
}
Map<String, String> arguments = new HashMap<>();
// If connected to MySQL > 5.0.2, and setFetchSize() > 0 on a statement,
// statement will use cursor-based fetching to retrieve rows
arguments.put("useCursorFetch", "true");
return arguments;
}
@Override
public void validate(FailureCollector collector) {
ConfigUtil.validateConnection(this, useConnection, connection, collector);
super.validate(collector);
}
@Override
protected CloudSQLMySQLConnectorConfig getConnection() {
return connection;
}
}
}