Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST:144 - Add provision to pass schema to the 'ConvertJSONStringToStruct' transformation #7

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
.metals
.venv
.vscode
.bsp
.idea

project
target
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ThisBuild / scalaVersion := "3.3.0"

ThisBuild / organization := "com.clairvoyant.data.scalaxy"

ThisBuild / version := "1.1.0"
ThisBuild / version := "1.2.0"

ThisBuild / resolvers ++= Seq(
"DataScalaxyTestUtil Repo" at "https://maven.pkg.github.com/teamclairvoyant/data-scalaxy-test-util"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,18 +448,30 @@ object DataFrameTransformerImplicits {
*
* @param columnName
* Name of the column to be converted
* @param schemaDDL
* The Data Definition Language (DDL) for the column
* @return
* DataFrame with the column converted to struct type
*/
def convertJSONStringToStruct(
columnName: String
columnName: String,
schemaDDL: Option[String] = None
): DataFrame =
import df.sparkSession.implicits.*

val schema =
schemaDDL match {
case Some(schemaDDL) =>
DataType.fromDDL(schemaDDL)
case None =>
df.sparkSession.read.json(df.select(columnName).as[String]).schema
}

df.withColumn(
columnName,
from_json(
col(columnName),
df.sparkSession.read.json(df.select(columnName).as[String]).schema
schema
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,46 @@ class DataFrameTransformerImplicitsSpec extends DataFrameReader with DataFrameMa
actualDF should matchExpectedDataFrame(expectedDF)
}

"convertJSONStringToStruct() - with columnName and schemaDDL" should "convert the specified column to Struct Type" in {
val df = readJSONFromText(
"""
|{
| "col_A": "val_A",
| "col_B": "{\"col_C\": \"val_C\",\"col_D\": 5}"
|}
|""".stripMargin
)

val actualDF = df.convertJSONStringToStruct(
columnName = "col_B",
schemaDDL = Some("col_C STRING, col_D STRING")
)

val expectedDF = readJSONFromText(
"""
|{
| "col_A": "val_A",
| "col_B": {
| "col_C": "val_C",
| "col_D": "5"
| }
|}
|""".stripMargin
)

actualDF.schema.fields
.filter(_.name == "col_B")
.head
.dataType shouldBe StructType(
List(
StructField("col_C", StringType),
StructField("col_D", StringType)
)
)

actualDF should matchExpectedDataFrame(expectedDF)
}

"flattenSchema()" should "flatten the dataframe" in {
val df = readJSONFromText(
"""
Expand Down