How to upload(write) data(dataframe) to azure SQL datastore from azure machine learning(azureML) using SDK

Jothy Babu 26 Reputation points
2020-08-05T07:34:23.487+00:00

From the documentation I could find ways to read data from Azure SQL database registered as datastore in azureML,but not ways to upload or write output data to azure SQL database from azureML.Can anyone please guide me on the same? Also can SQL datastore be used as output for the batch inference step

Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
2,868 questions
{count} votes

3 answers

Sort by: Most helpful
  1. GiftA-MSFT 11,166 Reputation points
    2020-08-08T00:11:04.25+00:00

    Thanks for reaching out. You need to register your storage as a datastore. Then write dataframe to a local file and upload to datastore as shown below (refer to this post as well):

    from azureml.core import Workspace, Dataset  
      
    subscription_id = 'id'  
    resource_group = 'resource group'  
    workspace_name = 'workspace name'  
      
    ws = Workspace(subscription_id, resource_group, workspace_name)  
      
    #write dataframe to a local file (e.g. csv, parquet)  
    local_path = 'data/prepared.csv'  
    df.to_csv(local_path)  
      
    # get the datastore to upload prepared data  
    datastore = ws.get_default_datastore()  
      
    # upload the local file from src_dir to the target_path in datastore  
    datastore.upload(src_dir='data', target_path='data')  
    

    If you continue to experience errors, please share your code so we can investigate further, thanks.


  2. Ramr-msft 17,736 Reputation points
    2020-08-26T03:52:48.503+00:00

    @Jothy Babu Thanks, Please follow the below repo and docker-instructions to use tools such as pyodbc and SQLAlchemy for writeback within an experiment, ParallelRunStep, etc without the need for multi-step pipelines.

    Here is the repo for write to sql.

    20383-docker-instructions.pdf


  3. Ramr-msft 17,736 Reputation points
    2020-08-31T06:31:43.787+00:00

    Thanks, Please follow the below pointers for Reading and updating Azure SQL Database from Azure ML pipeline.
    Doc: https://learn.microsoft.com/en-us/python/api/azureml-pipeline-steps/azureml.pipeline.steps.data_transfer_step.datatransferstep?view=azure-ml-py
    Example: https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/machine-learning-pipelines/intro-to-pipelines/aml-pipelines-data-transfer.ipynb

    Note: When copying data to an Azure SQL Database, data will be appended to an existing table. We also expect the source file to have a header row and the names should exactly match with column names in destination table.


Your answer

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