USQL-如何使用xml提取器从xml文件中提取属性值

时间:2019-06-14 13:48:48

标签: xml u-sql

如何使用U-SQL作业使用自定义提取器从XML文件中提取属性值。我可以从XML文件中提取子元素值。

sample Xml File:
<?xml version="1.0" encoding="UTF-8"?>
<Users>
<User ID="001">
    <FirstName>david</FirstName>
    <LastName>bacham</LastName>
</User>
<User ID="002">
  <FirstName>xyz</FirstName>
  <LastName>abc</LastName>
</User>
</Users>

我可以使用以下代码提取名和姓。如何获取ID值作为csv文件的一部分。

示例U sql作业:

REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];
@input = EXTRACT 
  FirstName string,
  LastName string 
  FROM @"/USERS.xml"
  USING new Microsoft.Analytics.Samples.Formats.Xml.XmlExtractor("User",
    new SQL.MAP<string, string> { 
    {"FirstName","FirstName"},
    {"LastName","LastName"}
 );

 @output = SELECT * FROM @input;

 OUTPUT @output
 TO "/USERS.csv"
 USING Outputters.Csv();

1 个答案:

答案 0 :(得分:1)

您可以在Databricks中轻松完成此操作,例如

%sql
CREATE TABLE User
USING com.databricks.spark.xml
OPTIONS (path "/FileStore/tables/input42.xml", rowTag "User")

然后阅读表格:

%sql
SELECT *
FROM User;

如果必须使用U-SQL进行操作,则使用Formats assembly中的XmlDomExtractor对我有用:

REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];

DECLARE @inputFile string = "/input/input40.xml";

@input =
    EXTRACT 
        id string,
        firstName string,
        lastName string
    FROM @inputFile
    USING new Microsoft.Analytics.Samples.Formats.Xml.XmlDomExtractor(rowPath : "/Users/User",
          columnPaths : new SQL.MAP<string, string>{
          { "@ID", "id" },
          { "FirstName", "firstName" },
          { "LastName", "lastName" }
          }
          );


@output =
    SELECT *
    FROM @input;


OUTPUT @output
TO "/output/output.csv"
USING Outputters.Csv();

我的结果:

My results