-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: UNIC-1512 Implement Jdbc Upsert
- Loading branch information
Laura Bégin
committed
May 30, 2024
1 parent
4a6a66c
commit a5a884f
Showing
5 changed files
with
136 additions
and
13 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
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
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
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,12 @@ | ||
create schema test_schema | ||
|
||
create table test_schema.test | ||
( | ||
uid varchar not null, | ||
oid varchar not null, | ||
createdOn timestamp not null, | ||
updatedOn timestamp not null, | ||
data bigint, | ||
chromosome varchar(2) not null, | ||
start bigint | ||
); |
79 changes: 79 additions & 0 deletions
79
datalake-spark3/src/test/scala/bio/ferlab/datalake/spark3/loader/JdbcLoaderSpec.scala
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,79 @@ | ||
package bio.ferlab.datalake.spark3.loader | ||
|
||
import bio.ferlab.datalake.testutils.SparkSpec | ||
import com.dimafeng.testcontainers.scalatest.TestContainerForEach | ||
import com.dimafeng.testcontainers.{JdbcDatabaseContainer, PostgreSQLContainer} | ||
import org.scalatest.matchers.should.Matchers | ||
import org.testcontainers.utility.DockerImageName | ||
|
||
import java.sql.Timestamp | ||
import java.time.LocalDateTime | ||
|
||
class JdbcLoaderSpec extends SparkSpec with Matchers with TestContainerForEach { | ||
|
||
import spark.implicits._ | ||
|
||
val databaseName = "test_db" | ||
val schemaName = "test_schema" | ||
val tableName = "test" | ||
|
||
override val containerDef: PostgreSQLContainer.Def = PostgreSQLContainer.Def( | ||
dockerImageName = DockerImageName.parse("postgres:15.1"), | ||
databaseName = databaseName, | ||
username = "scala", | ||
password = "scala", | ||
commonJdbcParams = JdbcDatabaseContainer.CommonParams(initScriptPath = Some("jdbc/init-dbt.sql")) | ||
) | ||
|
||
def withPostgresContainer(options: Map[String, String] => Unit): Unit = withContainers { psqlContainer => | ||
options(Map( | ||
"url" -> psqlContainer.jdbcUrl, | ||
"driver" -> "org.postgresql.Driver", | ||
"user" -> psqlContainer.username, | ||
"password" -> psqlContainer.password | ||
)) | ||
} | ||
|
||
"upsert" should "update existing data and insert new data" in { | ||
withPostgresContainer { options => | ||
val readOptions = options + ("dbtable" -> s"$schemaName.$tableName") | ||
val day1 = LocalDateTime.of(2020, 1, 1, 1, 1, 1) | ||
val day2 = day1.plusDays(1) | ||
|
||
// Write existing data in database | ||
val existingData = Seq( | ||
TestData(`uid` = "a", `oid` = "a", `chromosome` = "1", `createdOn` = Timestamp.valueOf(day1), `updatedOn` = Timestamp.valueOf(day1), `data` = 1), | ||
TestData(`uid` = "aa", `oid` = "aa", `chromosome` = "2", `createdOn` = Timestamp.valueOf(day1), `updatedOn` = Timestamp.valueOf(day1), `data` = 1), | ||
) | ||
JdbcLoader.writeOnce("", schemaName, tableName, existingData.toDF(), List(), "jdbc", options) | ||
|
||
// Check existing data was written in the database | ||
val existingDfInPsql = JdbcLoader.read("", "jdbc", readOptions, Some(schemaName), Some(tableName)) | ||
existingDfInPsql | ||
.as[TestData] | ||
.collect() should contain theSameElementsAs existingData | ||
|
||
// Upsert data | ||
val upsertData = Seq( | ||
TestData(`uid` = "aa", `oid` = "aa", `chromosome` = "1", `createdOn` = Timestamp.valueOf(day1), `updatedOn` = Timestamp.valueOf(day2), `data` = 2), // update | ||
TestData(`uid` = "aaa", `oid` = "aaa", `chromosome` = "3", `createdOn` = Timestamp.valueOf(day2), `updatedOn` = Timestamp.valueOf(day2), `data` = 2), // insert | ||
) | ||
val upsertResult = JdbcLoader.upsert("", schemaName, tableName, upsertData.toDF(), primaryKeys = Seq("uid", "oid"), List(), "jdbc", options) | ||
val updatedDfInPsql = JdbcLoader.read("", "jdbc", readOptions, Some(schemaName), Some(tableName)) | ||
|
||
val expectedData = Seq( | ||
TestData(`uid` = "a", `oid` = "a", `chromosome` = "1", `createdOn` = Timestamp.valueOf(day1), `updatedOn` = Timestamp.valueOf(day1), `data` = 1), | ||
TestData(`uid` = "aa", `oid` = "aa", `chromosome` = "1", `createdOn` = Timestamp.valueOf(day1), `updatedOn` = Timestamp.valueOf(day2), `data` = 2), | ||
TestData(`uid` = "aaa", `oid` = "aaa", `chromosome` = "3", `createdOn` = Timestamp.valueOf(day2), `updatedOn` = Timestamp.valueOf(day2), `data` = 2), | ||
) | ||
|
||
upsertResult | ||
.as[TestData] | ||
.collect() should contain theSameElementsAs expectedData | ||
|
||
updatedDfInPsql | ||
.as[TestData] | ||
.collect() should contain theSameElementsAs expectedData | ||
} | ||
} | ||
} |