Azure 流分析作业 - 转换查询 - ARM 模板中的正确格式

Azure Stream Analytics Job - Transformation Query - correct formatting in ARM template

在门户中编辑流分析转换查询时,您可以对其进行格式化以便跨多行阅读...例如

SELECT 
INTO [Output1]
FROM [Input1]
PARTITION BY PartitionId
WHERE etc etc etc

当将其放入 CI/CD 的 ARM 模板中时,这是作为一个巨大的长字符串输入的,最终将在门户中显示为...

SELECT * INTO [Output1] FROM [Input1] PARTITION BY PartitionId WHERE etc etc etc to infinity....

官方文档非常无用,没有给出模板查询部分的任何线索,只是它是一个 "string"...

https://docs.microsoft.com/en-us/azure/templates/microsoft.streamanalytics/2016-03-01/streamingjobs/transformations

有一个 Microsoft 示例模板,这是我能找到的唯一一个指定了转换查询的示例... https://github.com/Azure/azure-quickstart-templates/blob/master/101-streamanalytics-create/azuredeploy.json ...看起来它正在尝试间距...

"query": "SELECT\r\n    *\r\nINTO\r\n    [YourOutputAlias]\r\nFROM\r\n    [YourInputAlias]"

...但严重失败 - 请参阅屏幕截图

有人成功做到了吗?

还有谁知道为什么您可以在 Azure 资源浏览器 (https://resources.azure.com/) 中看到转换查询?或者它不能与流作业的其余部分一起从门户中导出? (在资源组级别完成)

提前致谢

我知道已经过了整整一年,也许你已经明白了,但是,这就是我所做的:

在我的 Parameters 文件中,我使用了一个字符串数组,例如:

"StreamAnalyticsJobQueryMultiline": {
    "value": [
    "WITH allData AS ( ",
    "    SELECT ",
    "        *, ",
    "        GetMetadataPropertyValue([%%INPUTSTREAMNAME%%], '[User].[EventNamespace]') AS EventNamespace ",
    "    FROM [%%INPUTSTREAMNAME%%] Partition By PartitionId ",
    "SELECT ",
    "    *, ",
    "    'EventHubWriterv1' AS EventType ",
    "INTO ",
    "    [%%OUTPUTSTREAMNAME%%] ",
    "FROM ",
    "    allData Partition By PartitionId "
    ]

当连接数组并输出为字符串时,它会生成类似这样的内容,其中该数组中的每个项目仍用引号括起来,整个内容包含在方括号中(请参阅:https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-string#concat)

["Foo","Bar","Blah"]

因此需要进行额外的转换以将其转换为流分析输出中可读的内容。
还要注意这里的 %%INPUTSTREAMNAME%% 和 %%OUTPUTSTREAMNAME%%,因为我的输入和输出流也是参数,并且使用典型的内联 [parameter('ParamName')] 不能很好地与其余部分一起使用需要的转换。

在我的 Template 文件中,我采用 StreamAnalyticsJobQueryMultiline 参数并使用变量字段进行此转换:

"QueryStringRaw":  "[string(concat(parameters('StreamAnalyticsJobQueryMultiline')))]",
    
// Update the end-of-lines by removing the doublequote and comma, and replacing it with a newline character 
"QueryStringIter1": "[replace(variables('QueryStringRaw'), '\",', '\n')]",

// Update the beginning-of-lines by removing the doublequote
"QueryStringIter2": "[replace(variables('QueryStringIter1'), '\"', '')]",

// Update the InputStreamName and OutputStreamName values
"QueryStringIter3": "[replace(variables('QueryStringIter2'), '%%INPUTSTREAMNAME%%', parameters('InputStreamName'))]",
"QueryStringIter4": "[replace(variables('QueryStringIter3'), '%%OUTPUTSTREAMNAME%%', parameters('OutputStreamName'))]",

// Produce the final output for the query string by trimming the leading and trailing square brackets
"QueryStringFinal": "[substring(variables('QueryStringIter4'), 1, sub(length(variables('QueryStringIter4')), 2))]"

然后我在 Microsoft.StreamAnalytics/streamingjobs 的转换部分引用了它的属性:

"transformation": {
    "name": "Transformation",
    "properties": {
        "streamingUnits": "[parameters('StreamAnalyticsStreamingUnitsScale')]",
        "query": "[variables('QueryStringFinal')]"
    }
}