简体   繁体   English

如何使用 Azure 数据工厂从 SQL 服务器读取 XML 列数据

[英]How to read XML column data from SQL server using Azure data Factory

There is a table in Azure SQL server and that table has one field call request which is of xml data type. Azure SQL 服务器中有一个表,该表有一个字段调用请求,它是xml数据类型。 We are reading the table in Azure Data Factory, so when we have created dataset in Azure data factory it is coming as XML but while using that dataset as a source in data flow it is coming as a string .我们正在阅读 Azure 数据工厂中的表格,因此当我们在Azure数据工厂中创建数据集时,它会以XML的形式出现,但在使用该数据流数据集作为源时,它会作为源出现。

Can anyone tell us how to parse XML column data in Azure data factory.谁能告诉我们如何解析 Azure 数据工厂中的 XML 列数据。 Thanks in advance.提前致谢。

We want to fetch the information from that XML data column and dump it to another table in Azure SQL server after performing aggregation on it.我们想从 XML 数据列中获取信息,并在对其执行聚合后将其转储到 Azure SQL 服务器中的另一个表中。

Here is the sample data of that table.这是该表的示例数据。

这是该表的示例数据

Azure Data Factory (ADF) does not have great support for XML but Azure SQL DB does. Azure 数据工厂 (ADF) 对 XML 没有很好的支持,但 Azure Z97788840A01041CB0B0 支持。 As your source and target are the same you could create a stored procedure which shreds the XML.由于您的源和目标相同,您可以创建一个存储过程来粉碎 XML。 Then use ADF's Stored Proc activity to call it if you need to schedule the activity.然后,如果您需要安排活动,请使用 ADF 的 Stored Proc 活动来调用它。

Here is a simple example of shredding XML in Azure SQL DB:这是在 Azure SQL DB 中粉碎 XML 的简单示例:

------------------------------------------------------------------------------------------------
-- Setup START
------------------------------------------------------------------------------------------------

DROP TABLE IF EXISTS dbo.students 
DROP TABLE IF EXISTS dbo.yourTarget
GO

CREATE TABLE dbo.students (
    studentId       INT NOT NULL PRIMARY KEY,
    studentName     VARCHAR(20) NOT NULL,
    request         XML NOT NULL
    )
GO


CREATE TABLE dbo.yourTarget (
    studentId       INT NOT NULL,
    customerno      INT NOT NULL,
    operation       VARCHAR(20) NOT NULL,
    email           VARCHAR(100) NOT NULL
    )
GO

-- Setup END
------------------------------------------------------------------------------------------------


------------------------------------------------------------------------------------------------
-- Test data START
------------------------------------------------------------------------------------------------

INSERT INTO dbo.students ( studentId, studentName, request )
VALUES 
    ( 1, 'xxx', '<Customers><row><CUSTOMERNO>12</CUSTOMERNO><OPERATION>INSERT</OPERATION><EMAIL>bill.gates@microsoft.com</EMAIL></row></Customers>' ),
    ( 2, 'yyy', '<Customers><row><CUSTOMERNO>13</CUSTOMERNO><OPERATION>INSERT</OPERATION><EMAIL>bill.gates@microsoft.com</EMAIL></row></Customers>' ),
    ( 3, 'zzz', '<Customers><row><CUSTOMERNO>14</CUSTOMERNO><OPERATION>INSERT</OPERATION><EMAIL>bill.gates@microsoft.com</EMAIL></row></Customers>' ),
    ( 4, 'xyz', '<Customers><row><CUSTOMERNO>100</CUSTOMERNO><OPERATION>INSERT</OPERATION><EMAIL>bill.gates@microsoft.com</EMAIL></row></Customers>' )
GO

-- Test data END
------------------------------------------------------------------------------------------------


------------------------------------------------------------------------------------------------
-- Shred XML START
------------------------------------------------------------------------------------------------

INSERT INTO dbo.yourTarget ( studentId, customerno, operation, email )
SELECT
    s.studentId,
    c.c.value( '(CUSTOMERNO/text())[1]', 'INT' ) customerno,
    c.c.value( '(OPERATION/text())[1]', 'VARCHAR(20)' ) operation,
    c.c.value( '(EMAIL/text())[1]', 'VARCHAR(100)' ) email  
FROM dbo.students s
    CROSS APPLY s.request.nodes('Customers/row') c(c)

-- Shred XML END
------------------------------------------------------------------------------------------------

GO


-- Results
SELECT *
FROM dbo.yourTarget

Wrap up your logic into a stored proc for scheduling.将您的逻辑包装到存储过程中以进行调度。 If your databases were on different servers you could still use the T-SQL for shredding the XML in a dataset so you are presenting columns to ADF not XML.如果您的数据库位于不同的服务器上,您仍然可以使用 T-SQL 来粉碎数据集中的 XML,因此您向 ADF 而非 XML 呈现列。

My results:我的结果:

我的结果

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

相关问题 Migrating Data from a SQL Server Encrypted Table to SQL Azure using Azure Data Factory Copy data - Migrating Data from a SQL Server Encrypted Table to SQL Azure using Azure Data Factory Copy data Can we create table in SQL by passing column information from Azure blob using Azure data flow or Azure data factory? - Can we create table in SQL by passing column information from Azure blob using Azure data flow or Azure data factory? 如何在 SQL 服务器中编写 xquery 以从表中的 XML 数据列中读取所有子节点? - How to write a xquery in SQL Server to read all the childnodes from an XML data column in a table? 从SQL Server中提取Azure数据工厂作业的作业详细信息 - Pulling job details for an Azure Data Factory Job from SQL Server 从SQL Server中的XML读取数据 - Read data from XML in SQL Server 从sql server表读取XML数据 - Read XML Data From sql server Table 如何从 SQL Server 中的多个 XML 文件中读取数据? - How to read data from multiple XML files in SQL Server? 使用数据工厂将嵌套对象从 SQL Server 复制到 Azure CosmosDB - Copy nested objects from SQL Server to Azure CosmosDB using a Data Factory 如何使用 Azure 数据工厂将数据从 excel 加载到 SQL DB - How to load data from excel to SQL DB using Azure Data Factory 如何在sql server中读取xml中的varbinary数据 - how to read varbinary data in xml in sql server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM