【问题标题】:ADLS ConcurrentAppend giving corrupt data for 1 MB filesADLS ConcurrentAppend 为 1 MB 文件提供损坏的数据
【发布时间】:2020-06-10 21:21:55
【问题描述】:

当我使用Parallel.For 循环将 10 个 1 MB 大小的文件同时附加到 Azure Data Lake Service 时,我只看到 Azure Data Lake 文件中最后两个文件的内容,尽管我看到正确的数据打印到控制台。

当我使用简单的 for 循环而不是 Parallel.For 时,附加到文件的数据是正确的。

有什么帮助吗?

Parallel.For(0, 10, i =>
{
    path[i] = @"C:\Users\t-chkum\Desktop\InputFiles\1MB\" + (i + 1) + ".txt";

    FileStream stream = File.OpenRead(path[i]);

    stream.Read(buffer, 0, buffer.Length);
    Console.WriteLine(Encoding.UTF8.GetString(buffer));


    client.ConcurrentAppend(fileName, true, buffer, 0, buffer.Length);

    stream.Close();
});

【问题讨论】:

  • 您使用的是 ADLS gen1 还是 gen2?您使用的是哪个 sdk(及其版本)?
  • 我正在使用 Gen1 和 .NET sdk(版本 3.1.100)
  • 此外,我尝试将数据打印到控制台,当我在调用 concurrentAppend API 之前打印它时,我会看到所有文件的内容,但是当我将其打印到控制台时,调用 concurrentAppend -> 只有最后一个文件内容显示在控制台上。
  • 知道了。我明天去看看。
  • 嘿!我想到了。在我阅读缓冲区时,这是一个关键部分

标签: c# concurrency task-parallel-library azure-data-lake parallel-for


【解决方案1】:

这实际上是一个临界区问题,可以使用块集合或锁来解决:

BlockingCollection<int> b = new BlockingCollection<int>(1);
Parallel.For(0, 10, i =>
{
    b.Add(i);
    path[i] = @"C:\Users\t-chkum\Desktop\InputFiles\1MB\" + (i + 1) + ".txt";
    FileStream stream = File.OpenRead(path[i]);

    stream.Read(buffer, 0, buffer.Length);

    client.ConcurrentAppend(fileName, true, buffer, 0, buffer.Length);

    Array.Clear(buffer, 0, buffer.Length);

    stream.Close();
    b.Take();
});

上面的代码解决了我的问题:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多