Skip to content

Instantly share code, notes, and snippets.

@SingularBunny
Created November 20, 2019 18:24
Show Gist options
  • Save SingularBunny/363e450309d982d621ad2dcbaaff8819 to your computer and use it in GitHub Desktop.
Save SingularBunny/363e450309d982d621ad2dcbaaff8819 to your computer and use it in GitHub Desktop.
case class TestData(key: Int, value: String)
case class ThreeCloumntable(key: Int, value: String, key1: String)
class InsertSuite extends QueryTest with TestHiveSingleton with BeforeAndAfter
with SQLTestUtils with PrivateMethodTester {
import spark.implicits._
override lazy val testData = spark.sparkContext.parallelize(
(1 to 100).map(i => TestData(i, i.toString))).toDF()
before {
// Since every we are doing tests for DDL statements,
// it is better to reset before every test. hiveContext.reset()
// Creates a temporary view with testData, which will be used in all tests.
testData.createOrReplaceTempView("testData")
}
test("insertInto() HiveTable") {
withTable("createAndInsertTest") {
sql("CREATE TABLE createAndInsertTest (key int, value string)")
// Add some data.
testData.write.mode(SaveMode.Append).insertInto("createAndInsertTest")
// Make sure the table has also been updated.
checkAnswer(
sql("SELECT * FROM createAndInsertTest"),
testData.collect().toSeq
)
// Add more data.
testData.write.mode(SaveMode.Append).insertInto("createAndInsertTest")
// Make sure the table has been updated.
checkAnswer(
sql("SELECT * FROM createAndInsertTest"),
testData.toDF().collect().toSeq ++ testData.toDF().collect().toSeq
)
// Now overwrite.
testData.write.mode(SaveMode.Overwrite).insertInto("createAndInsertTest")
// Make sure the registered table has also been updated.
checkAnswer(
sql("SELECT * FROM createAndInsertTest"),
testData.collect().toSeq
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment