Forum Discussion

chetanhiwale's avatar
chetanhiwale
Resolver I
6 months ago
Solved

Query data from Fabric SQL DB using spark / python.

Hi Folks,  I am trying to query data using Fabric SQL DB using python / pyspark. But I am not getting proper syntax to query. If anyone has , can someone share the syntax.  Thanks and regards.
  • Tamanchu's avatar
    6 months ago

    Hi chetanhiwale ,

    You can query a Fabric SQL Database from a Spark notebook using JDBC. Here's a working example:

    jdbc_url = "jdbc:sqlserver://<your-server>.database.fabric.microsoft.com:1433;database=<your-db>"

    df = spark.read \
        .format("jdbc") \
        .option("url", jdbc_url) \
        .option("dbtable", "dbo.YourTable") \
        .option("authentication", "ActiveDirectoryInteractive") \
        .load()
    df.show()
    Alternatively, if your SQL DB is in the same workspace, you can use the synapsesql connector:
    df = spark.read.synapsesql("YourDatabase.dbo.YourTable")

    Did this help?
    Drop a kudo so others can find it ! 

    😉