部署使用数据导入和数据导出模块的 Azure 机器学习工作室(经典)Web 服务Deploy Azure Machine Learning Studio (classic) web services that use Data Import and Data Export modules

适用于: yes机器学习工作室(经典) noAzure 机器学习APPLIES TO: yesMachine Learning Studio (classic) noAzure Machine Learning 创建预测性实验时,通常添加 Web 服务输入和输出。When you create a predictive experiment, you typically add a web service input and output. 部署实验时,使用者可通过输入和输出从 Web 服务发送和接收数据。When you deploy the experiment, consumers can send and receive data from the web service through the inputs and outputs. 对于某些应用程序,使用者的数据可能从数据源提供或已经驻留在外部数据源(如 Azure Blob 存储)中。For some applications, a consumer's data may be available from a data feed or already reside in an external data source such as Azure Blob storage. 在这些情况下,它们不需要使用 Web 服务输入和输出读取和写入数据。In these cases, they do not need read and write data using web service inputs and outputs. 它们可以改为使用批处理执行服务 (BES),使用导入数据模块从数据源读取数据,使用导出数据模块将评分结果写入不同的数据位置。They can, instead, use the Batch Execution Service (BES) to read data from the data source using an Import Data module and write the scoring results to a different data location using an Export Data module.

“导入数据”和“导出数据”模块可在各个数据位置(如使用 HTTP 的 Web URL、Hive 查询、Azure SQL 数据库中的数据库、Azure 表存储、Azure Blob 存储、数据馈送提供或 SQL Server 数据库)中读取和写入数据。The Import Data and Export data modules, can read from and write to various data locations such as a Web URL via HTTP, a Hive Query, a database in Azure SQL Database, Azure Table storage, Azure Blob storage, a Data Feed provide, or a SQL Server database.

本主题使用“示例 5:二元分类的训练、测试、评估:成人数据集”示例并假设数据集已加载到名为 censusdata 的 Azure SQL 表中。This topic uses the "Sample 5: Train, Test, Evaluate for Binary Classification: Adult Dataset" sample and assumes the dataset has already been loaded into an Azure SQL table named censusdata.

创建训练实验Create the training experiment

打开“示例 5:二元分类的训练、测试、评估:成人数据集”示例时,它使用“成人人口收入二元分类”数据集示例。When you open the "Sample 5: Train, Test, Evaluate for Binary Classification: Adult Dataset" sample it uses the sample Adult Census Income Binary Classification dataset. 画布中的实验将看起来类似于下图:And the experiment in the canvas will look similar to the following image:

实验的初始配置。

若要从 Azure SQL 表读取数据:To read the data from the Azure SQL table:

  1. 删除数据集模块。Delete the dataset module.

  2. 在组件搜索框中,键入导入。In the components search box, type import.

  3. 从结果列表中,将导入数据模块添加到实验画布。From the results list, add an Import Data module to the experiment canvas.

  4. 导入数据模块的输出连接到清除缺少的数据模块的输入。Connect output of the Import Data module the input of the Clean Missing Data module.

  5. 在属性窗格中,选择“数据源”下拉列表中的“Azure SQL 数据库”。In properties pane, select Azure SQL Database in the Data Source dropdown.

  6. 在“数据库服务器名称”、“数据库名称”、“用户名”和“密码”字段中,输入数据库的相应信息。In the Database server name, Database name, User name, and Password fields, enter the appropriate information for your database.

  7. 在“数据库查询”字段中,输入以下查询。In the Database query field, enter the following query.

     select [age],
    
        [workclass],
        [fnlwgt],
        [education],
        [education-num],
        [marital-status],
        [occupation],
        [relationship],
        [race],
        [sex],
        [capital-gain],
        [capital-loss],
        [hours-per-week],
        [native-country],
        [income]
     from dbo.censusdata;
    
  8. 在实验画布的底部,单击“运行”。At the bottom of the experiment canvas, click Run.

创建预测性实验Create the predictive experiment

接下来,设置从中部署 Web 服务的预测性实验。Next you set up the predictive experiment from which you deploy your web service.

  1. 在实验画布的底部,单击“设置 Web 服务”,并选择“预测 Web 服务 [推荐]”。At the bottom of the experiment canvas, click Set Up Web Service and select Predictive Web Service [Recommended].
  2. 从预测性实验中删除 Web 服务输入Web 服务输出模块Remove the Web Service Input and Web Service Output modules from the predictive experiment.
  3. 在组件搜索框中,键入导出。In the components search box, type export.
  4. 从结果列表中,将导出数据模块添加到实验画布。From the results list, add an Export Data module to the experiment canvas.
  5. 评分模型模块的输出连接到导出数据模块的输入。Connect output of the Score Model module the input of the Export Data module.
  6. 在属性窗格中,在数据目标下拉列表中选择“Azure SQL 数据库”。In properties pane, select Azure SQL Database in the data destination dropdown.
  7. 在“数据库服务器名称”、“数据库名称”、“服务器用户帐户名”和“服务器用户帐户密码”字段中,输入数据库的相应信息。In the Database server name, Database name, Server user account name, and Server user account password fields, enter the appropriate information for your database.
  8. 在“要保存的列的逗号分隔列表字段中,键入评分标签。In the Comma separated list of columns to be saved field, type Scored Labels.
  9. 在“数据表名称”字段中,键入 dbo.ScoredLabels。In the Data table name field, type dbo.ScoredLabels. 如果表不存在,则在运行实验或调用 Web 服务时创建它。If the table does not exist, it is created when the experiment is run or the web service is called.
  10. 在“数据表列的逗号分隔列表”字段中,键入 ScoredLabels。In the Comma separated list of datatable columns field, type ScoredLabels.

编写调用最终 Web 服务的应用程序时,可能要在运行时指定不同的输入查询或目标表。When you write an application that calls the final web service, you may want to specify a different input query or destination table at run time. 若要配置这些输入和输出,可使用 Web 服务参数功能设置导入数据模块数据源属性和导出数据模式数据目标属性。To configure these inputs and outputs, use the Web Service Parameters feature to set the Import Data module Data source property and the Export Data mode data destination property. 有关 Web 服务参数的详细信息,请参阅 Cortana Intelligence 和机器学习博客上的 Azure 机器学习工作室 Web 服务参数条目For more information on Web Service Parameters, see the Azure Machine Learning Studio Web Service Parameters entry on the Cortana Intelligence and Machine Learning Blog.

若要为导入查询和目标表配置 Web 服务参数:To configure the Web Service Parameters for the import query and the destination table:

  1. 导入数据模块的属性窗格中,单击“数据库查询”字段右上方的图标,并选择“设置为 Web 服务参数”。In the properties pane for the Import Data module, click the icon at the top right of the Database query field and select Set as web service parameter.
  2. 导出数据模块的属性窗格中,单击“数据表名称”字段右上方的图标,并选择“设置为 Web 服务参数”。In the properties pane for the Export Data module, click the icon at the top right of the Data table name field and select Set as web service parameter.
  3. 导出数据模块属性窗格底部,在“Web 服务参数”部分中单击“数据库查询”并将其重命名为“查询”。At the bottom of the Export Data module properties pane, in the Web Service Parameters section, click Database query and rename it Query.
  4. 单击“数据表名称”并将其重命名为“表”。Click Data table name and rename it Table.

完成后,实验应看起来类似于下图:When you are done, your experiment should look similar to the following image:

实验的最终外观。

现在可以将实验部署为 Web 服务。Now you can deploy the experiment as a web service.

部署 Web 服务Deploy the web service

可部署到经典或新 Web 服务。You can deploy to either a Classic or New web service.

部署经典 Web 服务Deploy a Classic Web Service

若要部署为经典 Web 服务并创建一个应用程序来使用它:To deploy as a Classic Web Service and create an application to consume it:

  1. 在实验画布的底部,单击“运行”。At the bottom of the experiment canvas, click Run.

  2. 当运行已完成时,单击“部署 Web 服务”,并选择“部署 Web 服务 [经典]”。When the run has completed, click Deploy Web Service and select Deploy Web Service [Classic].

  3. 在 Web 服务仪表板上,找到 API 密钥。On the web service dashboard, locate your API key. 复制并保存它以供以后使用。Copy and save it to use later.

  4. 在“默认终结点”表中,单击“批处理执行”链接打开 API 帮助页。In the Default Endpoint table, click the Batch Execution link to open the API Help Page.

  5. 在 Visual Studio 中,创建 C# 控制台应用程序:“新建” > “项目” > “Visual C#” > “Windows 经典桌面” > “控制台应用(.NET Framework)” 。In Visual Studio, create a C# console application: New > Project > Visual C# > Windows Classic Desktop > Console App (.NET Framework).

  6. 在 API 帮助页上,找到页面底部的“示例代码”部分。On the API Help Page, find the Sample Code section at the bottom of the page.

  7. 将 C# 示例代码复制并粘贴到 Program.cs 文件中,并删除对 blob 存储的所有引用。Copy and paste the C# sample code into your Program.cs file, and remove all references to the blob storage.

  8. 使用之前保存的 API 密钥更新 apiKey 变量的值。Update the value of the apiKey variable with the API key saved earlier.

  9. 找到请求声明并更新传递到导入数据导出数据模块的值。Locate the request declaration and update the values of Web Service Parameters that are passed to the Import Data and Export Data modules. 在此示例中,将使用原始查询,但定义新的表名。In this case, you use the original query, but define a new table name.

     var request = new BatchExecutionRequest()
     {
         GlobalParameters = new Dictionary<string, string>() {
             { "Query", @"select [age], [workclass], [fnlwgt], [education], [education-num], [marital-status], [occupation], [relationship], [race], [sex], [capital-gain], [capital-loss], [hours-per-week], [native-country], [income] from dbo.censusdata" },
             { "Table", "dbo.ScoredTable2" },
         }
     };
    
  10. 运行应用程序。Run the application.

完成运行时,将新表添加到包含评分结果的数据库。On completion of the run, a new table is added to the database containing the scoring results.

部署新的 Web 服务Deploy a New Web Service

备注

若要部署新的 Web 服务,必须对要部署 Web 服务的订阅拥有充分的权限。To deploy a New web service you must have sufficient permissions in the subscription to which you deploying the web service. 有关详细信息,请参阅使用 Azure 机器学习 Web 服务门户管理 Web 服务For more information, see Manage a Web service using the Azure Machine Learning Web Services portal.

若要部署为新的 Web 服务并创建一个应用程序来使用它:To deploy as a New Web Service and create an application to consume it:

  1. 在实验画布的底部,单击“运行”。At the bottom of the experiment canvas, click Run.

  2. 当运行已完成时,单击“部署 Web 服务”,并选择“部署 Web 服务 [新]”。When the run has completed, click Deploy Web Service and select Deploy Web Service [New].

  3. 在“部署实验”页上,输入 Web 服务的名称并选择定价计划,并单击“部署”。On the Deploy Experiment page, enter a name for your web service, and select a pricing plan, then click Deploy.

  4. 在“快速启动”页上,单击“使用”。On the Quickstart page, click Consume.

  5. 在“示例代码”部分中,单击“批处理”。In the Sample Code section, click Batch.

  6. 在 Visual Studio 中,创建 C# 控制台应用程序:“新建” > “项目” > “Visual C#” > “Windows 经典桌面” > “控制台应用(.NET Framework)” 。In Visual Studio, create a C# console application: New > Project > Visual C# > Windows Classic Desktop > Console App (.NET Framework).

  7. 将 C# 示例代码复制并粘贴到 Program.cs 文件中。Copy and paste the C# sample code into your Program.cs file.

  8. 使用位于“基本使用信息”部分中的主键替换 apiKey 变量的值。Update the value of the apiKey variable with the Primary Key located in the Basic consumption info section.

  9. 找到 scoreRequest 声明并更新传递到导入数据导出数据模块的值。Locate the scoreRequest declaration and update the values of Web Service Parameters that are passed to the Import Data and Export Data modules. 在此示例中,将使用原始查询,但定义新的表名。In this case, you use the original query, but define a new table name.

    var scoreRequest = new
    {
        Inputs = new Dictionary<string, StringTable>()
        {
        },
        GlobalParameters = new Dictionary<string, string>() {
            { "Query", @"select [age], [workclass], [fnlwgt], [education], [education-num], [marital-status], [occupation], [relationship], [race], [sex], [capital-gain], [capital-loss], [hours-per-week], [native-country], [income] from dbo.censusdata" },
            { "Table", "dbo.ScoredTable3" },
        }
    };
    
  10. 运行应用程序。Run the application.