Azure 数据工厂获取元数据以获取 blob 文件名并将它们传输到 Azure SQL 数据库 table 第 2 部分

Azure Data Factory Get Metadata to get blob filenames and transfer them to Azure SQL database table part 2

我正在尝试在 Azure 数据工厂中使用获取元数据 activity 来获取 blob 文件名并将它们复制到 Azure SQL 数据库 table。 我在 Get Metadata activity 之后添加了存储过程 activity。这是我的新 activity 存储过程,我按照建议添加了参数,但是我将名称更改为 JsonData,因为我的存储过程需要此参数。

这是我的存储过程。

/****** Object:  StoredProcedure [dbo].[InsertDataJSON]    Script Date: 10/14/2020 11:01:30 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*USE datafactorypoc1*/

ALTER PROCEDURE [dbo].[InsertDataJSON] (
    @JsonData NVARCHAR (MAX)
)
AS
BEGIN
    DECLARE @err int

    INSERT INTO extractFileNameTest1 (ItemName, ItemType, EffIntegrationRunTieme, ExecutionDuration, DurationInQueue)
    SELECT ItemName, ItemType, EffIntegrationRunTieme, ExecutionDuration, DurationInQueue
    FROM OPENJSON (@JsonData, N'$') WITH (
        ItemName VARCHAR(255) N'$.ItemName',
        ItemType VARCHAR(255) N'$.ItemType',
        EffIntegrationRunTieme VARCHAR(255) N'$.EffIntegrationRunTieme',
        ExecutionDuration INT N'$.ExecutionDuration',
        DurationInQueue INT N'$.DurationInQueue'
    )    

    SELECT @err = @@ERROR
    RETURN (@err)
END

我在存储过程中遇到这个错误:

{
    "errorCode": "2402",
    "message": "Execution fail against sql server. Sql error number: 13609. Error Message: JSON text is not properly formatted. Unexpected character 'S' is found at position 0.",
    "failureType": "UserError",
    "target": "Stored procedure1",
    "details": []
}

但是当我检查输入时,它似乎已经成功读取了 json 字符串 itemName.

但是,当我检查输出时,它不在那里。

你能帮我看看我哪里做错了吗?是我的存储过程吗?非常感谢您。

更新 15/10/2020 我创建了一个新管道并将存储过程移动到 ForEach activity 以连接到 Get_Filename_2 而不是第一个广告:

这里我将参数的值更改为 Get_Filename_2 并输出为 itemName 而不是 childitems(因为我在使用 childitems 时出错,因为 childitems 来自 Get_Filename_1 而不是 2)。

执行管道(失败)后,最后一个存储过程的输入为:

{
    "storedProcedureName": "[dbo].[InsertDataJSON]",
    "storedProcedureParameters": {
        "JsonData": {
            "value": "FRRNSC84FIN1_A2276801_20200103-152534.json",
            "type": "String"
        }
    }
}

输出为:

{
    "effectiveIntegrationRuntime": "DefaultIntegrationRuntime (West Europe)",
    "executionDuration": 0,
    "durationInQueue": {
        "integrationRuntimeQueue": 0
    },
    "billingReference": {
        "activityType": "ExternalActivity",
        "billableDuration": [
            {
                "meterType": "AzureIR",
                "duration": 0.016666666666666666,
                "unit": "Hours"
            }
        ]
    }
}

对于此管道,失败并显示以下错误消息。

{
    "errorCode": "2402",
    "message": "Execution fail against sql server. Sql error number: 13609. Error Message: JSON text is not properly formatted. Unexpected character 'F' is found at position 0.\r\nFRRNSC84FIN1_A2276801_20200103-152534.json",
    "failureType": "UserError",
    "target": "Stored procedure1",
    "details": []
}

在我的旧管道上,我在 ForEach 循环之外存储了过程。管道没有失败:

这里是最后一个存储过程的输入activity:

{
    "storedProcedureName": "[dbo].[InsertDataJSON]",
    "storedProcedureParameters": {
        "JsonData": {
            "value": "[{\"name\":\"FRRNSC84FIN1_A2274001_20200103-143748_back_camera_0.jpeg\",\"type\":\"File\"},{\"name\":\"FRRNSC84FIN1_A2274001_20200103-143748_right_camera_0.jpeg\",\"type\":\"File\"},{\"name\":\"FRRNSC84FIN1_A2274801_20200103-144811_right_camera_0.jpeg\",\"type\":\"File\"},{\"name\":\"FRRNSC84FIN1_A2275201_20200103-145229_right_camera_0.jpeg\",\"type\":\"File\"},{\"name\":\"FRRNSC84FIN1_A2276801_20200103-152534.json\",\"type\":\"File\"}]"
        }
    }
}

这是输出:

{
    "effectiveIntegrationRuntime": "DefaultIntegrationRuntime (West Europe)",
    "executionDuration": 0,
    "durationInQueue": {
        "integrationRuntimeQueue": 0
    },
    "billingReference": {
        "activityType": "ExternalActivity",
        "billableDuration": [
            {
                "meterType": "AzureIR",
                "duration": 0.016666666666666666,
                "unit": "Hours"
            }
        ]
    }
}

这条管道 运行 成功了,但是 SQL 中的结果并不是我真正期望的,在第​​一个 json 字符串之前还有很多 NULL 值,但它插入了所有 itemName相同的位置。这是因为我认为它在 ForEach 循环之外。但是为什么插入这么多NULL?

这是我修改的存储过程:

/****** Object:  StoredProcedure [dbo].[InsertDataJSON]    Script Date: 15/10/2020 10:31:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*USE datafactorypoc1*/

ALTER PROCEDURE [dbo].[InsertDataJSON] (
    @JsonData NVARCHAR (MAX)
)
AS
BEGIN
    PRINT @JsonData

    /*INSERT INTO Logs values (DEFAULT, @JsonData)*/
    INSERT INTO extractFileNameTest1 values (@JsonData, DEFAULT)
    SELECT * FROM
        OPENJSON (@JsonData)
    WITH (
        ItemName VARCHAR(255) N'$.ItemName',
        ItemType VARCHAR(255) N'$.ItemType'
    )
END

我试图删除测试中的所有数据 SQL table 以查看第二个失败管道的输出。我注意到它确实正确解析了我的 table 中的 json 字符串文件名和所有文件名(我在 blob 存储中只有 5 个文件)。但是其他数据都是NULL。

总而言之,新管道 SQL table 的结果要好得多,每个文件名都在 table 的不同单元格上,但我得到了管道 运行 失败。你能帮我检查一下我在这里做错了什么吗?是存储过程还是我上一个存储过程中的表达式activity在ForEach循环中?

非常感谢您。

对于这种情况,我仅使用 1 个获取元数据 activity: Dataset of Get Metadata activity = 来自 Blob 存储的二进制文件 获取元数据的字段列表 = 子项

此获取元数据 activity 具有连接到 ForEach activity 的输出: ForEach activity 设置 => 项目 => @activity('Get_FileName_1').output.childItems

在ForEachactivity里面,有1个activity是Stored Procedure: 存储过程设置已链接服务 = Azure SQL 数据库与选定的存储过程。在我编写的存储过程中,我定义了将从 blob 文件名中读取的列。然后,对于存储过程的参数activity,我在存储过程中定义了完全相同的参数numbers = @JsonData 的所有列。 然后,对于参数的每个值,我使用 Azure 数据工厂函数 @substring 提取 blob 文件名的不同部分。然后将这些插入到 Azure SQL table.

中的正确列中

子字符串函数的来源:https://docs.microsoft.com/en-us/azure/data-factory/control-flow-expression-language-functions#substring

我在 sp 的帮助下加载 get_metadata activity 的输出时也遇到了同样的问题。

简单的答案是在将 get_metadata activity 的输出提供给 SP 参数时,您需要将其转换为字符串。如下所示:

@string(activity('get_metadata').output.childitems)