I need to extract a table from Teradata (read-only access) to parquet with Scala (2.11) / Spark (2.1.0). I'm building a dataframe that I can load successfully
val df = spark.read.format("jdbc").options(options).load()
But df.show
gives me a NullPointerException:
java.lang.NullPointerException
at org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter.write(UnsafeRowWriter.java:210)
I did a df.printSchema
and I found out that the reason for this NPE is that the dataset contains null
values for (nullable = false)
columns (it looks like Teradata is giving me wrong information). Indeed, I can achieve a df.show
if I drop the problematic columns.
So, I tried specifying a new schema with all columns set to (nullable = true)
:
val new_schema = StructType(df.schema.map {
case StructField(n,d,nu,m) => StructField(n,d,true,m)
})
val new_df = spark.read.format("jdbc").schema(new_schema).options(options).load()
But then I got:
org.apache.spark.sql.AnalysisException: JDBC does not allow user-specified schemas.;
I also tried to create a new Dataframe from the previous one, specifying the wanted schema:
val new_df = df.sqlContext.createDataFrame(df.rdd, new_schema)
But I still got an NPE when taking action on the dataframe.
Any idea on how I could fix this?