设置 IoT Edge 模块和连接Set up IoT Edge modules and connections

本 Azure SQL Edge 铁矿石杂质预测教程由三部分组成,在第二部分中,你将设置以下 IoT Edge 模块:In part two of this three-part tutorial for predicting iron ore impurities in Azure SQL Edge, you'll set up the following IoT Edge modules:

  • Azure SQL EdgeAzure SQL Edge
  • 数据生成器 IoT Edge 模块Data generator IoT Edge module

指定容器注册表凭据Specify container registry credentials

需要指定托管模块映像的容器注册表的凭据。The credentials to the container registries hosting module images need to be specified. 可在资源组中创建的容器注册表中找到它们。These can be found in the container registry that was created in your resource group. 导航到“访问密钥”部分。Navigate to the Access Keys section. 请记下以下字段:Make note of the following fields:

  • 注册表名称Registry name
  • 登录服务器Login server
  • 用户名Username
  • 密码Password

现在,在 IoT Edge 模块中指定容器凭据。Now, specify the container credentials in the IoT Edge module.

  1. 导航到在资源组中创建的 IoT 中心。Navigate to the IoT hub that was created in your resource group.

  2. 在“自动设备管理”下的“IoT Edge”部分,单击“设备 ID”。In the IoT Edge section under Automatic Device Management, click Device ID. 对于本教程,ID 是 IronOrePredictionDeviceFor this tutorial, the ID is IronOrePredictionDevice.

  3. 选择“设置模块”部分。Select the Set Modules section.

  4. 在“容器注册表凭据”下,输入以下值:Under Container Registry Credentials, enter the following values:

    字段Field Value
    名称Name 注册表名称Registry name
    地址Address 登录服务器Login server
    用户名User Name 用户名Username
    密码Password 密码Password

部署数据生成器模块Deploy the data generator module

  1. 在“自动设备管理”下的“IoT Edge”部分,单击“设备 ID”。In the IoT Edge section under Automatic Device Management, click Device ID. 在本教程中,ID 为 IronOrePredictionDevice,然后单击“设置模块”。For this tutorial, the ID is IronOrePredictionDevice and then click on Set Modules.

  2. 在“在设备上设置模块:”页的“IoT Edge 模块”部分下,单击“+ ADD”并选择“IoT Edge 模块” 。Under the IoT Edge Modules section on the Set modules on device: page, click + ADD and select IoT Edge module.

  3. 为 IoT Edge 模块提供有效的名称和映像 URI。Provide a valid name and image URI for IoT Edge module. 可以在本教程第一部分中创建的资源组的容器注册表中找到映像 URI。The Image URI can be found in the container registry in the resource group created in part one of this tutorial. 选择“服务”下的“存储库”部分。Select the Repositories section under Services. 对于本教程,请选择名为 silicaprediction 的存储库。For this tutorial, choose the repository named silicaprediction. 选择相应标记。Select the appropriate tag. 映像 URI 的格式为:The Image URI will be of the format:

    containerregistry 登录服务器/存储库名称:标记名称login server of the containerregistry/repository name:tag name

    例如:For example:

    ASEdemocontregistry.azurecr.io/silicaprediction:amd64
    
  4. 按原样保留“重启策略”和“所需状态”字段 。Leave the Restart Policy and the Desired Status fields as is.

  5. 单击“添加” 。Click Add.

部署 Azure SQL Edge 模块Deploy the Azure SQL Edge module

  1. 通过依次单击“+ 添加”和“市场模块”部署 Azure SQL Edge 模块 。Deploy the Azure SQL Edge module by clicking on + Add and then Marketplace Module.

  2. 在“IoT Edge 模块市场”边栏选项卡上,搜索“Azure SQL Edge”并选择“Azure SQL Edge 开发人员” 。On the IoT Edge Module Marketplace blade, search for Azure SQL Edge and pick Azure SQL Edge Developer.

  3. 单击“IoT Edge 模块”下新添加的“Azure SQL Edge”模块,以配置 Azure SQL Edge 模块。Click on the newly added Azure SQL Edge module under IoT Edge Modules to configure the Azure SQL Edge module. 有关配置选项的详细信息,请参阅部署 Azure SQL EdgeFor more information on the configuration options, see Deploy Azure SQL Edge.

  4. MSSQL_PACKAGE 环境变量添加到 Azure SQL Edge 模块部署中,并指定在本教程第一部分的步骤 8 中创建的数据库 dacpac 文件的 SAS URL。Add the MSSQL_PACKAGE environment variable to the Azure SQL Edge module deployment, and specify the SAS URL of the database dacpac file created in step 8 of Part one of this tutorial.

  5. 单击“更新”Click update

  6. 在“在设备上设置模块”页上 ,单击“下一步:路由 >”On the Set modules on device page, click Next: Routes >.

  7. 在“在设备上设置模块”页的路由窗格上,按如下所述为模块指定到 IoT Edge 中心通信的路由。On the routes pane of the Set modules on device page, specify the routes for module to IoT Edge hub communication as described below. 请确保更新下面路由定义中的模块名称。Make sure to update the module names in the route definitions below.

    FROM /messages/modules/<your_data_generator_module>/outputs/IronOreMeasures INTO
    BrokeredEndpoint("/modules/<your_azure_sql_edge_module>/inputs/IronOreMeasures")
    

    例如:For example:

    FROM /messages/modules/ASEDataGenerator/outputs/IronOreMeasures INTO BrokeredEndpoint("/modules/AzureSQLEdge/inputs/IronOreMeasures")
    
  8. 在“在设备上设置模块”页上 ,单击“下一步:查看 + 创建 >”On the Set modules on device page, click Next: Review + create >

  9. 在“在设备上设置模块”页上,单击“创建” On the Set modules on device page, click Create

在 Azure SQL Edge 中创建并启动 T-SQL 流式处理作业。Create and start the T-SQL Streaming Job in Azure SQL Edge.

  1. 打开 Azure Data Studio。Open Azure Data Studio.

  2. 在“欢迎使用”选项卡中,使用以下详细信息启动一个新连接:In the Welcome tab, start a new connection with the following details:

    字段Field Value
    连接类型Connection type Microsoft SQL ServerMicrosoft SQL Server
    服务器Server 为此演示创建的 VM 中提及的公共 IP 地址Public IP address mentioned in the VM that was created for this demo
    用户名Username sasa
    密码Password 创建 Azure SQL Edge 实例时使用的强密码The strong password that was used while creating the Azure SQL Edge instance
    数据库Database 默认Default
    服务器组Server group 默认Default
    名称(可选)Name (optional) 提供可选名称Provide an optional name
  3. 单击“连接”Click Connect

  4. 在“文件”菜单选项卡中,打开一个新笔记本或使用键盘快捷方式 Ctrl + N。In the File menu tab, open a new notebook or use the keyboard shortcut Ctrl + N.

  5. 在新的查询窗口中,执行以下脚本以创建 T-SQL 流式处理作业。In the new Query window, execute the script below to create the T-SQL Streaming job. 在执行脚本之前,请确保更改以下变量。Before executing the script, make sure to change the following variables.

    • SQL_SA_Password:部署 Azure SQL Edge 模块时指定的 MSSQL_SA_PASSWORD 值。SQL_SA_Password: The MSSQL_SA_PASSWORD value specified while deploy the Azure SQL Edge Module.
    Use IronOreSilicaPrediction
    Go
    
    Declare @SQL_SA_Password varchar(200) = '<SQL_SA_Password>'
    declare @query varchar(max) 
    
    /*
    Create Objects Required for Streaming
    */
    
    CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'MyStr0ng3stP@ssw0rd';
    
    If NOT Exists (select name from sys.external_file_formats where name = 'JSONFormat')
    Begin
       CREATE EXTERNAL FILE FORMAT [JSONFormat]  
       WITH ( FORMAT_TYPE = JSON)
    End 
    
    
    If NOT Exists (select name from sys.external_data_sources where name = 'EdgeHub')
    Begin
       Create EXTERNAL DATA SOURCE [EdgeHub] 
       With(
          LOCATION = N'edgehub://'
       )
    End 
    
    If NOT Exists (select name from sys.external_streams where name = 'IronOreInput')
    Begin
       CREATE EXTERNAL STREAM IronOreInput WITH 
       (
          DATA_SOURCE = EdgeHub,
          FILE_FORMAT = JSONFormat,
          LOCATION = N'IronOreMeasures'
        )
    End
    
    
    If NOT Exists (select name from sys.database_scoped_credentials where name = 'SQLCredential')
    Begin
        set @query = 'CREATE DATABASE SCOPED CREDENTIAL SQLCredential
                  WITH IDENTITY = ''sa'', SECRET = ''' + @SQL_SA_Password + ''''
        Execute(@query)
    End 
    
    If NOT Exists (select name from sys.external_data_sources where name = 'LocalSQLOutput')
    Begin
       CREATE EXTERNAL DATA SOURCE LocalSQLOutput WITH (
       LOCATION = 'sqlserver://tcp:.,1433',CREDENTIAL = SQLCredential)
    End
    
    If NOT Exists (select name from sys.external_streams where name = 'IronOreOutput')
    Begin
       CREATE EXTERNAL STREAM IronOreOutput WITH 
       (
          DATA_SOURCE = LocalSQLOutput,
          LOCATION = N'IronOreSilicaPrediction.dbo.IronOreMeasurements'
       )
    End
    
    EXEC sys.sp_create_streaming_job @name=N'IronOreData',
    @statement= N'Select * INTO IronOreOutput from IronOreInput'
    
    exec sys.sp_start_streaming_job @name=N'IronOreData'
    
  6. 使用以下查询验证数据生成器模块中的数据是否正在流式传输到数据库中。Use the following query to verify that the data from the data generation module is being streamed into the database.

    Select Top 10 * from dbo.IronOreMeasurements
    order by timestamp desc
    

在本教程中,我们部署了数据生成器模块和 SQL Edge 模块。In this tutorial, we deployed the data generator module and the SQL Edge module. 然后,我们创建了一个流式处理作业,将数据生成器模块生成的数据流式传输到 SQL。Then we created a streaming job to stream the data generated by the data generation module to SQL.

后续步骤Next Steps