Azure 数据工厂中的 Azure 函数活动Azure Function activity in Azure Data Factory

适用于: 是 Azure 数据工厂是 Azure Synapse Analytics(预览版)APPLIES TO: yesAzure Data Factory yesAzure Synapse Analytics (Preview)

通过 Azure 函数活动可以在数据工厂管道中运行 Azure FunctionsThe Azure Function activity allows you to run Azure Functions in a Data Factory pipeline. 若要运行 Azure 函数,需要创建链接服务连接以及指定计划执行的 Azure 函数的活动。To run an Azure Function, you need to create a linked service connection and an activity that specifies the Azure Function that you plan to execute.

有关此功能的 8 分钟简介和演示,请观看以下视频:For an eight-minute introduction and demonstration of this feature, watch the following video:

Azure 函数链接服务Azure Function linked service

Azure 函数的返回类型必须是有效的 JObjectThe return type of the Azure function has to be a valid JObject. (请记住:JArray 不 是 JObject。)除了 JObject 之外的任何返回类型都将失败,并且会引发用户错误响应内容不是有效的 JObject(Keep in mind that JArray is not a JObject.) Any return type other than JObject fails and raises the user error Response Content is not a valid JObject.

属性Property 说明Description 必需Required
typetype type 属性必须设置为:AzureFunctionThe type property must be set to: AzureFunction yes
函数应用 URLfunction app url Azure 函数应用的 URL。URL for the Azure Function App. 格式为 https://<accountname>.chinacloudsites.cnFormat is https://<accountname>.chinacloudsites.cn. 在 Azure 门户中查看函数应用时,此 URL 是 URL 部分下的值This URL is the value under URL section when viewing your Function App in the Azure portal yes
函数密钥function key Azure 函数的访问密钥。Access key for the Azure Function. 单击相应函数的“管理” 部分,并复制“函数密钥” 或“主机密钥” 。Click on the Manage section for the respective function, and copy either the Function Key or the Host key. 在此处了解详细信息:Azure Functions HTTP 触发器和绑定Find out more here: Azure Functions HTTP triggers and bindings yes

Azure 函数活动Azure Function activity

属性Property 说明Description 允许的值Allowed values 必需Required
namename 管道中活动的名称Name of the activity in the pipeline StringString yes
typetype 活动的类型为“AzureFunctionActivity”Type of activity is ‘AzureFunctionActivity’ StringString yes
链接服务linked service 相应 Azure 函数应用的 Azure 函数链接服务The Azure Function linked service for the corresponding Azure Function App 链接服务引用Linked service reference yes
函数名称function name 此活动在 Azure 函数应用中调用的函数的名称Name of the function in the Azure Function App that this activity calls StringString yes
methodmethod 函数调用的 Rest API 方法REST API method for the function call 字符串支持的类型:“GET”、“POST”、“PUT”String Supported Types: "GET", "POST", "PUT" yes
标头的值开始缓存响应header 发送到请求的标头。Headers that are sent to the request. 例如,若要在请求中设置语言和类型:"headers": { "Accept-Language": "en-us", "Content-Type": "application/json" }For example, to set the language and type on a request: "headers": { "Accept-Language": "en-us", "Content-Type": "application/json" } 字符串(或带有 resultType 字符串的表达式)String (or expression with resultType of string) No
bodybody 随请求一起发送到函数 API 方法的正文body that is sent along with the request to the function api method 字符串(或带有 resultType 字符串的表达式)或对象。String (or expression with resultType of string) or object.  PUT/POST 方法所必需Required for PUT/POST methods

请参阅 请求有效负载架构 部分中的请求有效负载架构。See the schema of the request payload in Request payload schema section.

路由和查询Routing and queries

Azure 函数活动支持路由The Azure Function Activity supports routing. 例如,如果你的 Azure Function 具有终结点 https://functionAPP.chinacloudsites.cn/api/<functionName>/<value>?code=<secret>,则要在 Azure 函数活动中使用的 functionName<functionName>/<value>For example, if your Azure Function has the endpoint https://functionAPP.chinacloudsites.cn/api/<functionName>/<value>?code=<secret>, then the functionName to use in the Azure Function Activity is <functionName>/<value>. 你可以将此函数参数化以在运行时提供所需的 functionNameYou can parameterize this function to provide the desired functionName at runtime.

Azure 函数活动还支持查询The Azure Function Activity also supports queries. 必须将查询包括为 functionName 的一部分。A query has to be included as part of the functionName. 例如,当函数名称为 HttpTriggerCSharp 并且要包括的查询为 name=hello 时,无法将 Azure 函数活动中的 functionName 构造为 HttpTriggerCSharp?name=helloFor example, when the function name is HttpTriggerCSharp and the query that you want to include is name=hello, then you can construct the functionName in the Azure Function Activity as HttpTriggerCSharp?name=hello. 可以将此函数参数化,以便在运行时确定值。This function can be parameterized so the value can be determined at runtime.

超时和长时间运行的函数Timeout and long running functions

Azure Functions 会在 230 秒之后超时,无论在设置中配置的 functionTimeout 设置如何。Azure Functions times out after 230 seconds regardless of the functionTimeout setting you've configured in the settings. 有关详细信息,请参阅此文章For more information, see this article. 若要解决此行为,请遵循某个异步模式或使用 Durable Functions。To work around this behavior, follow an async pattern or use Durable Functions. Durable Functions 的好处是,它们提供自己的状态跟踪机制,因此无需实现你自己的机制。The benefit of Durable Functions is that they offer their own state-tracking mechanism, so you won't have to implement your own.

此文章中详细了解 Durable Functions。Learn more about Durable Functions in this article. 你可以设置 Azure 函数活动来调用持久函数,这将返回具有不同 URI 的响应,例如此示例You can set up an Azure Function Activity to call the Durable Function, which will return a response with a different URI, such as this example. 因为 statusQueryGetUri 在函数正在运行时返回 HTTP 状态 202,因此你可以使用 Web 活动轮询函数的状态。Because statusQueryGetUri returns HTTP Status 202 while the function is running, you can poll the status of the function by using a Web Activity. 简单地设置一个 Web 活动并将 url 字段设置为 @activity('<AzureFunctionActivityName>').output.statusQueryGetUriSimply set up a Web Activity with the url field set to @activity('<AzureFunctionActivityName>').output.statusQueryGetUri. 当持久函数完成时,该函数的输出将是 Web 活动的输出。When the Durable Function completes, the output of the function will be the output of the Web Activity.

示例Sample

可以在此处找到使用 Azure 函数提取 tar 文件内容的数据工厂示例。You can find a sample of a Data Factory that uses an Azure Function to extract the content of a tar file here.

后续步骤Next steps

Azure 数据工厂中的管道和活动中了解有关数据工厂中的活动的详细信息。Learn more about activities in Data Factory in Pipelines and activities in Azure Data Factory.