【问题标题】:Cannot read Excel file using U-SQL无法使用 U-SQL 读取 Excel 文件
【发布时间】:2019-03-27 09:22:07
【问题描述】:

我正在尝试读取 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"
}

【问题讨论】:

  • 您是没有遗漏 REFERENCE ASSEMBLY 指令,还是没有发布整个脚本?示例见github.com/Azure/AzureDataLake/blob/master/Samples/…
  • 我应该写USING new ExcelExtractor("result01");而不是USING new oh22is.Analytics.Formats.ExcelExtractor("result01");
  • 不,你必须注册并引用程序集,如示例中所述

标签: azure azure-data-factory u-sql


【解决方案1】:

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

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

遗憾的是,据我所知,这些文件并不是单独分发的,而且微软似乎要求您使用 Visual Studio 手动编译 from sourcethis repository。使用VS的好处是你可以直接引用程序集来加快你的开发(但我觉得没有意义,因为我只用它来提取excel,只需要生成一次文件)。编译过程还应该为您提供来自documentformat.openxmlpackage 的动态链接库,这样您就不必下载它或从.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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多