spark-testing-base를 사용해서 DataFrame의 일치를 확인하는 과정에서
DataFrame의 SchemaField의 nullable
의 값이 틀려도 assert
가 발생한다.
해결하는 방법은 아래와 같이 setNullableStateOfColumnn
을 SparkUtil.scala
에 작성한 뒤에
특정 컬럼의 NullableState를 변경하면 끝
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
import org.apache.spark.sql.types._ | |
object SparkUtil { | |
def setNullableStateOfColumn( df: DataFrame, cn: String, nullable: Boolean) : DataFrame = { | |
// get schema | |
val schema = df.schema | |
// modify [[StructField] with name `cn` | |
val newSchema = StructType(schema.map { | |
case StructField( c, t, _, m) if c.equals(cn) => StructField( c, t, nullable = nullable, m) | |
case y: StructField => y | |
}) | |
// apply new schema | |
df.sqlContext.createDataFrame( df.rdd, newSchema ) | |
} | |
} | |
// Usage | |
SparkUtil.setNullableStateOfColumn(df, "col", true) |
'우리는 개발자 > Data Engineering' 카테고리의 다른 글
[Spark] spark-testing-base에서 DataFrameSuiteBase 사용 (0) | 2020.02.28 |
---|---|
[Spark] None, null? DataFrame 생성시에 java.lang.ClassNotFoundException: scala.Any (0) | 2020.02.28 |
[Spark] scala DataFrame 생성하기 for 예제 (0) | 2020.02.27 |
[Spark] UserDefinedFunction (udf) 구현하는 방법 (+예제코드) (0) | 2020.02.27 |
[Spark] Scala joda Datetime 사용하는 방법 (+예제코드) - Days, DateTimeFormat, DateTime (0) | 2020.02.27 |