你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

自动执行流分析项目的生成、测试和部署

Azure 流分析 (ASA) CI/CD npm 包可帮助你自动生成、测试和部署流分析项目。 本文演示如何在任何 CI/CD 系统中使用 npm 包。 若要使用 Azure DevOps设置管道,请参阅 使用 Azure DevOps为流分析作业创建 CI/CD 管道

如果你没有流分析项目,请使用 Visual Studio Code 创建一个,或者从 Azure 门户导出现有项目。

安装

可以从 npm 站点下载包,也可以在终端中运行以下命令。

npm install -g azure-streamanalytics-cicd

构建项目

注意

使用 --v2 选项来指定更新的 ARM 模板架构。 更新的架构的参数较少,但保留与以前的版本相同的功能。

旧 ARM 模板已弃用。 仅通过 build --v2 创建的模板才能接收更新或 bug 修复。

azure-streamanalytics-cicd build --v2 --project <projectFullPath> [--outputPath <outputPath>]

build 命令执行关键字语法检查并生成Azure 资源管理器 (ARM) 模板。

论点 说明
--project 使用绝对路径或相对路径指定 asaproj.json 文件。
--outputPath 使用绝对路径或相对路径指定用于存储 ARM 模板的输出文件夹。 如果未指定 outputPath,模板将进入当前目录。

示例

# Go to the project directory
cd <path-to-the-project>

# Build project
azure-streamanalytics-cicd build --v2 --project ./asaproj.json --outputPath ./Deploy

如果项目成功生成,则会看到在输出文件夹下创建的两个 JSON 文件:

  • ARM 模板文件:[ProjectName].JobTemplate.json
  • Azure 资源管理器参数文件:[ProjectName].JobTemplate.parameters.json

parameters.json 文件的默认值来自项目设置。 如果要部署到其他环境,请相应地替换值。

所有凭据的默认值均为 null。 在部署到Azure之前设置值。

"Input_EntryStream_sharedAccessPolicyKey": {
  "value": null
}

若要将托管标识用于 Azure Data Lake Store Gen1 作为输出接收器,需要在部署到Azure之前使用 PowerShell 提供对服务主体的访问权限。 若要了解详细信息,请参阅 使用 Azure 资源管理器模板和托管标识部署 ADLS Gen1

在本地运行

如果项目包含本地输入文件,请使用 localrun 命令在本地运行流分析脚本。

azure-streamanalytics-cicd localrun -project <projectFullPath> [-outputPath <outputPath>] [-customCodeZipFilePath <zipFilePath>]
论点 说明
--project 使用绝对路径或相对路径指定 asaproj.json 文件。
--outputPath 使用绝对路径或相对路径指定用于存储 ARM 模板的输出文件夹。 如果未指定 outputPath,则将模板放在当前目录中。
--customCodeZipFilePath C# 自定义代码(如 UDF 或反序列化器)的 zip 文件的路径(如果使用它们)。 将 DLL 打包到一个 zip 文件中,并指定此路径。

示例

# Go to the project directory
cd <path-to-the-project>

# Run project locally
azure-streamanalytics-cicd localrun --project ./asaproj.json"

注意

JavaScript UDF 仅适用于Windows。

自动测试

使用 CI/CD npm 包为流分析项目配置和运行自动测试。

添加测试用例

azure-streamanalytics-cicd addtestcase --project <projectFullPath> [-testConfigPath <testConfigFileFullPath>]

可以在测试配置文件中找到测试用例。

论点 说明
--project 使用绝对路径或相对路径指定 asaproj.json 文件。
--testConfigPath 测试配置文件的路径。 如果未指定此参数,该工具将在 \test 中搜索 asaproj.json 文件的当前目录下的文件,默认文件名 testConfig.json。 如果该文件不存在,该工具将创建一个新文件。

示例

# Go to the project directory
cd <path-to-the-project>

# Add a test case
azure-streamanalytics-cicd addtestcase --project ./asaproj.json

如果测试配置文件为空,请将以下内容添加到该文件。 否则,将测试用例添加到 TestCases 数组。 该工具根据输入配置文件自动填充所需的输入配置。 在运行测试之前,必须为每个输入和预期输出指定 FilePath 。 可手动修改配置。

如果希望测试验证忽略某个输出,请将预期输出的“必填”字段设置为 false 。

{
  "Script": [Absolute path of your script],
  "TestCases": [
    {
      "Name": "Case 1",
      "Inputs": [
        {
          "InputAlias": [Input alias string],
          "Type": "Data Stream",
          "Format": "JSON",
          "FilePath": [Required],
          "ScriptType": "InputMock"
        }
      ],
      "ExpectedOutputs": [
        {
          "OutputAlias": [Output alias string],
          "FilePath": [Required],
          "IgnoreFields": [Fields to ignore for test validation, e.g., ["col1", "col2"]],
          "Required": true
        }
      ]
    }
  ]
}

运行单元测试

使用以下命令为项目运行多个测试用例。 该过程在输出文件夹中生成测试结果摘要。 如果所有测试都通过,则进程会退出代码 0 ;如果发生异常,则为 -1 ;如果测试失败,则 为 -2

azure-streamanalytics-cicd test --project <projectFullPath> [--testConfigPath <testConfigFileFullPath>] [--outputPath <outputPath>] [--customCodeZipFilePath <zipFilePath>]
论点 说明
--project asaproj.json 文件的路径。
--testConfigPath 测试配置文件的路径。 如果未指定此参数,则进程将在 \test 中搜索 asaproj.json 文件的当前目录下的文件,默认文件名 testConfig.json
--outputPath 测试结果输出文件夹的路径。 如果未指定此参数,进程会将输出结果文件置于当前目录中。
--customCodeZipFilePath UDF 或反序列化程序(若使用)等自定义代码的 zip 文件的路径。 需要将 DLL 打包为 zip 文件并指定路径。

如果运行测试用例,可以在输出文件夹中找到生成的 testResultSummary.json 文件。

{
  "Total": (integer) total_number_of_test_cases,
  "Passed": (integer) number_of_passed_test_cases,
  "Failed": (integer) number_of_failed_test_cases,
  "Script": (string) absolute_path_to_asaql_file,
  "Results": [ (array) detailed_results_of_test_cases
    {
      "Name": (string) name_of_test_case,
      "Status": (integer) 0(passed)_or_1(failed),
      "Time": (string) time_span_of_running_test_case,
      "OutputMatched": [ (array) records_of_actual_outputs_equal_to_expected_outputs
        {
          "OutputAlias": (string) output_alias,
          "ExpectedOutput": (string) path_to_the_expected_output_file,
          "Output": (string) path_to_the_actual_output_file
        }
      ],
      "OutputNotEqual": [ (array) records_of_actual_outputs_not_equal_to_expected_outputs
        {
          "OutputAlias": (string) output_alias,
          "ExpectedOutput": (string) path_to_the_expected_output_file,
          "Output": (string) path_to_the_actual_output_file
        }
      ],
      "OutputMissing": [ (array) records_of_actual_outputs_missing
        {
          "OutputAlias": (string) output_alias,
          "ExpectedOutput": (string) path_to_the_expected_output_file,
          "Output": ""
        }
      ],
      "OutputUnexpected": [ (array) records_of_actual_outputs_unexpected
        {
          "OutputAlias": (string) output_alias,
          "ExpectedOutput": "",
          "Output": (string) path_to_the_actual_output_file
        }
      ],
      "OutputUnrequired": [ (array) records_of_actual_outputs_unrequired_to_be_checked
        {
          "OutputAlias": (string) output_alias,
          "ExpectedOutput": (string) path_to_the_expected_output_file,
          "Output": (string) path_to_the_actual_output_file
        }
      ]
    }
  ],
  "Time": (string) time_span_of_running_all_test_cases,
}

注意

如果查询结果包含浮点值,则可能在生成的值中略有差异,从而导致可能失败的测试。 这种差异基于支持Visual Studio或Visual Studio引擎和测试处理引擎的不同.NET框架。 若要确保测试成功运行,请降低生成的值的精度,或使结果与生成的测试结果进行手动比较。

部署到 Azure

若要使用 ARM 模板部署流分析项目,请执行以下步骤:

  1. 连接到 Azure 帐户:

    # Connect to Azure
    Connect-AzAccount
    # Set the Azure subscription
    Set-AzContext [SubscriptionID/SubscriptionName]
    
  2. 部署流分析项目:

    $templateFile = ".\Deploy\ClickStream-Filter.JobTemplate.json"
    $parameterFile = ".\Deploy\ClickStream-Filter.JobTemplate.parameters.json"
    New-AzResourceGroupDeployment `
      -Name devenvironment `
      -ResourceGroupName myResourceGroupDev `
      -TemplateFile $templateFile `
      -TemplateParameterFile $parameterFile
    

有关使用 ARM 模板部署资源的详细信息,请参阅 使用资源管理器模板文件和 Azure PowerShell 进行部署