将 csv 数据直接写入 azure blob 存储

Tho*_*ato 4 azure-blob-storage

我有一个包含大约 100 万个数据对象的大型数组。我找到了很多用于将文件上传到 azure blob 存储的示例。我想你可以用内存流来做到这一点,但我还没有找到从对象中做到这一点的示例。我不确定对于这么大的数据,你是否应该逐行写出我有哪些选项。欢迎所有意见,如果有一些样品就更完美了。目标是将数据对象写入 Azure Blob 存储中的 csv 文件。

Pet*_*Pan 5

我假设你正在使用最新版本的 Azure Storage SDK for .NET (9.3.3) 用 C# 编写代码。

下面是我的代码,用于实现将大量数据对象直接写入 Azure Blob 存储的需求。

using System;
using System.Collections;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

namespace WriteCSVDataToBlob
{

    class Record
    {
        string[] cols;

        public Record(string[] cols)
        {
            this.cols= cols;
        }

        override public string ToString()
        {
            return String.Join(',', cols);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var arr = new ArrayList();
            arr.Add(new Record(new string[]{ "A", "B","one" }));
            arr.Add(new Record(new string[] { "C", "D","two"}));
            string storageConnectionString = "<your storage connection string>";
            var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            var client = storageAccount.CreateCloudBlobClient();
            var container = client.GetContainerReference("test");
            var blob = container.GetBlockBlobReference("data.txt");
            using (CloudBlobStream x = blob.OpenWriteAsync().Result)
            {
                foreach(var rec in arr)
                {
                    x.Write(System.Text.Encoding.Default.GetBytes(rec.ToString()+"\n"));
                }
                x.Flush();
                x.Close();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)