简体   繁体   English

如何将 SQL 行转换为 Azure 数据工厂中的 json 对象数组?

[英]How to convert SQL rows to an array of json objects in Azure Data Factory?

I'm working on a Azure Data Factory Pipeline and have following challenge currently: I'd like to make an API call (Post) which requires some data with the syntax of array and in it, multiple objects .我正在研究Azure 数据工厂管道,目前面临以下挑战:我想做一个 API 调用(Post),它需要一些具有array语法的数据,其中包含多个objects

Now - both, the data retrieving (from SQL db) and API call work when used independently (In case of the API call: I've been using hardcoded mock data for the body).现在 - 数据检索(来自SQL db)和API调用都在独立使用时工作(如果是 API 调用:我一直在使用硬编码的数据模拟)。 The challenge is in connecting both of them.挑战在于将两者联系起来。 That means: I'd like to get multiple rows out of a SQL table, convert them to the required json structure and fill that data then into the API call.这意味着:我想从 SQL 表中获取多行,将它们转换为所需的 json 结构并将该数据填充到 API 调用中。 See picture below:见下图:

在此处输入图像描述

In simple steps explained again:在简单的步骤中再次解释:

  1. Get rows from SQL table从 SQL 表中获取行
  2. Convert each row into an object eg { "somekey": valueOfRow}将每一行转换为 object 例如{ "somekey": valueOfRow}
  3. Collect all objects in an array收集array中的所有对象
  4. Provide array to API callAPI调用提供array

I'm just unsure how to proceed with the connection.我只是不确定如何进行连接。

Additional Information附加信息

As requested, some further detailed information.根据要求,提供一些更详细的信息。

Currently the API call uses following hardcoded mockdata:目前 API 调用使用以下硬编码模拟数据:

[{"idType": "ID_ISIN", "idValue": "US0123456789" }]

From the dataflow I'll get rows with one column called isin (with row values such as US0123456789 )从数据流中,我将获得一列名为isin的行(具有诸如US0123456789类的行值)

The goal is to fill API's body dynamically such that it receives something like this:目标是动态填充 API 的主体,使其接收如下内容:

[
    {
        "idType": "ID_ISIN", 
        "idValue": "US0123456789" 
    },
    {
        "idType": "ID_ISIN", 
        "idValue": "US9876543210" 
    },
    {...}

]

I saw that one can achieve something similiar with SQL query - see: https://docs.microsoft.com/en-us/azure/azure-sql/database/json-features?view=azuresql我看到一个可以实现与SQL查询类似的东西 - 请参阅: https://docs.microsoft.com/en-us/azure/azure-sql/database/json-features?view=azuresql

But I'd miss the part "idType": "ID_ISIN" in each row/object.但我会错过每行/对象中的"idType": "ID_ISIN"

Using dataflow to retrieve records and create an array of objects (where each object is row from SQL table) might not be the right way to achieve your requirement.使用数据流检索记录并创建对象数组(其中每个 object 是 SQL 表中的行)可能不是满足您要求的正确方法。

  • We can use Lookup activity which returns the rows based on given table or query as an array of objects.我们可以使用Lookup活动,它根据给定的表或查询返回行作为对象数组。 Look at the following demonstration.看下面的演示。

  • The following is the data in my table repro1 which I am going to use (we only need ID_ISIN column).以下是我要使用的表repro1中的数据(我们只需要 ID_ISIN 列)。

在此处输入图像描述

  • In your data factory studio, create a new lookup activity.在您的数据工厂工作室中,创建一个新的查找活动。 Create a new source dataset for your SQL database table and click OK.为您的 SQL 数据库表创建一个新的源数据集,然后单击确定。在此处输入图像描述

  • Uncheck First Row only checkbox.取消选中First Row only复选框。 There is no need to select a table here, instead we can just query from the required table based on requirement.这里不需要 select 一个表,我们可以根据需要从需要的表中查询。 Therefore, check the Query box.因此,请选中Query框。

  • Now, I have written the following query to extract data as required from the repro1 table (such that it would give results which are in line with hardcoded mock data).现在,我编写了以下查询来根据需要从repro1表中提取数据(这样它会给出与硬编码的模拟数据一致的结果)。

select 'ID_ISIN' as idType, ID_ISIN as idValue from repro1

在此处输入图像描述

  • Now when I debug the pipeline, you can see the debug output of the lookup activity.现在当我调试管道时,您可以看到查找活动的调试 output。在此处输入图像描述

  • The above output consists of lot of other information along with the rows returned as a result of given query.上面的 output 包含许多其他信息以及作为给定查询结果返回的行。

  • Now you can directly retrieve the above highlighted array of objects using the following dynamic content (if your lookup activity name is Lookup1).现在您可以使用以下动态内容直接检索上面突出显示的对象数组(如果您的查找活动名称是 Lookup1)。

@activity('Lookup1').output.value

You can use the above dynamic content (an array of objects) to fill your API call's body.您可以使用上述动态内容(对象数组)来填充 API 调用的主体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM