将 'Set Variable' activity 的输出传输到 json 文件 [Azure 数据工厂]

Transfer the output of 'Set Variable' activity into a json file [Azure Data Factory]

在数据工厂中,我们可以将 'Set Variable' activity 的输出记录为 json 文件吗?

据我所知,没有满足您需求的内置简单方法。

2 种解决方法:

1.Use 启用 Azure Monitor diagnostic log in ADF to log data into Azure Blob Storage as JSON files.And every activity's execution details(contains output) 可以在 file.However 中登录,您需要了解 json 模式的结构并获取您想要的内容。

2.Use Azure Function 或 Web Activity 在设置变量 Activity 后调用 API(@activity('Set Variable1').output ).在函数方法中用SDK代码将输出保存为json文件。

实现此要求的另一种最简单的方法是利用“Add additional columns during copy”功能,如下所示。

设置变量activity并设置变量的值,然后复制activity。在复制 activity、Source 设置中,您可以在 Additional columns 属性 中为源变量列命名。使用动态表达式 @variables('varInput') 分配变量值。然后在 Mapping 部分中,您可以删除不需要的列,只保留所需的列,包括您在 Additional columnsSource 中创建的变量列。然后在目标端给出你想要的列名并测试它。

注意:此功能适用于最新的数据集模型。如果您在 UI 中没有看到此选项,请尝试创建一个新数据集。

希望这对您有所帮助。

如果您想写入数组类型变量的内容,有一个工作正常的变通方法。 目标:将数组的内容作为每个数组值的 1 行写入文件

变量:[a, b, c]

文件内容:
一个
b
c

步骤:

  1. 创建 1 行的 'empty' 文件,可以是 json 文件或其他只有 1 行的文件
  2. 使用附加列机制
  3. Expand-join-with-carriage 使用@join 和@decodeUriComponent 返回数组变量 -> @join(variable,decodeUriComponent('%0A'))

是的,微软没有创建特殊字符的@char(int) 函数真是太可怕了。(或者我是个白痴,不知道连接 '\n' 的正确方法,我试过了但没用。)

我通常使用复制 activity 来写入文件,但可以使用 Blob REST APIPUT 命令将内容写入 Azure Data Lake (ADLS) Gen 2。 Web 中的设置 activity 对这项工作至关重要:

Setting Value Notes
URL some blob NB this is using the .blob address not the .dfs one. The path must end in ?resource=file
Method PUT
Headers
x-ms-version 2019-07-07
x-ms-blob-type BlockBlob
Content-Type application/json This value is for writing json but can be customised eg application/csv
Body @variables('varResult') I'm using a pre-prepared variable with json content but this can be anything
Authentication Managed Identity
Resource https://storage.azure.com

请注意,您必须将 URL 设置为所需的文件名和文件夹,并使用 .blob 地址。 URL 必须以 ?resource=file:

结尾

示例URL / Blob 地址 https://yourstorage.blob.core.windows.net/yourFilesystem/yourFolder/someFile.json?resource=file

另请注意,我在这里写的是 json,但您可以根据需要进行修改,例如 application/csv。我在 Body 中使用了一个变量,但这可以是你喜欢的任何东西。文档说明这仅支持最大 2GB 的文件,因此这仅适用于小型活动。

屏幕截图:

我无法使它与 .dfs 地址 and/or 数据湖方法一起使用,但只要它适用于 blob 就没问题。