From 875400890851ea7a41125b20105e19f1eb0227f8 Mon Sep 17 00:00:00 2001 From: fpatin Date: Wed, 1 Feb 2023 14:28:21 +0100 Subject: [PATCH] Remove runtime call (#6) * Chore: Fix CustomPrettifier object name * Create SparkShowString to enable access to DataFrame#showString * build.sbt: change release version * README.md: change release version --- README.md | 2 +- build.sbt | 2 +- .../data/sparktest/CustomPrettifier.scala | 19 +++++-------------- .../apache/spark/sql/SparkShowString.scala | 7 +++++++ .../data/sparktest/CustomPrettifierSpec.scala | 2 +- 5 files changed, 15 insertions(+), 17 deletions(-) create mode 100644 src/main/scala/org/apache/spark/sql/SparkShowString.scala diff --git a/README.md b/README.md index eaae264..15d6ae5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# SparkTest - 0.2.0 +# SparkTest - 0.3.0 **SparkTest** is a Scala library for unit testing with [Spark](https://github.com/apache/spark). For now, it is only made for DataFrames. diff --git a/build.sbt b/build.sbt index 1e5224e..62d5c11 100644 --- a/build.sbt +++ b/build.sbt @@ -1,5 +1,5 @@ ThisBuild / organization := "com.bedrockstreaming" -ThisBuild / version := "0.2.0" +ThisBuild / version := "0.3.0" ThisBuild / scalaVersion := "2.12.11" // ******** diff --git a/src/main/scala/com/bedrockstreaming/data/sparktest/CustomPrettifier.scala b/src/main/scala/com/bedrockstreaming/data/sparktest/CustomPrettifier.scala index bf2b019..99808af 100644 --- a/src/main/scala/com/bedrockstreaming/data/sparktest/CustomPrettifier.scala +++ b/src/main/scala/com/bedrockstreaming/data/sparktest/CustomPrettifier.scala @@ -1,11 +1,9 @@ package com.bedrockstreaming.data.sparktest -import org.apache.spark.sql.DataFrame +import com.bedrockstreaming.data.sparktest.CustomPrettifier.prettyDataFrame import org.apache.spark.sql.functions.col +import org.apache.spark.sql.{ DataFrame, SparkShowString } import org.scalactic.Prettifier -import java.lang.reflect.Method - -import CustomerPrettifier.prettyDataFrame trait CustomPrettifier { @@ -15,7 +13,7 @@ trait CustomPrettifier { } } -object CustomerPrettifier { +object CustomPrettifier { private[sparktest] def prettyDataFrame(df: DataFrame): String = { val schemaTitle = @@ -48,13 +46,6 @@ object CustomerPrettifier { numRows: Int = Int.MaxValue, truncate: Int = 0, vertical: Boolean = false - ): String = { - val methodName = "showString" - val method: Method = - df.getClass.getDeclaredMethod(methodName, numRows.getClass, truncate.getClass, vertical.getClass) - method.setAccessible(true) - method - .invoke(df, numRows.asInstanceOf[Object], truncate.asInstanceOf[Object], vertical.asInstanceOf[Object]) - .asInstanceOf[String] - } + ): String = + SparkShowString(df, numRows, truncate, vertical) } diff --git a/src/main/scala/org/apache/spark/sql/SparkShowString.scala b/src/main/scala/org/apache/spark/sql/SparkShowString.scala new file mode 100644 index 0000000..e0e5de7 --- /dev/null +++ b/src/main/scala/org/apache/spark/sql/SparkShowString.scala @@ -0,0 +1,7 @@ +package org.apache.spark.sql + +object SparkShowString { + + def apply(df: DataFrame, numRows: Int = Int.MaxValue, truncate: Int = 0, vertical: Boolean = false): String = + df.showString(numRows, truncate, vertical) +} diff --git a/src/test/scala/com/bedrockstreaming/data/sparktest/CustomPrettifierSpec.scala b/src/test/scala/com/bedrockstreaming/data/sparktest/CustomPrettifierSpec.scala index 61496d6..44c342a 100644 --- a/src/test/scala/com/bedrockstreaming/data/sparktest/CustomPrettifierSpec.scala +++ b/src/test/scala/com/bedrockstreaming/data/sparktest/CustomPrettifierSpec.scala @@ -1,6 +1,6 @@ package com.bedrockstreaming.data.sparktest -import com.bedrockstreaming.data.sparktest.CustomerPrettifier.prettyDataFrame +import com.bedrockstreaming.data.sparktest.CustomPrettifier.prettyDataFrame import com.bedrockstreaming.data.sparktest.SparkTestTools.SparkSessionOps import org.apache.spark.sql.types.{ IntegerType, LongType, StringType } import org.scalatest.flatspec.AnyFlatSpec