-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathMysqlSink.java
180 lines (153 loc) · 6.25 KB
/
MysqlSink.java
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
/*
* Copyright © 2019 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.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.api.data.format.StructuredRecord;
import io.cdap.cdap.etl.api.FailureCollector;
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.ConnectionConfig;
import io.cdap.plugin.db.DBRecord;
import io.cdap.plugin.db.config.AbstractDBSpecificSinkConfig;
import io.cdap.plugin.db.sink.AbstractDBSink;
import io.cdap.plugin.util.DBUtils;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Sink support for a MySQL database.
*/
@Plugin(type = BatchSink.PLUGIN_TYPE)
@Name(MysqlConstants.PLUGIN_NAME)
@Description("Writes records to a MySQL table. Each record will be written in a row in the table")
@Metadata(properties = {@MetadataProperty(key = Connector.PLUGIN_TYPE, value = MysqlConnector.NAME)})
public class MysqlSink extends AbstractDBSink<MysqlSink.MysqlSinkConfig> {
private final MysqlSinkConfig mysqlSinkConfig;
public MysqlSink(MysqlSinkConfig mysqlSinkConfig) {
super(mysqlSinkConfig);
this.mysqlSinkConfig = mysqlSinkConfig;
}
@Override
protected DBRecord getDBRecord(StructuredRecord output) {
return new MysqlDBRecord(output, columnTypes);
}
@Override
protected LineageRecorder getLineageRecorder(BatchSinkContext context) {
String fqn = DBUtils.constructFQN("mysql",
mysqlSinkConfig.getConnection().getHost(),
mysqlSinkConfig.getConnection().getPort(),
mysqlSinkConfig.database, mysqlSinkConfig.getReferenceName());
Asset asset = Asset.builder(mysqlSinkConfig.getReferenceName()).setFqn(fqn).build();
return new LineageRecorder(context, asset);
}
/**
* MySQL action configuration.
*/
public static class MysqlSinkConfig 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 MysqlConnectorConfig connection;
@Name(ConnectionConfig.DATABASE)
@Description("Database name to connect to")
@Macro
private String database;
@Name(MysqlConstants.AUTO_RECONNECT)
@Description("Should the driver try to re-establish stale and/or dead connections")
@Nullable
public Boolean autoReconnect;
@Name(MysqlConstants.USE_COMPRESSION)
@Description("Select this option for WAN connections")
@Nullable
public Boolean useCompression;
@Name(MysqlConstants.SQL_MODE)
@Description("Override the default SQL_MODE session variable used by the server")
@Nullable
public String sqlMode;
@Name(MysqlConstants.USE_SSL)
@Description("Turns on SSL encryption. Connection will fail if SSL is not available")
@Nullable
public String useSSL;
@Name(MysqlConstants.CLIENT_CERT_KEYSTORE_URL)
@Description("URL to the client certificate KeyStore (if not specified, use defaults)")
@Nullable
public String clientCertificateKeyStoreUrl;
@Name(MysqlConstants.CLIENT_CERT_KEYSTORE_PASSWORD)
@Description("Password for the client certificates KeyStore")
@Nullable
public String clientCertificateKeyStorePassword;
@Name(MysqlConstants.TRUST_CERT_KEYSTORE_URL)
@Description("URL to the trusted root certificate KeyStore (if not specified, use defaults)")
@Nullable
public String trustCertificateKeyStoreUrl;
@Name(MysqlConstants.TRUST_CERT_KEYSTORE_PASSWORD)
@Description("Password for the trusted root certificates KeyStore")
@Nullable
public String trustCertificateKeyStorePassword;
@Override
public String getConnectionString() {
return MysqlUtil.getConnectionString(connection.getHost(), connection.getPort(), database);
}
@Override
public Map<String, String> getDBSpecificArguments() {
return MysqlUtil.composeDbSpecificArgumentsMap(autoReconnect, useCompression, useSSL,
clientCertificateKeyStoreUrl,
clientCertificateKeyStorePassword,
trustCertificateKeyStoreUrl,
trustCertificateKeyStorePassword, false);
}
@Override
public String getTransactionIsolationLevel() {
return connection.getTransactionIsolationLevel();
}
@Override
public MysqlConnectorConfig getConnection() {
return connection;
}
@Override
public void validate(FailureCollector collector) {
super.validate(collector);
ConfigUtil.validateConnection(this, useConnection, connection, collector);
}
@Override
public List<String> getInitQueries() {
if (!Strings.isNullOrEmpty(sqlMode)) {
return Collections.singletonList(String.format(MysqlConstants.SET_SQL_MODE_QUERY_FORMAT, sqlMode));
}
return Collections.emptyList();
}
@Override
public boolean canConnect() {
return super.canConnect() && !containsMacro(ConnectionConfig.DATABASE);
}
}
}