c# - 创建文件作为流并上传到 Azure

标签 c# azure parquet choetl

我正在使用 ChoETL 和 ChoETL.Parquet 库根据其他一些数据创建 parquet 文件。我可以在本地很好地创建该文件。

  using (ChoParquetWriter parser = new ChoParquetWriter($"..\\..\\..\\parquet_files\\{club}_events.parquet"))
       {
           parser.Write(events);
       }

在此代码片段中,事件是包含字符串的对象列表。它们将被转换为 Parquet 数据。

到目前为止,我已经编写了上传到 Azure 的代码,但它需要本地文件作为输入。

BlobServiceClient BlobServiceClient = new BlobServiceClient("REDACTED");
var containerClient = BlobServiceClient.GetBlobContainerClient("base-test");
BlobClient blobClient = containerClient.GetBlobClient($"Base/{RequestTime.Year}/{RequestTime.Month}/{RequestTime.Day}/{RequestTime.Hour}/{RequestTime.Minute}/events.parquet");
using FileStream uploadFileStream = File.OpenRead("..\\..\\..\\events.parquet"); 
await blobClient.UploadAsync(uploadFileStream, true);
uploadFileStream.Close();

我需要在内存中创建它,然后上传到 Azure blob 存储。我怎样才能做到这一点?澄清一下:我需要上传 Parquet 文件。

最佳答案

关于这个问题,您可以使用BlockBlobClient.OpenWriteAsync方法获取流并为ChoParquetWriter提供该流。然后写入器将直接将内容写入Azure blob。

例如

  List<EmployeeRecSimple> objs = new List<EmployeeRecSimple>();

            EmployeeRecSimple rec1 = new EmployeeRecSimple();
            rec1.Id = 1;
            rec1.Name = "Mark";
            objs.Add(rec1);

            EmployeeRecSimple rec2 = new EmployeeRecSimple();
            rec2.Id = 2;
            rec2.Name = "Jason";
            objs.Add(rec2);

            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            var desContainer = blobServiceClient.GetBlobContainerClient("output");
            var desBlob= desContainer.GetBlockBlobClient("my.parquet");
            var options = new BlockBlobOpenWriteOptions {
                HttpHeaders = new BlobHttpHeaders {
                    ContentType = MimeMapping.GetMimeMapping("parquet"),
                },
                // progress updates about data transfers
                ProgressHandler = new Progress<long> (
                    progress => Console.WriteLine("Progress: {0} bytes written", progress))
                    
                
            };

            using (var outStream = await desBlob.OpenWriteAsync(true, options).ConfigureAwait(false))
            using (ChoParquetWriter parser = new ChoParquetWriter(outStream)) {

                parser.Write(objs);
            }

public partial class EmployeeRecSimple
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

enter image description here

关于c# - 创建文件作为流并上传到 Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65041373/

相关文章:

c# - 格式错误的引用元素

c# - Azure 工作进程间通信

Azure : GroupsClient. BaseClient.Get():意外状态 403,带有 OData 错误:Authorization_RequestDenied:权限不足

apache-spark - 使用 Spark 生成拼花数据文件以测试 Hive/Presto/Drill/等的快速方法是什么?

python - 如何使用 Pandas 编写分区的 Parquet 文件

c# - 当 visible=false 时,asp.net requiredfieldvalidator 不工作

c# - 即使找到键, Entity Framework 也会为导航属性插入行

Azure 移动服务 PullAsync 并非所有数据

apache-spark - 快速压缩

c# - Xamarin Forms : System. 反射.TargetInvocationException