cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I specify column types when writing to a MSSQL server using the JDBC driver (

jonathan-dufaul
Valued Contributor

I have a pyspark dataframe that I'm writing to an on-prem MSSQL server--it's a stopgap while we convert data warehousing jobs over to databricks.

The processes that use those tables in the on-prem server rely on the tables maintaining the identical structure. I have a structure, for instance, like below.

SOURCE	VARCHAR(3)
LOCATION_NO	VARCHAR(3)
SKU_NO	LONG
CASE_SKU_NO	LONG
ITEM_TYPE	VARCHAR(3)
SYSTEM	VARCHAR(MAX)
PRICE	DOUBLE

when I've just done vanilla

  (my_dataframe.write.format("jdbc") 
    .option("url",sqlsUrl) 
    .option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver") 
    .option("dbtable", table_name )
    .option("user", username) 
    .option("password", password) 
    .save(mode=mode)
  )

I get a different structure, biggest difference being varchar becomes nvarchar(max). Since the jobs on the server join to other tables that have varchar type, it means that jobs take hours that used to take minutes.

Ideally I'd like to specify the schema type and then save the table.

Follow up question. Am I using the most recent/correct JDBC driver?

I hope that makes sense.

2 REPLIES 2

dasanro
New Contributor II

It's happenging to me too!

Did you find any solution @jonathan-dufaul  ?

Thanks!!

I think I ended up doing a truncate and mode = "append" so the structure stayed the same.

For truncate I had to get some direct access from the sparkcontext/sc variable

driver_manager = spark._sc._gateway.jvm.java.sql.DriverManager
connection = driver_manager.getConnection(mssql_url, mssql_user, mssql_pass)
connection.prepareCall("TRUNCATE TABLE my_table").execute()
connection.close()

 

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now