我有一大组跨越几百个文件的数据。显然,它有一些编码问题(主要是 UTF-8,但显然有些字符是无效的)。根据https://msdn.microsoft.com/en-us/library/azure/mt764098.aspx如果存在编码错误,无论将静默标志设置为 true 都会发生运行时错误(目的只是跳过错误行)。
因此,我需要编写一个自定义提取器。我在https://blogs.msdn.microsoft.com/data_otaku/2016/10/27/a-fixed-width-extractor-for-azure-data-lake写了一个主要是简化版本的示例-analytics/因为它只需要一行,用分隔符分割它,然后只返回 try 块中的值。如果有任何异常,我只是处理它们并继续前进。
不幸的是,我在 USQL 脚本本身中实际引用此提取器时遇到了问题。当我按照上述链接上的指导进行操作时,它建议在另一个程序集中编写逻辑,构建它,将其注册到 ADLS 数据库/程序集中,然后通过REFERENCE ASSEMBLY MyExtractors;
脚本顶部将其包含在内(因为这是使用的命名空间)。在下面的 Using 语句中,我将其调用为USING new SimpleExtractor();
If I do so, I get a error when running the script against ADLS service that the type or namespace cannot be found
. 此外,如果我尝试更精确并USING new MyExtractors.SimpleExtractor();
在 using 语句中使用,它会产生相同的错误,引用上面的 USING 语句。
然后,我在https://azure.microsoft.com/en-us/documentation/articles/data-lake-analytics-u-sql-develop-user-defined-operators/的旧源中找到了其他文档,其中描述了执行同样的事情,但在代码隐藏文件中。我删除了单独的程序集并将逻辑复制到该文件中的一个类中。第 6 步中的示例没有显示任何REFERENCE ASSEMBLY
语句,但同样,当我运行它时,我收到一个错误,即type or namespace name cannot be found
.
查看最新的发行说明,希望这里有些东西已经过时了,我唯一看到的是,如果我使用USING
语句,我需要在实际之前引用自定义代码的程序集(如第一次尝试)使用它,我就是。
谁能提供一些关于如何在 USQL 中正确引用 UDO 的指导,或者指出如何让运行时静默处理编码异常(并跳过它们)?
这是我的逻辑在提取器本身中的样子:
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.Analytics.Interfaces;
namespace Utilities
{
[SqlUserDefinedExtractor(AtomicFileProcessing = true)]
public class ModifiedTextExtractor : IExtractor
{
//Contains the row
private readonly Encoding _encoding;
private readonly byte[] _row_delim;
private readonly char _col_delim;
public ModifiedTextExtractor()
{
_encoding = Encoding.UTF8;
_row_delim = _encoding.GetBytes("\r\n");
_col_delim = '\t';
}
public override IEnumerable<IRow> Extract(IUnstructuredReader input, IUpdatableRow output)
{
//Read the input line by line
foreach (var current in input.Split(_row_delim))
{
using (var reader = new StreamReader(current, this._encoding))
{
var line = reader.ReadToEnd().Trim();
//If there are any single or double quotes in the line, escape them
line = line.Replace(@"""", @"\""");
var count = 0;
//Split the input by the column delimiter
var parts = line.Split(_col_delim);
foreach (var part in parts)
{
output.Set<string>(count, part);
count += 1;
}
}
yield return output.AsReadOnly();
}
}
}
}
以及我如何在 USQL 语句中使用它的片段(在将其注册为程序集之后):
REFERENCE ASSEMBLY [Utilities];
CREATE VIEW MyView AS ...
USING new Utilities.ModifiedTextExtractor();
谢谢!