Github user marmbrus commented on a diff in the pull request:
https://github.com/apache/spark/pull/8101#discussion_r36889141
--- Diff:
sql/core/src/test/scala/org/apache/spark/sql/jdbc/MySQLIntegration.scala ---
@@ -0,0 +1,218 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.spark.sql.jdbc
+
+import java.math.BigDecimal
+import java.sql.{Date, Timestamp}
+
+import org.scalatest.BeforeAndAfterAll
+
+import com.spotify.docker.client.DockerClient
+import com.spotify.docker.client.messages.ContainerConfig
+
+import org.apache.spark.SparkFunSuite
+import org.apache.spark.sql.test._
+
+class MySQLDatabase {
+ val docker: DockerClient = DockerClientFactory.get()
+
+ val containerId = {
+ docker.pull("mysql")
+ val config = ContainerConfig.builder().image("mysql")
+ .env("MYSQL_ROOT_PASSWORD=rootpass")
+ .build()
+ val id = docker.createContainer(config).id
+ docker.startContainer(id)
+ id
+ }
+
+ val ip = docker.inspectContainer(containerId).networkSettings.ipAddress
+
+ def close() {
+ docker.killContainer(containerId)
+ docker.removeContainer(containerId)
+ DockerClientFactory.close(docker)
+ }
+}
+
+class MySQLIntegration extends SparkFunSuite with BeforeAndAfterAll {
+ var ip: String = null
+
+ def url(ip: String): String = url(ip, "mysql")
+ def url(ip: String, db: String): String =
s"jdbc:mysql://$ip:3306/$db?user=root&password=rootpass"
+
+ def waitForDatabase(ip: String, maxMillis: Long) {
+ val before = System.currentTimeMillis()
+ var lastException: java.sql.SQLException = null
+ while (true) {
+ if (System.currentTimeMillis() > before + maxMillis) {
+ throw new java.sql.SQLException(s"Database not up after $maxMillis
ms.", lastException)
+ }
+ try {
+ val conn = java.sql.DriverManager.getConnection(url(ip))
+ conn.close()
+ return
+ } catch {
+ case e: java.sql.SQLException =>
+ lastException = e
+ java.lang.Thread.sleep(250)
+ }
+ }
+ }
+
+ def setupDatabase(ip: String) {
+ val conn = java.sql.DriverManager.getConnection(url(ip))
+ try {
+ conn.prepareStatement("CREATE DATABASE foo").executeUpdate()
+ conn.prepareStatement("CREATE TABLE foo.tbl (x INTEGER, y
TEXT(8))").executeUpdate()
+ conn.prepareStatement("INSERT INTO foo.tbl VALUES
(42,'fred')").executeUpdate()
+ conn.prepareStatement("INSERT INTO foo.tbl VALUES
(17,'dave')").executeUpdate()
+
+ conn.prepareStatement("CREATE TABLE foo.numbers (onebit BIT(1),
tenbits BIT(10), "
+ + "small SMALLINT, med MEDIUMINT, nor INT, big BIGINT, deci
DECIMAL(40,20), flt FLOAT, "
+ + "dbl DOUBLE)").executeUpdate()
+ conn.prepareStatement("INSERT INTO foo.numbers VALUES (b'0',
b'1000100101', "
+ + "17, 77777, 123456789, 123456789012345,
123456789012345.123456789012345, "
+ + "42.75, 1.0000000000000002)").executeUpdate()
+
+ conn.prepareStatement("CREATE TABLE foo.dates (d DATE, t TIME, dt
DATETIME, ts TIMESTAMP, "
+ + "yr YEAR)").executeUpdate()
+ conn.prepareStatement("INSERT INTO foo.dates VALUES ('1991-11-09',
'13:31:24', "
+ + "'1996-01-01 01:23:45', '2009-02-13 23:31:30',
'2001')").executeUpdate()
+
+ // TODO: Test locale conversion for strings.
+ conn.prepareStatement("CREATE TABLE foo.strings (a CHAR(10), b
VARCHAR(10), c TINYTEXT, "
+ + "d TEXT, e MEDIUMTEXT, f LONGTEXT, g BINARY(4), h VARBINARY(10),
i BLOB)"
+ ).executeUpdate()
+ conn.prepareStatement("INSERT INTO foo.strings VALUES ('the',
'quick', 'brown', 'fox', " +
+ "'jumps', 'over', 'the', 'lazy', 'dog')").executeUpdate()
+ } finally {
+ conn.close()
+ }
+ }
+
+ var db: MySQLDatabase = null
+
+ override def beforeAll() {
+ // If you load the MySQL driver here, DriverManager will deadlock. The
+ // MySQL driver gets loaded when its jar gets loaded, unlike the
Postgres
+ // and H2 drivers.
+ // scalastyle:off classforname
+ // Class.forName("com.mysql.jdbc.Driver")
+ // scalastyle:on classforname
+
+ db = new MySQLDatabase()
+ waitForDatabase(db.ip, 60000)
+ setupDatabase(db.ip)
+ ip = db.ip
+ }
+
+ override def afterAll() {
+ db.close()
+ }
+
+ test("Basic test") {
+ val df = TestSQLContext.jdbc(url(ip, "foo"), "tbl")
+ val rows = df.collect()
+ assert(rows.length == 2)
+ val types = rows(0).toSeq.map(x => x.getClass.toString)
+ assert(types.length == 2)
+ assert(types(0).equals("class java.lang.Integer"))
+ assert(types(1).equals("class java.lang.String"))
+ }
+
+ test("Numeric types") {
+ val df = TestSQLContext.jdbc(url(ip, "foo"), "numbers")
--- End diff --
```
[warn]
/home/jenkins/workspace/SparkPullRequestBuilder@2/sql/core/src/test/scala/org/apache/spark/sql/jdbc/MySQLIntegration.scala:130:
method jdbc in class SQLContext is deprecated: use read.jdbc()
[warn] val df = TestSQLContext.jdbc(url(ip, "foo"), "tbl")
[warn] ^
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]