2017-03-18 47 views
0

我有要求从Azure blob存储中读取csv文件并将内容加载到Azure表存储中。我成功完成了这部分工作,并且工作正常。我的代码根据列标题的位置从csv中检测列值,但不基于列名称。所以,我想进一步增强它,以便我的c#代码应该根据标题名称检测列值,而不是基于列位置。我探索了词典的选项,但我不确定是否可以使用它来实现。Azure:从基于头名称的blob存储中读取csv而不是位置

CSV内容是这样的:

PartitionKey,RowKey,王牌,年龄

TYSE,88010,A1,5

TYSE,88011,A2,4

这里是我的代码的核心部分,像col [0],col [1],col [2]等列分配等。

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); 
CloudBlobClient client = storageAccount.CreateCloudBlobClient(); 
CloudBlobContainer container = client.GetContainerReference("testblobcontainer"); 
CloudBlob blob = container.GetBlobReference("Test.csv"); 
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); 
CloudTable table = tableClient.GetTableReference("TestStorageTable"); 

int lineCount = 0; 

using (var stream1 = blob.OpenRead()) 
{ 
    using (StreamReader reader1 = new StreamReader(stream1)) 
    { 
     while (!reader1.EndOfStream) 
     { 
      lineCount++; 
      reader1.ReadLine(); 
     } 
    } 
} 

using (var stream = blob.OpenRead()) 
{ 
    using (StreamReader reader = new StreamReader(stream)) 
    { 
     while (!reader.EndOfStream) 
     { 
      string line = reader.ReadLine(); 
      if (lineCount > 0) 
      { 
       string[] cols = line.Split(','); 
       TestLiveTbl TestEntity = new TestLiveTbl(); 

       if ((cols[0] != "PartitionKey")) 
       { 
        string TempPartitionKey = cols[0].ToString(); 
        string TempRowKey = cols[1].ToString(); 

        TestEntity.TempPartKey = TempPartitionKey; 
        TestEntity.TempRwKey = TempRowKey; 
        TestEntity.Ace = cols[2].ToString(); 
        TestEntity.Age = Convert.ToDouble(cols[3]); 
        TestEntity.AssignPartitionKey(); 
        TestEntity.AssignRowKey(); 

        TestLiveTbl TestEntityIns = RetrieveRecord(table, TempPartitionKey, TempRowKey); 
        if (TestEntityIns == null) 
        { 
         TableOperation tableOperation = TableOperation.Insert(TestEntity); 
         table.Execute(tableOperation); 
         Console.WriteLine("Record inserted"); 

        } 
       } 

      } 
     } 
    } 
+0

我不确定您的实际问题是什么。也许你可以编辑你的问题,以包括你正在导入的内容的样本?此外,我不明白你在做什么临时分区/行键分配。 –

+0

嗨大卫,我编辑了我的问题...谢谢 – Nathan

回答

-1

也许你应该使用任何图书馆从NuGet这个? (例如 - csvHelper:http://joshclose.github.io/CsvHelper/)。

按名称映射可以帮助您)

+0

感谢您的评论。我使用DataTable并将csv值加载到数据表中,然后根据列名称读取DT并加载到Azure表存储中。无论列的位置如何,它都可以正常工作。 – Nathan

相关问题