-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathCloudSQLMySQLSink.java
More file actions
191 lines (166 loc) · 6.95 KB
/
CloudSQLMySQLSink.java
File metadata and controls
191 lines (166 loc) · 6.95 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
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* 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.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
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.api.data.format.StructuredRecord;
import io.cdap.cdap.api.data.schema.Schema;
import io.cdap.cdap.etl.api.FailureCollector;
import io.cdap.cdap.etl.api.PipelineConfigurer;
import io.cdap.cdap.etl.api.batch.BatchSink;
import io.cdap.cdap.etl.api.batch.BatchSinkContext;
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.DBRecord;
import io.cdap.plugin.db.config.AbstractDBSpecificSinkConfig;
import io.cdap.plugin.db.sink.AbstractDBSink;
import io.cdap.plugin.mysql.MysqlDBRecord;
import io.cdap.plugin.util.CloudSQLUtil;
import io.cdap.plugin.util.DBUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import javax.annotation.Nullable;
/** Sink support for a CloudSQL MySQL database. */
@Plugin(type = BatchSink.PLUGIN_TYPE)
@Name(CloudSQLMySQLConstants.PLUGIN_NAME)
@Description(
"Writes records to a CloudSQL MySQL table. Each record will be written in a row in the table.")
@Metadata(properties = {@MetadataProperty(key = Connector.PLUGIN_TYPE, value = CloudSQLMySQLConnector.NAME)})
public class CloudSQLMySQLSink extends AbstractDBSink<CloudSQLMySQLSink.CloudSQLMySQLSinkConfig> {
private final CloudSQLMySQLSinkConfig cloudsqlMysqlSinkConfig;
private static final Character ESCAPE_CHAR = '`';
public CloudSQLMySQLSink(CloudSQLMySQLSinkConfig cloudsqlMysqlSinkConfig) {
super(cloudsqlMysqlSinkConfig);
this.cloudsqlMysqlSinkConfig = cloudsqlMysqlSinkConfig;
}
@Override
public void configurePipeline(PipelineConfigurer pipelineConfigurer) {
FailureCollector failureCollector = pipelineConfigurer.getStageConfigurer().getFailureCollector();
if (!cloudsqlMysqlSinkConfig.containsMacro(CloudSQLUtil.INSTANCE_TYPE) &&
!cloudsqlMysqlSinkConfig.containsMacro(CloudSQLUtil.CONNECTION_NAME)) {
CloudSQLUtil.checkConnectionName(
failureCollector,
cloudsqlMysqlSinkConfig.connection.getInstanceType(),
cloudsqlMysqlSinkConfig.connection.getConnectionName(),
CloudSQLUtil.CLOUDSQL_MYSQL);
}
super.configurePipeline(pipelineConfigurer);
}
@Override
protected DBRecord getDBRecord(StructuredRecord output) {
return new MysqlDBRecord(output, columnTypes);
}
@Override
protected void setColumnsInfo(List<Schema.Field> fields) {
List<String> columnsList = new ArrayList<>();
StringJoiner columnsJoiner = new StringJoiner(",");
for (Schema.Field field : fields) {
columnsList.add(field.getName());
columnsJoiner.add(ESCAPE_CHAR + field.getName() + ESCAPE_CHAR);
}
super.columns = Collections.unmodifiableList(columnsList);
super.dbColumns = columnsJoiner.toString();
}
@VisibleForTesting
String getDbColumns() {
return dbColumns;
}
@Override
protected String getErrorDetailsProviderClassName() {
return CloudSQLMySQLErrorDetailsProvider.class.getName();
}
@Override
protected String getExternalDocumentationLink() {
return DBUtils.CLOUDSQLMYSQL_SUPPORTED_DOC_URL;
}
@Override
protected LineageRecorder getLineageRecorder(BatchSinkContext context) {
String host;
String location = "";
if (CloudSQLUtil.PRIVATE_INSTANCE.equalsIgnoreCase(cloudsqlMysqlSinkConfig.getConnection().getInstanceType())) {
// connection is the private IP address
host = cloudsqlMysqlSinkConfig.getConnection().getConnectionName();
} else {
// connection is of the form <projectId>:<region>:<instanceName>
String[] connectionParams = cloudsqlMysqlSinkConfig.getConnection().getConnectionName().split(":");
host = connectionParams[2];
location = connectionParams[1];
}
String fqn = DBUtils.constructFQN("mysql", host,
cloudsqlMysqlSinkConfig.getConnection().getPort(),
cloudsqlMysqlSinkConfig.getConnection().getDatabase(),
cloudsqlMysqlSinkConfig.getReferenceName());
Asset.Builder assetBuilder = Asset.builder(cloudsqlMysqlSinkConfig.getReferenceName()).setFqn(fqn);
if (!Strings.isNullOrEmpty(location)) {
assetBuilder.setLocation(location);
}
return new LineageRecorder(context, assetBuilder.build());
}
/** CloudSQL MySQL sink configuration. */
public static class CloudSQLMySQLSinkConfig extends AbstractDBSpecificSinkConfig {
@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;
@Name(CloudSQLMySQLConstants.CONNECTION_TIMEOUT)
@Description(
"The timeout value used for socket connect operations. If connecting to the server takes longer "
+ "than this value, the connection is broken. The timeout is specified in seconds and a value "
+ "of zero means that it is disabled")
@Nullable
public Integer connectionTimeout;
@Name(TRANSACTION_ISOLATION_LEVEL)
@Description("Transaction isolation level for queries run by this sink.")
@Nullable
public String transactionIsolationLevel;
@Override
public String getTransactionIsolationLevel() {
return transactionIsolationLevel;
}
@Override
public Map<String, String> getDBSpecificArguments() {
return ImmutableMap.of(
CloudSQLMySQLConstants.CONNECTION_TIMEOUT, String.valueOf(connectionTimeout));
}
@Override
public void validate(FailureCollector collector) {
ConfigUtil.validateConnection(this, useConnection, connection, collector);
super.validate(collector);
}
@Override
@Nullable
protected CloudSQLMySQLConnectorConfig getConnection() {
return connection;
}
}
}