-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for JdbcRDD using embedded derby, per rxin suggestion
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,4 @@ streaming-tests.log | |
dependency-reduced-pom.xml | ||
.ensime | ||
.ensime_lucene | ||
derby.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package spark | ||
|
||
import org.scalatest.{ BeforeAndAfter, FunSuite } | ||
import spark.SparkContext._ | ||
import spark.rdd.JdbcRDD | ||
import java.sql._ | ||
|
||
class JdbcRDDSuite extends FunSuite with BeforeAndAfter with LocalSparkContext { | ||
|
||
before { | ||
Class.forName("org.apache.derby.jdbc.EmbeddedDriver") | ||
val conn = DriverManager.getConnection("jdbc:derby:target/JdbcRDDSuiteDb;create=true") | ||
try { | ||
val create = conn.createStatement | ||
create.execute(""" | ||
CREATE TABLE FOO( | ||
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), | ||
DATA INTEGER | ||
)""") | ||
create.close | ||
val insert = conn.prepareStatement("INSERT INTO FOO(DATA) VALUES(?)") | ||
(1 to 100).foreach { i => | ||
insert.setInt(1, i * 2) | ||
insert.executeUpdate | ||
} | ||
insert.close | ||
} catch { | ||
case e: SQLException if e.getSQLState == "X0Y32" => | ||
// table exists | ||
} finally { | ||
conn.close | ||
} | ||
} | ||
|
||
test("basic functionality") { | ||
sc = new SparkContext("local", "test") | ||
val rdd = new JdbcRDD( | ||
sc, | ||
() => { DriverManager.getConnection("jdbc:derby:target/JdbcRDDSuiteDb") }, | ||
"SELECT DATA FROM FOO WHERE ? <= ID AND ID <= ?", | ||
1, 100, 3, | ||
(r: ResultSet) => { r.getInt(1) } ).cache | ||
|
||
assert(rdd.count === 100) | ||
assert(rdd.reduce(_+_) === 10100) | ||
} | ||
|
||
after { | ||
try { | ||
DriverManager.getConnection("jdbc:derby:;shutdown=true") | ||
} catch { | ||
case se: SQLException if se.getSQLState == "XJ015" => | ||
// normal shutdown | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters