PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/CsvReader.Azure/AzureDataTableExtensions.cs

https://github.com/tpwalke2/DataTable
C# | 136 lines | 63 code | 12 blank | 61 comment | 1 complexity | f68506d4323a2b5ccadfd8e418ba0485 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.WindowsAzure;
  6. using Microsoft.WindowsAzure.StorageClient;
  7. using System.IO;
  8. using System.Diagnostics;
  9. namespace DataAccess
  10. {
  11. /// <summary>
  12. /// Azure Extensions for DataTable instance.
  13. /// These generally support saving a datatable up to Azure as a blob or Azure Table.
  14. /// </summary>
  15. public static class DataTableAzureExtensions
  16. {
  17. /// <summary>
  18. /// Save the data table to the given azure blob. This will overwrite an existing blob.
  19. /// </summary>
  20. /// <param name="table">instance of table to save</param>
  21. /// <param name="account">azure acount</param>
  22. /// <param name="containerName">conatiner name</param>
  23. /// <param name="blobName">blob name</param>
  24. public static void SaveToAzureBlob(this DataTable table, CloudStorageAccount account, string containerName, string blobName)
  25. {
  26. CloudBlobContainer container = DataTableBuilderAzureExtensions.GetContainer(account, containerName);
  27. SaveToAzureBlob(table, container, blobName);
  28. }
  29. /// <summary>
  30. /// Save the data table to the given azure blob. This will overwrite an existing blob.
  31. /// </summary>
  32. /// <param name="table">instance of table to save</param>
  33. /// <param name="container">conatiner</param>
  34. /// <param name="blobName">blob name</param>
  35. public static void SaveToAzureBlob(this DataTable table, CloudBlobContainer container, string blobName)
  36. {
  37. var blob = container.GetBlobReference(blobName);
  38. using (BlobStream stream = blob.OpenWrite())
  39. using (TextWriter writer = new StreamWriter(stream))
  40. {
  41. table.SaveToStream(writer);
  42. }
  43. }
  44. /// <summary>
  45. /// Save to azure table, typing all columns as strings.
  46. /// Overwrite if the table already exists
  47. /// Fabricate a partition and row key is they're not provided in the table.
  48. /// </summary>
  49. /// <param name="table">datatable to save</param>
  50. /// <param name="account">cloud account to write to</param>
  51. /// <param name="tableName">azure table name to save as. </param>
  52. public static void SaveToAzureTable(this DataTable table, CloudStorageAccount account, string tableName)
  53. {
  54. // When no types are provided, just assume they're all strings.
  55. int len = table.ColumnNames.Count();
  56. Type[] columnTypes = new Type[len];
  57. for (int i = 0; i < len; i++)
  58. {
  59. columnTypes[i] = typeof(string);
  60. }
  61. table.SaveToAzureTable(account, tableName, columnTypes);
  62. }
  63. public static void SaveToAzureTable(this DataTable table, CloudStorageAccount account, string tableName, Type[] columnTypes)
  64. {
  65. table.SaveToAzureTable(account, tableName, columnTypes, funcComputeKeys : null);
  66. }
  67. /// <summary>
  68. /// Save the datatable up to an AzureTable. Overwrite if the azure table already exists.
  69. /// </summary>
  70. /// <param name="table">source table to save</param>
  71. /// <param name="account">azure storage account</param>
  72. /// <param name="tableName">name of azure table to write to. </param>
  73. /// <param name="columnTypes">parallel array to table.ColumnNames.
  74. /// Strong typing for the columns in the azure table. Column i is skipped if columnTypes[i] is null.
  75. /// ColumnTypes should be types that can be normalized to OData (string,byte,sbyte,i16,i32,i64,double,single,boolean,decimal, datetime, guid).
  76. /// </param>
  77. /// <param name="funcComputeKeys">function to compute the partion and row keys for each row. </param>
  78. public static void SaveToAzureTable(this DataTable table, CloudStorageAccount account, string tableName, Type[] columnTypes, Func<int, Row, ParitionRowKey> funcComputeKeys)
  79. {
  80. GenericTableWriter.SaveToAzureTable(table, account, tableName, columnTypes, funcComputeKeys);
  81. }
  82. }
  83. /// <summary>
  84. /// Class to encapsulate a partition and row key. This is similar to Tuple[string,string], but less ambiguous.
  85. /// Partition plus Row key must be unique.
  86. /// </summary>
  87. public class ParitionRowKey
  88. {
  89. /// <summary>
  90. /// Partition key to use for Azure Table.
  91. /// </summary>
  92. public string PartitionKey { get; set; }
  93. /// <summary>
  94. /// Row key to use for azure table.
  95. /// </summary>
  96. public string RowKey { get; set; }
  97. /// <summary>
  98. /// Empty constructor. Set the partition and row key via the properties.
  99. /// </summary>
  100. public ParitionRowKey()
  101. { }
  102. /// <summary>
  103. /// initialize a container for an parition key and row key pair.
  104. /// </summary>
  105. /// <param name="partitionKey">partition key for azure table row</param>
  106. /// <param name="rowKey">Row key for azure table row.</param>
  107. public ParitionRowKey(string partitionKey, string rowKey)
  108. {
  109. PartitionKey = partitionKey;
  110. RowKey = rowKey;
  111. }
  112. /// <summary>
  113. /// initialize a container for an parition key and row key pair.
  114. /// Overload to pad rowkey with 0s so that rows sort nicely as strings.
  115. /// </summary>
  116. /// <param name="partitionKey">partition key for azure table row</param>
  117. /// <param name="rowKey">Row key for azure table row. pad rowkey with 0s so that it sorts nicely. </param>
  118. public ParitionRowKey(string partitionKey, int rowKey)
  119. {
  120. PartitionKey = partitionKey;
  121. RowKey = rowKey.ToString("D8");
  122. }
  123. }
  124. }