|
| 1 | +# Table of Contents |
| 2 | +1. [Introduction](#introduction) |
| 3 | +1. [Setup using Azure Data Studio](#azuredatastudiosetup) |
| 4 | +1. [Energy demand example using Azure Data Studio](#azuredatastudioenergydemand) |
| 5 | +1. [Set using SQL Server Management Studio for SQL Server 2017 on Windows](#ssms2017) |
| 6 | +1. [Set using SQL Server Management Studio for SQL Server 2019 on Linux](#ssms2019) |
| 7 | +1. [Energy demand example using SQL Server Management Studio](#ssmsenergydemand) |
| 8 | + |
| 9 | + |
| 10 | +<a name="introduction"></a> |
| 11 | +# Introduction |
| 12 | +SQL Server 2017 or 2019 can call Azure ML automated machine learning to create models trained on data from SQL Server. |
| 13 | +This uses the sp_execute_external_script stored procedure, which can call Python scripts. |
| 14 | +SQL Server 2017 and SQL Server 2019 can both run on Windows or Linux. |
| 15 | +However, this integration is not available for SQL Server 2017 on Linux. |
| 16 | + |
| 17 | +This folder shows how to setup the integration and has a sample that uses the integration to train and predict based on an energy demand dataset. |
| 18 | + |
| 19 | +This integration is part of SQL Server and so can be used from any SQL client. |
| 20 | +These instructions show using it from Azure Data Studio or SQL Server Managment Studio. |
| 21 | + |
| 22 | +<a name="azuredatastudiosetup"></a> |
| 23 | +## Setup using Azure Data Studio |
| 24 | + |
| 25 | +These step show setting up the integration using Azure Data Studio. |
| 26 | + |
| 27 | +1. If you don't already have SQL Server, you can install it from [https://www.microsoft.com/en-us/sql-server/sql-server-downloads](https://www.microsoft.com/en-us/sql-server/sql-server-downloads) |
| 28 | +1. Install Azure Data Studio from [https://docs.microsoft.com/en-us/sql/azure-data-studio/download?view=sql-server-2017](https://docs.microsoft.com/en-us/sql/azure-data-studio/download?view=sql-server-2017) |
| 29 | +1. Start Azure Data Studio and connect to SQL Server. [https://docs.microsoft.com/en-us/sql/azure-data-studio/sql-notebooks?view=sql-server-2017](https://docs.microsoft.com/en-us/sql/azure-data-studio/sql-notebooks?view=sql-server-2017) |
| 30 | +1. Create a database named "automl". |
| 31 | +1. Open the notebook how-to-use-azureml\automated-machine-learning\sql-server\setup\auto-ml-sql-setup.ipynb and follow the instructions in it. |
| 32 | + |
| 33 | + <a name="azuredatastudioenergydemand"></a> |
| 34 | +## Energy demand example using Azure Data Studio |
| 35 | + |
| 36 | +Once you have completed the setup, you can try the energy demand sample in the notebook energy-demand\auto-ml-sql-energy-demand.ipynb. |
| 37 | +This has cells to train a model, predict based on the model and show metrics for each pipeline run in training the model. |
| 38 | + |
| 39 | +<a name="ssms2017"></a> |
| 40 | +## Setup using SQL Server Management Studio for SQL Server 2017 on Windows |
| 41 | + |
| 42 | +These instruction setup the integration for SQL Server 2017 on Windows. |
| 43 | + |
| 44 | +1. If you don't already have SQL Server, you can install it from [https://www.microsoft.com/en-us/sql-server/sql-server-downloads](https://www.microsoft.com/en-us/sql-server/sql-server-downloads) |
| 45 | +2. Enable external scripts with the following commands: |
| 46 | +```sh |
| 47 | + sp_configure 'external scripts enabled',1 |
| 48 | + reconfigure with override |
| 49 | +``` |
| 50 | +3. Stop SQL Server. |
| 51 | +4. Install the automated machine learning libraries using the following commands from Administrator command prompt (If you are using a non-default SQL Server instance name, replace MSSQLSERVER in the second command with the instance name) |
| 52 | +```sh |
| 53 | + cd "C:\Program Files\Microsoft SQL Server" |
| 54 | + cd "MSSQL14.MSSQLSERVER\PYTHON_SERVICES" |
| 55 | + python.exe -m pip install azureml-sdk[automl] |
| 56 | + python.exe -m pip install --upgrade numpy |
| 57 | + python.exe -m pip install --upgrade sklearn |
| 58 | +``` |
| 59 | +5. Start SQL Server and the service "SQL Server Launchpad service". |
| 60 | +6. In Windows Firewall, click on advanced settings and in Outbound Rules, disable "Block network access for R local user accounts in SQL Server instance xxxx". |
| 61 | +7. Execute the files in the setup folder in SQL Server Management Studio: aml_model.sql, aml_connection.sql, AutoMLGetMetrics.sql, AutoMLPredict.sql and AutoMLTrain.sql |
| 62 | +8. Create an Azure Machine Learning Workspace. You can use the instructions at: [https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-manage-workspace ](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-manage-workspace) |
| 63 | +9. Create a config.json file file using the subscription id, resource group name and workspace name that you used to create the workspace. The file is described at: [https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-configure-environment#workspace](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-configure-environment#workspace) |
| 64 | +10. Create an Azure service principal. You can do this with the commands: |
| 65 | +```sh |
| 66 | + az login |
| 67 | + az account set --subscription subscriptionid |
| 68 | + az ad sp create-for-rbac --name principlename --password password |
| 69 | +``` |
| 70 | +11. Insert the values \<tenant\>, \<AppId\> and \<password\> returned by create-for-rbac above into the aml_connection table. Set \<path\> as the absolute path to your config.json file. Set the name to “Default”. |
| 71 | + |
| 72 | +<a name="ssms2019"></a> |
| 73 | +## Setup using SQL Server Management Studio for SQL Server 2019 on Linux |
| 74 | +1. Install SQL Server 2019 from: [https://www.microsoft.com/en-us/sql-server/sql-server-downloads](https://www.microsoft.com/en-us/sql-server/sql-server-downloads) |
| 75 | +2. Install machine learning support from: [https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-machine-learning?view=sqlallproducts-allversions#ubuntu](https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-machine-learning?view=sqlallproducts-allversions#ubuntu) |
| 76 | +3. Then install SQL Server management Studio from [https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-2017](https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-2017) |
| 77 | +4. Enable external scripts with the following commands: |
| 78 | +```sh |
| 79 | + sp_configure 'external scripts enabled',1 |
| 80 | + reconfigure with override |
| 81 | +``` |
| 82 | +5. Stop SQL Server. |
| 83 | +6. Install the automated machine learning libraries using the following commands from Administrator command (If you are using a non-default SQL Server instance name, replace MSSQLSERVER in the second command with the instance name): |
| 84 | +```sh |
| 85 | + sudo /opt/mssql/mlservices/bin/python/python -m pip install azureml-sdk[automl] |
| 86 | + sudo /opt/mssql/mlservices/bin/python/python -m pip install --upgrade numpy |
| 87 | + sudo /opt/mssql/mlservices/bin/python/python -m pip install --upgrade sklearn |
| 88 | +``` |
| 89 | +7. Start SQL Server. |
| 90 | +8. Execute the files aml_model.sql, aml_connection.sql, AutoMLGetMetrics.sql, AutoMLPredict.sql and AutoMLTrain.sql in SQL Server Management Studio. |
| 91 | +9. Create an Azure Machine Learning Workspace. You can use the instructions at: [https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-manage-workspace](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-manage-workspace) |
| 92 | +10. Create a config.json file file using the subscription id, resource group name and workspace name that you use to create the workspace. The file is described at: [https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-configure-environment#workspace](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-configure-environment#workspace) |
| 93 | +11. Create an Azure service principal. You can do this with the commands: |
| 94 | +```sh |
| 95 | + az login |
| 96 | + az account set --subscription subscriptionid |
| 97 | + az ad sp create-for-rbac --name principlename --password password |
| 98 | +``` |
| 99 | +12. Insert the values \<tenant\>, \<AppId\> and \<password\> returned by create-for-rbac above into the aml_connection table. Set \<path\> as the absolute path to your config.json file. Set the name to “Default”. |
| 100 | + |
| 101 | +<a name="ssmsenergydemand"></a> |
| 102 | +## Energy demand example using SQL Server Management Studio |
| 103 | + |
| 104 | +Once you have completed the setup, you can try the energy demand sample queries. |
| 105 | +First you need to load the sample data in the database. |
| 106 | +1. In SQL Server Management Studio, you can right-click the database, select Tasks, then Import Flat file. |
| 107 | +1. Select the file MachineLearningNotebooks\notebooks\how-to-use-azureml\automated-machine-learning\forecasting-energy-demand\nyc_energy.csv. |
| 108 | +1. When you get to the column definition page, allow nulls for all columns. |
| 109 | + |
| 110 | +You can then run the queries in the energy-demand folder: |
| 111 | +* TrainEnergyDemand.sql runs AutoML, trains multiple models on data and selects the best model. |
| 112 | +* PredictEnergyDemand.sql predicts based on the most recent training run. |
| 113 | +* GetMetrics.sql returns all the metrics for each model in the most recent training run. |
0 commit comments