Azure Synapse Analytics - Connect to Serverless SQL Pool via Spark using Managed Identity

Graeme Cash 76 Reputation points
2024-02-05T12:38:24.3166667+00:00

Hello Is it possible to connect to the serverless sql pool via Spark in Synapse using managed identity? I need to run queries against the serverless pool and have tried pyodbc. I can run queries using a SQL logon but when I try managed identity I get a timeout... OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

driver = '{ODBC Driver 18 for SQL Server}'

cnxn = pyodbc.connect(f'Driver={driver};Server={server};Database={database};Port=1433;Authentication=ActiveDirectoryMsi')

I have also configured the Spark session to run as managed identity... User's image Thanks in advance

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
{count} votes

Answer accepted by question author
  1. Anonymous
    2024-02-09T05:01:50.06+00:00

    @Graeme Cash
    Welcome to Microsoft Q&A platform and thanks for posting your question. I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others "I'll repost your solution in case you'd like to accept the answer.

    • Ask: Hello Is it possible to connect to the serverless sql pool via Spark in Synapse using managed identity? I need to run queries against the serverless pool and have tried pyodbc. I can run queries using a SQL logon but when I try managed identity I get a timeout... OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
    driver = '{ODBC Driver 18 for SQL Server}'
    
    cnxn = pyodbc.connect(f'Driver={driver};Server={server};Database={database};Port=1433;Authentication=ActiveDirectoryMsi')
    

    I have also configured the Spark session to run as managed identity...

    • Solution: Hello, I have managed to get it working with pyodbc by passing the connection string from the linked service to the pyodbc.connect using attrs_before...
    token = mssparkutils.credentials.getConnectionStringOrCreds(linkedService=linked_service)
    
    exptoken = b""
    for i in token:
    	exptoken += bytes(i,'utf-8');
        exptoken += bytes(1);
    tokenstruct = struct.pack("=i", len(exptoken)) + exptoken;
    
    pyodbc.connect(f'Driver={DRIVER};Server={server};Database={database};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;', attrs_before = { 1256:tokenstruct })
    
    
    

    Graeme.

    If I missed anything please let me know and I'd be happy to add it to my answer, or feel free to comment below with any additional information. If you have any other questions, please let me know. Thank you again for your time and patience throughout this issue.

    ---Please don’t forget to Accept Answer and Yes for "was this answer helpful" wherever the information provided helps you, this can be beneficial to other community members.


1 additional answer

Sort by: Most helpful
  1. Graeme Cash 76 Reputation points
    2024-02-08T09:53:22.81+00:00

    Hello I have managed to get it working with pyodbc by passing the connection string from the linked service to the pyodbc.connect using attrs_before...

    token = mssparkutils.credentials.getConnectionStringOrCreds(linkedService=linked_service)
    
    exptoken = b""
    for i in token:
    	exptoken += bytes(i,'utf-8');
        exptoken += bytes(1);
    tokenstruct = struct.pack("=i", len(exptoken)) + exptoken;
    
    pyodbc.connect(f'Driver={DRIVER};Server={server};Database={database};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;', attrs_before = { 1256:tokenstruct })
    
    
    

    Graeme


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.