Question: Why the following code is complaining about differing column nullable configurations of Hiring_Date column in df vs. SQL Table. What I may be missing here? The error occurs at line df3.write where df3 is supposed to be written to a SQL table
ERROR: ava.sql.SQLException: Spark Dataframe and SQL Server table have differing column nullable configurations at column index 0 DF col Hiring_Date nullable config is true Table col Hiring_Date nullable config is false
Remarks: From I understand (and it has worked in my other scripts), once you define a data type of a column in pyspark and set its null values to some value, that column in df is not nullable.
df = spark.read.csv("myDataFile.txt", sep="|", header="true", inferSchema="false")
df1 = df.select( *[ F.when(F.col(column).isNull(),'').otherwise(F.col(column)).alias(column) for column in df.columns])
df2 = df1.withColumn("Hiring_Date", df1.Hiring_Date.cast(TimestampType())) \
.withColumn("Hiring_Fee", df1.Hiring_Fee.cast(DoubleType()))
df3 = df2.fillna( {'Hiring_Fee' : 0, 'Hiring_Date': '1753-01-01 00:00:00.000'} )
try:
df3.write \
.format("com.microsoft.sqlserver.jdbc.spark") \
.mode("append") \
.option("url", url) \
.option("dbtable", table_name) \
.option("user", myUserName) \
.option("password", myPassword) \
.save()
except ValueError as error :
print("Connector write failed", error)
SQL Server Table definition:
CREATE TABLE HR_History(
Hiring_Date datetime NOT NULL,
Hiring_Fee float NOT NULL
)