-
Notifications
You must be signed in to change notification settings - Fork 40
/
JdbcExampleV2.scala
295 lines (237 loc) · 7.65 KB
/
JdbcExampleV2.scala
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package datasource
import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}
import java.util.Optional
import org.apache.spark.sql.sources.{EqualTo, Filter}
import org.apache.spark.sql.{Row, SaveMode, SparkSession}
import org.apache.spark.sql.sources.v2.reader._
import org.apache.spark.sql.sources.v2.writer.{DataSourceWriter, DataWriter, DataWriterFactory, WriterCommitMessage}
import org.apache.spark.sql.sources.v2.{DataSourceOptions, DataSourceV2, ReadSupport, WriteSupport}
import org.apache.spark.sql.types._
import org.slf4j.LoggerFactory
import collection.JavaConverters._
import scala.collection.mutable.ListBuffer
object JdbcSourceV2 {
val schema = StructType(Seq(
StructField("id", IntegerType),
StructField("emp_name", StringType),
StructField("dep_name", StringType),
StructField("salary", DecimalType(7, 2)),
StructField("age", DecimalType(3, 0))
))
}
class JdbcSourceV2 extends DataSourceV2 with ReadSupport with WriteSupport {
def createReader(options: DataSourceOptions) = new JdbcDataSourceReader(
options.get("url").get(),
options.get("user").get(),
options.get("password").get(),
options.get("table").get()
)
override def createWriter(
jobId: String,
schema: StructType,
mode: SaveMode,
options: DataSourceOptions): Optional[DataSourceWriter] = {
Optional.of(new JdbcDataSourceWriter(
options.get("url").get(),
options.get("user").get(),
options.get("password").get(),
options.get("table").get(),
schema
))
}
}
class JdbcDataSourceReader(
url: String,
user: String,
password: String,
table: String)
extends DataSourceReader with SupportsPushDownRequiredColumns
with SupportsPushDownFilters {
var requiredSchema = JdbcSourceV2.schema
var filters = Array.empty[Filter]
var wheres = Array.empty[String]
def readSchema = requiredSchema
def createDataReaderFactories() = {
val columns = requiredSchema.fields.map(_.name)
Seq((1, 6), (7, 11)).map { case (minId, maxId) =>
val partition = s"id BETWEEN $minId AND $maxId"
new JdbcDataReaderFactory(url, user, password, table, columns, wheres, partition)
.asInstanceOf[DataReaderFactory[Row]]
}.asJava
}
def pruneColumns(requiredSchema: StructType) = {
this.requiredSchema = requiredSchema
}
def pushFilters(filters: Array[Filter]) = {
val supported = ListBuffer.empty[Filter]
val unsupported = ListBuffer.empty[Filter]
val wheres = ListBuffer.empty[String]
filters.foreach {
case filter: EqualTo => {
supported += filter
wheres += s"${filter.attribute} = '${filter.value}'"
}
case filter => unsupported += filter
}
this.filters = supported.toArray
this.wheres = wheres.toArray
unsupported.toArray
}
def pushedFilters = filters
}
class JdbcDataReaderFactory(
url: String,
user: String,
password: String,
table: String,
columns: Seq[String],
wheres: Seq[String],
partition: String)
extends DataReaderFactory[Row] {
def createDataReader() = new JdbcDataReader(url, user, password, table, columns, wheres, partition)
}
class JdbcDataReader(
url: String,
user: String,
password: String,
table: String,
columns: Seq[String],
wheres: Seq[String],
partition: String)
extends DataReader[Row] {
private val logger = LoggerFactory.getLogger(this.getClass)
private var conn: Connection = null
private var rs: ResultSet = null
def next() = {
if (rs == null) {
conn = DriverManager.getConnection(url, user, password)
val sqlBuilder = new StringBuilder()
sqlBuilder ++= s"SELECT ${columns.mkString(", ")} FROM $table WHERE $partition"
if (wheres.nonEmpty) {
sqlBuilder ++= " AND " + wheres.mkString(" AND ")
}
val sql = sqlBuilder.toString
logger.info(sql)
val stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY)
stmt.setFetchSize(1000)
rs = stmt.executeQuery()
}
rs.next()
}
def get() = {
val values = columns.map {
case "id" => rs.getInt("id")
case "emp_name" => rs.getString("emp_name")
case "dep_name" => rs.getString("dep_name")
case "salary" => rs.getBigDecimal("salary")
case "age" => rs.getBigDecimal("age")
}
Row.fromSeq(values)
}
def close() = {
conn.close()
}
}
class JdbcDataSourceWriter(
url: String,
user: String,
password: String,
table: String,
schema: StructType)
extends DataSourceWriter {
override def createWriterFactory(): DataWriterFactory[Row] = {
new JdbcDataWriterFactory(url, user, password, table, schema)
}
override def commit(messages: Array[WriterCommitMessage]): Unit = Unit
override def abort(messages: Array[WriterCommitMessage]): Unit = Unit
}
class JdbcDataWriterFactory(
url: String,
user: String,
password: String,
table: String,
schema: StructType)
extends DataWriterFactory[Row] {
override def createDataWriter(
partitionId: Int,
attemptNumber: Int): DataWriter[Row] = {
new JdbcDataWriter(url, user, password, table, schema)
}
}
class JdbcDataWriter(
url: String,
user: String,
password: String,
table: String,
schema: StructType)
extends DataWriter[Row] {
private val logger = LoggerFactory.getLogger(this.getClass)
private var conn: Connection = null
private var stmt: PreparedStatement = null
override def write(record: Row): Unit = {
if (stmt == null) {
conn = DriverManager.getConnection(url, user, password)
conn.setAutoCommit(false)
val columns = schema.fields.map(_.name).mkString(", ")
val placeholders = ("?" * schema.fields.length).mkString(", ")
stmt = conn.prepareStatement(s"REPLACE INTO $table ($columns) VALUES ($placeholders)")
}
schema.fields.map(_.name).zipWithIndex.foreach { case (name, index) =>
name match {
case "id" => stmt.setInt(index + 1, record.getInt(index))
case "emp_name" => stmt.setString(index + 1, record.getString(index))
case "dep_name" => stmt.setString(index + 1, record.getString(index))
case "salary" => stmt.setBigDecimal(index + 1, record.getDecimal(index))
case "age" => stmt.setBigDecimal(index + 1, record.getDecimal(index))
}
}
logger.info(stmt.toString)
stmt.executeUpdate()
}
override def commit(): WriterCommitMessage = {
conn.commit()
conn.close()
null
}
override def abort(): Unit = {
conn.rollback()
conn.close()
}
}
object JdbcExampleV2 {
def main(args: Array[String]): Unit = {
val spark = SparkSession
.builder()
.master("local[2]")
.getOrCreate()
val df = spark.read
.format("datasource.JdbcSourceV2")
.option("url", "jdbc:mysql://localhost/spark")
.option("user", "root")
.option("password", "")
.option("table", "employee")
.load()
df.printSchema()
df.show()
df.createTempView("employee")
val dfSelect = spark.sql("SELECT COUNT(*), AVG(salary) FROM employee WHERE dep_name = 'Management'")
dfSelect.explain(true)
dfSelect.show()
val dfWrite = spark.createDataFrame(
spark.sparkContext.parallelize(Seq(
Row(100, "John Doe", "Test", BigDecimal.decimal(1500), BigDecimal.decimal(32)),
Row(101, "Jane Doe", "Test", BigDecimal.decimal(1000), BigDecimal.decimal(28))
)),
JdbcSourceV2.schema
)
dfWrite.write
.format("datasource.JdbcSourceV2")
.option("url", "jdbc:mysql://localhost/spark")
.option("user", "root")
.option("password", "")
.option("table", "employee")
.save()
spark.stop()
}
}