Python API to change a sql dataset connection

Solved!
JohnB
Level 3
Python API to change a sql dataset connection

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

0 Kudos
1 Solution
sergeyd
Dataiker
Dataiker

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()

 

View solution in original post

0 Kudos
2 Replies
sergeyd
Dataiker
Dataiker

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()

 

View solution in original post

0 Kudos
JohnB
Level 3
Author

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

 

 

0 Kudos
Labels (1)
A banner prompting to get Dataiku DSS
Public