Built with Tutify.io

    AIStudyLabs

    Spark with SQL Server - Read and Write Table

    Spark with SQL Server - Read and Write Table

    Can we connect to SQL Server (mssql) from Spark and read the table into Spark DataFrame and write the DataFrame to the SQL table?

    In order to connect to SQL-server (mssql) from Apache Spark, you would need the following. Make sure you have these details before you read or write to the SQL server. The driver I am going to use in this article is com.microsoft.sqlserver.jdbc.spark

    Steps to connect Spark to SQL Server and Read and write Table.

    <a id="h-1-spark-connector-for-sql-server-mssql"></a>

    1. Spark Connector for SQL Server (mssql)

    Apache Spark connector for SQL server works with both

    Following are the available connectors, based on the Spark version you are using, you should choose the appropriate version.

    | Spark Version | Maven dependency | | --- | --- | | Spark 2.4.x | groupeId : com.microsoft.azure artifactId : spark-mssql-connector version : 1.0.2 | | Spark 3.0.x | groupeId : com.microsoft.azure artifactId : spark-mssql-connector_2.12 version : 1.1.0 | | Spark 3.1.x | groupeId : com.microsoft.azure spark-mssql-connector_2.12 version : 1.2.0 |

    You can find the all available Spark Microsoft SQL (Mssql) connectors at https://search.maven.org/search?q=spark-mssql-connector

    2. Maven Dependency

    If you are using Maven, add the below dependency to your pom.xml. For my example, I am using Spark 3.0 and Scala 2.12 hence I chose the following dependency.

    
    <dependency>
      <groupId>com.microsoft.azure</groupId>
      <artifactId>spark-mssql-connector_2.12_3.0</artifactId>
      <version>1.0.0-alpha</version>
    </dependency>
    

    3. Create Spark Session & DataFrame

    Creating a SparkSession is a basic step to work with Spark hence, first, let's create a SparkSession and construct a sample DataFrame with columns id, name, age and gender. In the below sections, I will use this DataFrame to write into SQL Server table and read from it.

    
    import org.apache.spark.sql.SparkSession
    
    // Create SparkSession in spark 2.x or later
    val spark = SparkSession.builder().master("local[*]")
        .appName("AIStudyLabs.com")
        .getOrCreate()
    
    import spark.implicits._
    
    // Create DataFrame
    val sampleDF = Seq((1, "James",30,"M"),
        (2, "Ann",40,"F"), (3, "Jeff",41,"M"),
        (4, "Jennifer",20,"F")
      ).toDF("id", "name","age","gender")
    

    4. Write Spark DataFrame to SQL Server Table

    You would be required to provide the SQL Server details while writing DataFrame to SQL table. By using the format() specify the driver, this driver is defined in the connector dependency.

    Some points to note while writing.

    
    // Write to SQL Table
    sampleDF.write
      .format("com.microsoft.sqlserver.jdbc.spark")
      .mode("overwrite")
      .option("url", "jdbc:sqlserver://{SERVER_ADDR};databaseName=emp;")
      .option("dbtable", "employee")
      .option("user", "replace_user_name")
      .option("password", "replace_password")
      .save()
    

    Here, used mode("overwrite") means if the table already exists with rows, overwrite the table with the rows from the DataFrame. The overwrite mode first drops the table if it already exists in the database.

    5. Read SQL Server Table to Spark DataFrame

    Similar to writing, with read() also you need to provide the driver and the SQL connection details. In the below example, I am reading a table employee from the database emp to the DataFrame.

    
    // Read from SQL Table
    val df = spark.read
      .format("com.microsoft.sqlserver.jdbc.spark")
      .option("url", "jdbc:sqlserver://{SERVER_ADDR};databaseName=emp;")
      .option("dbtable", "employee")
      .option("user", "replace_user_name")
      .option("password", "replace_password")
      .load()
    
    df.show()
    

    Yields below output.

    6. Select Specific Columns to Read

    In the above example, it reads the entire table into Spark DataFrame. Sometimes you may not be required to select the entire table, so to select the specific columns, specify the query you wanted to select with dbtable option.

    
    // Read from SQL Table
    val df = spark.read
      .format("com.microsoft.sqlserver.jdbc.spark")
      .option("url", "jdbc:sqlserver://{SERVER_ADDR};databaseName=emp;")
      .option("dbtable", "select id,age from employee where gender='M'")
      .option("user", "replace_user_name")
      .option("password", "replace_password")
      .load()
    
    df.show()
    

    7. Append Table

    Use spark.write.mode("append") to append the rows to the existing SQL Server table.

    
    // Write to SQL Table
    sampleDF.write
      .format("com.microsoft.sqlserver.jdbc.spark")
      .mode("append")
      .option("url", "jdbc:sqlserver://{SERVER_ADDR};databaseName=emp;")
      .option("dbtable", "employee")
      .option("user", "replace_user_name")
      .option("password", "replace_password")
      .save()
    

    8. Complete Example

    Following is the complete example of how to connect to SQL server or Azure SQL from Spark and read/write the table.

    
    import org.apache.spark.sql.SparkSession
    
    object ReadSQLTable extends App{
    
      // Create SparkSession
      val spark = SparkSession.builder().master("local[*]")
        .appName("AIStudyLabs.com")
        .getOrCreate()
    
      import spark.implicits._
    
      // Create DataFrame
      val sampleDF = Seq((1, "James",30,"M"),
        (2, "Ann",40,"F"), (3, "Jeff",41,"M"),
        (4, "Jennifer",20,"F")
      ).toDF("id", "name","age","gender")
    
      // Write to SQL Table
      sampleDF.write
      .format("com.microsoft.sqlserver.jdbc.spark")
      .mode("overwrite")
      .option("url", "jdbc:sqlserver://{SERVER_ADDR};databaseName=emp;")
      .option("dbtable", "employee")
      .option("user", "replace_user_name")
      .option("password", "replace_password")
      .save()
    
      // Read from SQL Table
      val df = spark.read
      .format("com.microsoft.sqlserver.jdbc.spark")
      .option("url", "jdbc:sqlserver://{SERVER_ADDR};databaseName=emp;")
      .option("dbtable", "employee")
      .option("user", "replace_user_name")
      .option("password", "replace_password").load()
    
      // Show sample records from data frame
      df.show(5)
    }
    

    Conclusion

    In this article, you have learned how to connect to an SQL server from Spark and write the DataFrame to SQL table and read the table into DataFrame with examples.

    Related Articles

    References