Python API to change a sql dataset connection

Options
JohnB
JohnB Registered Posts: 32 ✭✭✭✭✭

Can I use Python to change a SQL Server dataset connection?

import dataiku
client = dataiku.api_client()
project = client.get_default_project()


dataset = project.get_dataset('mysqldataset')
settings = dataset.get_settings()
print(settings.get_raw_params()['connection']) # gives the current connection

..how can I change the connection only?

a method such as settings.set_connection() would be handy

Tagged:

Best Answer

  • Sergey
    Sergey Dataiker, Dataiku DSS Core Designer, Dataiku DSS & SQL, Dataiku DSS Core Concepts Posts: 365 Dataiker
    edited July 17 Answer ✓
    Options

    Hi @JohnB

    Can you try this? This will replace CONNECTION_TO_REPLACE with NEW_CONNECTION for all datasets within the current project.

    import dataiku
    
    client = dataiku.api_client()
    project = client.get_default_project()
    
    for dataset in project.list_datasets(as_type="objects"):
        settings = dataset.get_settings()
        if settings.get_raw()["managed"] and settings.get_raw_params().get("connection", None) == "CONNECTION_TO_REPLACE":
            settings.get_raw_params()["connection"] = "NEW_CONNECTION"
            settings.save()

Answers

  • JohnB
    JohnB Registered Posts: 32 ✭✭✭✭✭
    Options

    Hi Sergey,

    That works well.

    I wasn't aware that this line would change the original settings object:

    settings.get_raw_params()["connection"] = "NEW_CONNECTION"

    Thanks!

    John

  • Yasmine
    Yasmine Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 14 ✭✭✭
    Options

    hello,

    I need to access the settings of a SQL dataset > Advanced > SQL table < Table creation SQL and modify the SQL script. How can I do this in python please?

    Kind regards

Setup Info
    Tags
      Help me…