解压缩Azure Data湖中的.gz文件

时间:2018-01-23 08:31:39

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

如何使用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();
    }

2 个答案:

答案 0 :(得分:3)

我得到了解决方案。 我们应该使用 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);
                                }
                            }
                        }
                    }

答案 1 :(得分:1)

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的进一步说明。