Prerequisites
- Set up your Azure Synapse Analytics workspace set up
- You need to have the necessary permissions to access the Synapse workspace. You can use a service principal or Azure AD authentication.
- Python installed on your local machine.
You need to install the necessary Python libraries. You can do this using pip:
pip install pyodbc
pip install azure-identity
pip install azure-synapse
If you are using Azure AD authentication, you can use the azure-identity
library to authenticate.
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
You can use the pyodbc
library to connect to Azure Synapse Analytics. Below is an example of how to set up the connection and run a query.
import pyodbc
# Connection parameters
server = '<your-synapse-workspace>.sql.azuresynapse.net'
database = 'default'
username = '<your-username>'
password = '<your-password>'
driver= '{ODBC Driver 17 for SQL Server}'
# Connection string
conn_str = f'DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password}'
# Establish the connection
conn = pyodbc.connect(conn_str)
# Create a cursor
cursor = conn.cursor()
# Execute a query
query = "SELECT * FROM abc"
cursor.execute(query)
# Fetch and print the results
rows = cursor.fetchall()
for row in rows:
print(row)
# Close the connection
cursor.close()
conn.close()
If you prefer using the Azure Synapse SDK, you can use the azure-synapse
library to interact with your Synapse workspace.
from azure.synapse import SynapseClient
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
synapse_client = SynapseClient(credential, '<your-subscription-id>', '<your-resource-group>', '<your-synapse-workspace>')
query = "SELECT * FROM abc"
result = synapse_client.sql_pools.execute('<your-sql-pool-name>', query)
for row in result:
print(row)