首页 > 解决方案 > 无法使用 U-SQL 读取 Excel 文件

问题描述

我正在尝试读取 Excel 文件,并且必须在 Azure Datalake 中写入 csv 文件。当我尝试这样做时,它会显示错误。

U-SQL 脚本:

DECLARE @ExcelFile = @"/Output/demog_data_Merged_08022017.xlsx";

@result01 = EXTRACT Id string,
UNIQUE_ID long,
SOL_ID int,
EMAIL_ID string,
mobilenumber string,
CUST_OPN_DATE DateTime,
gender char,
age int,
CUR_CITY string,
CUR_COUNTRY string,
CUR_PIN string,
NRE_CNTRY string,
MARITAL_STATUS char,
FREZ_CODE char,
UNFREEZ_DATE DateTime,
LAST_FREZ_DATE DateTime,
DORMANCY_STATUS char,
AVAILABLE_AMOUNT double,
ACCOUNT_OPEN_DATE DateTime,
nullcol string,
Salaried_account_flag char,
ACCOUNT_TYPE string
FROM @ExcelFile
USING new oh22is.Analytics.Formats.ExcelExtractor("result01");


@result02 = SELECT * FROM @result01;

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

错误:

{
    "errorCode": "2703",
    "message": "Error Id: E_CSC_USER_INVALIDCSHARP, Error Message: C# error CS0246: The type or namespace name 'oh22is' could not be found (are you missing a using directive or an assembly reference?). ",
    "failureType": "UserError",
    "target": "U-SQL1"
}

标签: azureazure-data-factoryu-sql

解决方案


没有程序集引用就无法读取 Excel 文件。您需要这些文件DocumentFormat.OpenXml.dlloh22is.Analytics.Formats.dll在您的数据湖目录中,以及您的 excel 文件(不一定在同一个文件夹中)。

程序集引用保存文件读取逻辑,并充当数据的 u-sql 内部表示和文件格式之间的网关,生成可以处理的数据。

遗憾的是,据我所知,这些文件并不是单独分发的,微软似乎要求您使用Visual Studio从源代码手动编译它。使用VS的好处是你可以直接引用程序集来加快你的开发(但我觉得没有意义,因为我只用它来提取excel,只需要生成一次文件)。documentformat.openxml 编译过程还应该为您提供包中的动态链接库,这样您就不必下载它或从.nupkg文件中提取它,如果您这样做,更喜欢使用 的版本/lib/net40/DocumentFormat.OpenXml.dll,这是适用的我的 xlsx 文件(2007-2019 格式)。

将程序集文件(两个.dll文件)放入数据湖后,记下它们的路径并像以下 u-sql 脚本一样使用它们:

// Register the dependency to the analytics assembly (xml file reader)
DROP ASSEMBLY IF EXISTS openxml;
CREATE ASSEMBLY openxml FROM @"/MyProject/Assemblies/DocumentFormat.OpenXml.dll";
REFERENCE ASSEMBLY openxml;

// Register the analytics assembly that read our excel file
DROP ASSEMBLY IF EXISTS analytics;
CREATE ASSEMBLY analytics FROM @"/MyProject/Assemblies/oh22is.Analytics.Formats.dll";
REFERENCE ASSEMBLY analytics;

// Define a local variable for the excel file
DECLARE @ExcelFile = @"/MyProject/MyFolder/test-file.xlsx";

@sheet = EXTRACT
    A string,
    B string,
    C string
FROM @ExcelFile
    USING new oh22is.Analytics.Formats.ExcelExtractor("Sheet1");

//And you can save, transform, select it like you would use any other data:

OUTPUT (SELECT * FROM @sheet) TO "/MyProject/output.csv" USING Outputters.Csv();

推荐阅读