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

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
3 Replies
sergeyd
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()

 

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
Yasmine
Level 3

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

0 Kudos