static void CreateCustomerMetadata(CloudTableClient tableClient) { Console.WriteLine("Creating customers metadata..."); CloudTable customersMetadataTable = tableClient.GetTableReference("customersmetadata"); customersMetadataTable.CreateIfNotExists(); var msftAddress1 = new DictionaryTableEntity(); msftAddress1.PartitionKey = "MSFT"; msftAddress1.RowKey = "ADDRESS-" + Guid.NewGuid().ToString("N").ToUpper(); msftAddress1.Add("city", "Seattle"); msftAddress1.Add("street", "111 South Jackson"); var msftWebsite1 = new DictionaryTableEntity(); msftWebsite1.PartitionKey = "MSFT"; msftWebsite1.RowKey = "WEBSITE-" + Guid.NewGuid().ToString("N").ToUpper(); msftWebsite1.Add("url", "http://www.microsoft.com"); var msftWebsite2 = new DictionaryTableEntity(); msftWebsite2.PartitionKey = "MSFT"; msftWebsite2.RowKey = "WEBSITE-" + Guid.NewGuid().ToString("N").ToUpper(); msftWebsite2.Add("url", "http://www.windowsazure.com"); var batch = new TableBatchOperation(); batch.Add(TableOperation.Insert(msftAddress1)); batch.Add(TableOperation.Insert(msftWebsite1)); batch.Add(TableOperation.Insert(msftWebsite2)); customersMetadataTable.ExecuteBatch(batch); Console.WriteLine("Done. Press ENTER to read the customer metadata."); Console.ReadLine(); }
public async Task Persist(ProjectionMetaData metadata) { Trace.TraceInformation("Preparing batch for projection {0}.", metadata.ProjectionType); var batch = new TableBatchOperation(); var entity = new DictionaryTableEntity { PartitionKey = metadata.ProjectionType, RowKey = Handler.Current() }; entity.Add("ProjectionHash", metadata.ProjectionHash); batch.Add(TableOperation.InsertOrReplace(entity)); Trace.TraceInformation("Executing batch for projection {0}.", metadata.ProjectionType); await _table.ExecuteBatchAsync(batch).ContinueWith(r => { Trace.TraceInformation("Batch for projection {0} complete {1} exceptions.", metadata.ProjectionType, r.Exception != null ? "with" : "without"); if (r.Exception != null) { r.Exception.Handle(exception => { Trace.TraceError(exception.ToString()); return true; }); } }); }
public void TestCreateEntity() { // retrieve the connection string NameValueCollection appsettings = ConfigurationManager.AppSettings; string storageConnectionString = appsettings["StorageConnectionString"]; WindowsAzureTableStorageService dao = new WindowsAzureTableStorageService(storageConnectionString); dao.createCloudTableClient(); dao.CreateTable("test"); DictionaryTableEntity entity = new DictionaryTableEntity(); entity.PartitionKey = "Test"; entity.RowKey = Guid.NewGuid().ToString(); entity.Add("city", "seattle"); entity.Add("street", "111 South Jackson"); dao.AddEntity("test", entity); }
public async Task Persist(string streamtype, string id, IEnumerable<ISourcedEvent> pendingEvents) { Trace.TraceInformation("Preparing batch for stream {0}.", streamtype); var batch = new TableBatchOperation(); foreach (var @event in pendingEvents) { var entity = new DictionaryTableEntity { PartitionKey = streamtype, RowKey = id + "_" + @event.Version.ToString(VersionKeyFormat) }; var compressed = false; var body = Json.Encode(@event); if (body.Length > 32*1024) { body = new GzipCompression().Compress(body); compressed = true; } entity.Add("EventType", @event.GetType().FullName); entity.Add("SourceId", @event.SourceId); entity.Add("Version", @event.Version); entity.Add("Body", body); entity.Add("Compressed", compressed); batch.Add(TableOperation.Insert(entity)); } Trace.TraceInformation("Executing batch on stream {0}.", streamtype); await _table.ExecuteBatchAsync(batch).ContinueWith(r => { Trace.TraceInformation("Batch on stream {0} complete {1} exceptions.", streamtype, r.Exception != null ? "with" : "without"); if (r.Exception != null) { r.Exception.Handle(exception => { Trace.TraceError(exception.ToString()); return true; }); } }); }
public void TestCreateBatchesLessThan100Records() { // retrieve the connection string NameValueCollection appsettings = ConfigurationManager.AppSettings; string storageConnectionString = appsettings["StorageConnectionString"]; WindowsAzureTableStorageService dao = new WindowsAzureTableStorageService(storageConnectionString); dao.createCloudTableClient(); dao.CreateTable("test"); List <ITableEntity> entities = new List <ITableEntity>(); for (int i = 0; i < 50; i++) { DictionaryTableEntity entity = new DictionaryTableEntity(); entity.PartitionKey = "Test"; entity.RowKey = Guid.NewGuid().ToString(); entity.Add("city", "seattle"); entity.Add("street", "111 South Jackson"); entities.Add(entity); } dao.AddBatch("test", entities); }
static void Main(string[] args) { try { // load configuration settings from the app.config // output debug information to the console TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out); Debug.Listeners.Add(myWriter); NameValueCollection appsettings = ConfigurationManager.AppSettings; string fileName = appsettings["FileName"]; // get configuration data from App.Config string partitionKeyField = appsettings["PartitionKeyField"]; string rowKeyField = appsettings["RowKeyField"]; string azureTableName = appsettings["AzureTableName"]; string storageConnectionString = appsettings["StorageConnectionString"]; string maximumRowsToImportString = appsettings["MaximumRowsToImport"]; string maximumTasksString = appsettings["MaximumTasks"]; string startingOffsetString = appsettings["startingOffset"]; int maximumRowsToImport = 0; if (maximumRowsToImportString != null) { maximumRowsToImport = int.Parse(maximumRowsToImportString); } int maximumTasks = 0; if (maximumTasks != null) { maximumTasks = int.Parse(maximumTasksString); } int startingOffset = 0; if (startingOffsetString != null) { startingOffset = int.Parse(startingOffsetString); } // create storage service and list of entities to populate List <ITableEntity> entities = new List <ITableEntity>(); WindowsAzureTableStorageService tableStorageService = new WindowsAzureTableStorageService(storageConnectionString); tableStorageService.CreateTable(azureTableName); // open the file and start reading StreamReader textReader = File.OpenText(fileName); CsvReader reader = new CsvReader(textReader); while (reader.Read()) { if (reader.Row - 1 < startingOffset) { // Row -1 is because the header // do nothing - move read to the startingOffset } else { // populate an entity for each row DictionaryTableEntity entity = new DictionaryTableEntity(); if (rowKeyField == null) { entity.RowKey = Guid.NewGuid().ToString(); } foreach (string field in reader.FieldHeaders) { if (field == rowKeyField) { entity.RowKey = reader[field]; } else if (field == partitionKeyField) { entity.PartitionKey = WindowsAzureTableStorageService.createValidPartitionKey(reader[field]); } else { string value = reader[field]; entity.Add(field, value); } } if (entity.PartitionKey == null) { throw new Exception("Bad data record. Partition key not found."); } entities.Add(entity); if (maximumRowsToImport > 0 && entities.Count == maximumRowsToImport) { break; } } } Console.Out.WriteLine("Starting Upload of " + entities.Count + " entities to Windows Azure Table Storage at " + DateTime.Now); var task = tableStorageService.AddBatchAsync("test", entities, maximumTasks); task.Wait(); Console.Out.WriteLine("Finished Upload to Windows Azure Table Storage at " + DateTime.Now); } catch (Exception e) { Console.Write(e.Message); } }