【问题标题】:Decompress .gz file in Azure Data lake在 Azure 数据湖中解压 .gz 文件
【发布时间】:2018-01-23 08:31:39
【问题描述】:

如何使用 c# asp.net 解压和读取 Azure 数据湖中的 .gz 文件

我尝试了以下代码,但它导致异常。

异常:找不到路径“D:\xxxxxx\filename”的一部分。

public static void Main(string[] args)
    {
        // Obtain AAD token
        var creds = new ClientCredential(applicationId, clientSecret);
        var clientCreds = ApplicationTokenProvider.LoginSilentAsync(tenantId, creds).GetAwaiter().GetResult();

        // Create ADLS client object
        AdlsClient client = AdlsClient.CreateClient(adlsAccountFQDN, clientCreds);

        try
        {
            // Enumerate directory
            foreach (var entry in client.EnumerateDirectory("/Test/"))
            {
                try
                {
                    string filename =entry.Name;
                    using (Stream fileStream = File.OpenRead(filename), zippedStream = new GZipStream(fileStream, CompressionMode.Decompress))
                    {
                        using (StreamReader reader = new StreamReader(zippedStream))
                        {

                            // work with reader
                            reader.ReadLine();

                        }
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }
        catch (AdlsException e)
        {
            PrintAdlsException(e);
        }

        Console.WriteLine("Done. Press ENTER to continue ...");
        Console.ReadLine();
    }

【问题讨论】:

  • 我不希望有文件位于 d:\ 中的 azure 数据湖存储中。你能发布更多代码吗?
  • @PeterBons 我已经用更多代码更新了这个问题。
  • 您可能想要使用 entry.FullName 而不是 entry.Name,这样它就有了路径的其余部分

标签: c# asp.net azure-data-lake


【解决方案1】:

我得到了解决方案。 我们应该使用 client.GetReadStream(entry.FullName) 而不是 File.OpenRead(filename)

代码是:

 foreach (var entry in client.EnumerateDirectory("/Test/"))
                {
                    StringBuilder lines = new StringBuilder();
                    try
                    {
                        using (Stream fileStream = client.GetReadStream(entry.FullName), zippedStream = new GZipStream(fileStream, CompressionMode.Decompress))
                        {
                            using (StreamReader reader = new StreamReader(zippedStream))
                            {
                                string line;
                                while ((line = reader.ReadLine()) != null)
                                {
                                    lines.AppendLine(line);
                                    Console.WriteLine(lines);
                                }
                            }
                        }
                    }

【讨论】:

    【解决方案2】:

    built-in extractors(Text、Csv、Tsv)现在原生支持 gzip 压缩文件,因此您无需执行任何特殊操作,只需阅读它们:

    @data =
        EXTRACT Timestamp DateTime,
                Event string,
                Value int
        FROM "/input/input.csv.gz"
        USING Extractors.Csv();
    

    这也适用于自定义提取器:

    @data =
        EXTRACT Timestamp DateTime,
                Event string,
                Value int
        FROM "/input/input.csv.gz"
        USING  new USQLworking.MyExtractor();
    

    请参阅 here 了解 Michael Rys 的进一步说明。

    【讨论】:

    • 他没有使用 u-SQL,所以我认为这不适用
    • 嗨@PeterBons,我认为他们正在尝试推出自己的方法,因为他们不知道内置的提取器本机处理 gzip 压缩文件,但是当然,他们可能正在尝试创建自定义提取器 - 但它仍然是名称;只需将文件命名为...something.gz
    猜你喜欢
    • 2012-08-23
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    相关资源
    最近更新 更多