示例#1
0
        public static void AddEntities <T>(string tablename, List <T> entitieslist) where T : ITableEntity, new()
        {
            //Get Caller Method name
            string callerMethodName = string.Empty;

            try
            {
                //Get Caller Method name from CallerInformation class
                callerMethodName = CallerInformation.TrackCallerMethodName();
                //get's azure table instance
                CloudTable          ArchivalTable  = GetAzureTableInstance(tablename);
                TableBatchOperation batchOperation = new TableBatchOperation();
                //insert list of entities into batch operation
                foreach (T entity in entitieslist)
                {
                    batchOperation.InsertOrReplace(entity);
                    if (batchOperation.Count == 100)
                    {
                        ArchivalTable.ExecuteBatch(batchOperation);
                        batchOperation = new TableBatchOperation();
                    }
                }
                if (batchOperation.Count > 0)
                {
                    ArchivalTable.ExecuteBatch(batchOperation);
                }
            }
            catch (Exception exception)
            {
                //write exception into application insights
                InsightLogger.Exception(exception.Message + " - Error in DataProver while adding entities to " + tablename, exception, callerMethodName);
                throw new Exception();
            }
        }
示例#2
0
        private bool InsertOrReplace <T>(T[] entries, CloudTable table)
            where T : ITableEntity
        {
            try
            {
                TableBatchOperation batch = new TableBatchOperation();

                for (int i = 0; i < entries.Length; i++)
                {
                    // Create the Delete TableOperation
                    batch.InsertOrMerge(entries[i]);

                    if (batch.Count >= 100)
                    {
                        table.ExecuteBatch(batch);
                        batch = new TableBatchOperation();
                    }
                }

                if (batch.Count > 0)
                {
                    table.ExecuteBatch(batch);
                }

                return(true);
            }
            catch (Exception ex)
            {
                //LogHelper.GetLogger().Error(ex.ToString(), ex);
                return(false);
            }
        }
        public static async Task UpdateData()
        {
            Console.WriteLine("UpdateData is running");
            // Job code goes here
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
            CloudTableClient    tableClient    = storageAccount.CreateCloudTableClient();
            CloudTable          table          = tableClient.GetTableReference("Services");

            table.CreateIfNotExists();
            var serviceList = await getCentrelink();

            Console.WriteLine("Got {0} services", serviceList.ToList().Count);
            TableBatchOperation batchOperation = new TableBatchOperation();

            foreach (var result in serviceList)
            {
                result.PartitionKey = result.serviceType;
                result.RowKey       = EncodeAddressForRowKey(result.address + result.suburb);
                batchOperation.InsertOrReplace(result);
                Console.WriteLine("{0} entries batched for insertion", batchOperation.Count);
                if (batchOperation.Count > 98)
                {
                    Console.WriteLine("This batch is full. Sending to cloud");
                    table.ExecuteBatch(batchOperation);
                    batchOperation = new TableBatchOperation();
                }
            }
            table.ExecuteBatch(batchOperation);
        }
示例#4
0
        private static TimeSpan InsertEntitiesInBatch(List <DynamicTableEntity> entities, CloudTable table)
        {
            TableBatchOperation batchOp = new TableBatchOperation();

            Stopwatch sw = new Stopwatch();

            sw.Start();

            foreach (DynamicTableEntity entity in entities)
            {
                batchOp.Add(TableOperation.Insert(entity));
                if (batchOp.Count > 99)
                {
                    table.ExecuteBatch(batchOp);
                    batchOp.Clear();
                }
            }

            if (batchOp.Count > 0)
            {
                table.ExecuteBatch(batchOp);
            }

            sw.Stop();
            return(sw.Elapsed);
        }
示例#5
0
        /// <summary>
        /// Writes an array of logging events to the log target. By default it iterates on all
        /// events and passes them to "Write" method. Inheriting classes can use this method to
        /// optimize batch writes.
        /// </summary>
        /// <param name="logEvents">Logging events to be written out.</param>
        protected override void Write(IList <AsyncLogEventInfo> logEvents)
        {
            //must sort into containers and then into the blobs for the container
            if (_getTableNameDelegate == null)
            {
                _getTableNameDelegate = c => TableName.Render(c.LogEvent);
            }

            var tableBuckets = SortHelpers.BucketSort(logEvents, _getTableNameDelegate);

            //Iterate over all the tables being written to
            foreach (var tableBucket in tableBuckets)
            {
                var tableNameFinal = CheckAndRepairTableNamingRules(tableBucket.Key);
                InitializeTable(tableNameFinal);
                var batch = new TableBatchOperation();
                //add each message for the destination table limit batch to 100 elements
                foreach (var asyncLogEventInfo in tableBucket.Value)
                {
                    var entity = new NLogEntity(asyncLogEventInfo.LogEvent, Layout);
                    batch.Insert(entity);
                    if (batch.Count == 100)
                    {
                        _table.ExecuteBatch(batch);
                        batch.Clear();
                    }
                }
                if (batch.Count > 0)
                {
                    _table.ExecuteBatch(batch);
                }
            }
        }
示例#6
0
        private bool ExecuteBatch(CloudTable table, TableOperation[] operations)
        {
            try
            {
                TableBatchOperation batch = new TableBatchOperation();

                for (int i = 0; i < operations.Length; i++)
                {
                    // Create the Delete TableOperation
                    batch.Add(operations[i]);

                    if (batch.Count >= 100)
                    {
                        table.ExecuteBatch(batch);
                        batch = new TableBatchOperation();
                    }
                }

                if (batch.Count > 0)
                {
                    table.ExecuteBatch(batch);
                }

                return(true);
            }
            catch (Exception ex)
            {
                //LogHelper.GetLogger().Error(ex.ToString(), ex);
                return(false);
            }
        }
示例#7
0
        protected void InsertOrMerge(TableEntity entity, string tableName)
        {
            // Create the table client.
            CloudTableClient tableClient = StorageAccount.CreateCloudTableClient();

            // Retrieve a reference to the table.
            CloudTable table = tableClient.GetTableReference(tableName);

            // Create the batch operation.
            TableBatchOperation batchOperation = new TableBatchOperation();

            // Add the entity to the batch
            batchOperation.InsertOrMerge(entity);

            // Merge
            try
            {
                table.ExecuteBatch(batchOperation);
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode == 404)
                {
                    // Create the table if it doesn't exist.
                    table.CreateIfNotExists();

                    // do the insert again
                    table.ExecuteBatch(batchOperation);
                }
            }
        }
示例#8
0
        /// <summary>
        /// Extends <see cref="CloudTable"/> to enable batched deleting of all entries in it.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entities in the <paramref name="table"/>.</typeparam>
        /// <param name="table">The Cloud Table where to delete all entries.</param>
        /// <param name="filter">A filter for selecting the entities.</param>
        public static void DeleteAllEntitiesInBatches <TEntity>(this CloudTable table, Expression <Func <TEntity, bool> > filter) where TEntity : ITableEntity, new()
        {
            Action <IEnumerable <TEntity> > processor = entities =>
            {
                var batches = new Dictionary <string, TableBatchOperation>();

                foreach (var entity in entities)
                {
                    TableBatchOperation batch = null;
                    if (batches.TryGetValue(entity.PartitionKey, out batch) == false)
                    {
                        batches[entity.PartitionKey] = batch = new TableBatchOperation();
                    }
                    batch.Add(TableOperation.Delete(entity));
                    if (batch.Count != 100)
                    {
                        continue;
                    }
                    table.ExecuteBatch(batch);
                    batches[entity.PartitionKey] = new TableBatchOperation();
                }
                foreach (var batch in batches.Values.Where(batch => batch.Count > 0))
                {
                    table.ExecuteBatch(batch);
                }
            };

            table.ProcessEntities(processor, filter);
        }
示例#9
0
        /// <summary>
        /// Writes a stream of entities to the given Azure table.
        /// </summary>
        /// <typeparam name="TEntity">Type of entities to be written</typeparam>
        /// <param name="source">Source stream</param>
        /// <param name="table">Azure table</param>
        /// <returns>Subscription corresponding to the Azure writer</returns>
        public static Subscription WriteToAzureTable <TEntity>(this Stream <TEntity, Epoch> source, CloudTable table)
            where TEntity : ITableEntity
        {
            return(source.PartitionBy(x => x.PartitionKey.GetHashCode()).Subscribe((message, workerid) =>
            {
                string lastPartitionKey = null;
                TableBatchOperation batchOperation = new TableBatchOperation();
                for (int i = 0; i < message.length; ++i)
                {
                    if (batchOperation.Count == 100 || (lastPartitionKey != null && !message.payload[i].PartitionKey.Equals(lastPartitionKey)))
                    {
                        table.ExecuteBatch(batchOperation);
                        batchOperation = new TableBatchOperation();
                    }

                    lastPartitionKey = message.payload[i].PartitionKey;

                    batchOperation.Insert(message.payload[i]);
                }

                if (batchOperation.Count > 0)
                {
                    table.ExecuteBatch(batchOperation);
                }
            }, (epoch, workerid) => { }, workerid => { }));
        }
        void ClearTable(CloudTable table)
        {
            foreach (var partitionKey in this.PartitionKeys())
            {
                TableBatchOperation batchDelete = new TableBatchOperation();

                // gets all the entities in the table for this partition key
                string partitionCondition          = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey);
                List <DynamicTableEntity> entities = table.ExecuteQuery(new TableQuery().Where(partitionCondition)).ToList();

                entities.ForEach(e =>
                {
                    batchDelete.Add(TableOperation.Delete(e));

                    // Azure has a limit on batch operations
                    if (batchDelete.Count == 100)
                    {
                        table.ExecuteBatch(batchDelete);
                        batchDelete = new TableBatchOperation();
                    }
                });

                // flush out whatever is left
                if (batchDelete.Count > 0)
                {
                    table.ExecuteBatch(batchDelete);
                }
            }
        }
示例#11
0
        private static void DeleteAllEntitiesInBatches(CloudTable table, string filter)
        {
            Action <IEnumerable <DynamicTableEntity> > processor = entities =>
            {
                var batches = new Dictionary <string, TableBatchOperation>();

                foreach (var entity in entities)
                {
                    TableBatchOperation batch = null;

                    if (batches.TryGetValue(entity.PartitionKey, out batch) == false)
                    {
                        batches[entity.PartitionKey] = batch = new TableBatchOperation();
                    }

                    batch.Add(TableOperation.Delete(entity));

                    if (batch.Count == 100)
                    {
                        table.ExecuteBatch(batch);
                        batches[entity.PartitionKey] = new TableBatchOperation();
                    }
                }

                foreach (var batch in batches.Values)
                {
                    if (batch.Count > 0)
                    {
                        table.ExecuteBatch(batch);
                    }
                }
            };

            ProcessEntities(table, processor, filter);
        }
示例#12
0
        private static void saveData(newsItems items, string tableName)
        {
            //return if no items to process
            if (items.NewsItems.Count == 0)
            {
                return;
            }

            CloudTable table = getTableStorage(tableName);

            // Create the table if it doesn't exist.
            table.CreateIfNotExists();

            // Create the batch operation.
            TableBatchOperation batchOperation = new TableBatchOperation();

            // Create the TableOperation object that inserts the customer entity.
            foreach (newsItem item in items.NewsItems)
            {
                batchOperation.InsertOrReplace(item);
                //update in batches of 100
                if (batchOperation.Count >= 100)
                {
                    table.ExecuteBatch(batchOperation);
                    batchOperation.Clear();
                }
            }
            // Execute the insert operation for anything left over
            if (batchOperation.Count > 0)
            {
                table.ExecuteBatch(batchOperation);
            }
        }
示例#13
0
        internal static void DeleteAllEntitiesInBatches(CloudTable table, string partitionKey)
        {
            void processor(IEnumerable <DynamicTableEntity> entities)
            {
                var batches = new Dictionary <string, TableBatchOperation>();

                foreach (var entity in entities)
                {
                    if (batches.TryGetValue(entity.PartitionKey, out TableBatchOperation batch) == false)
                    {
                        batches[entity.PartitionKey] = batch = new TableBatchOperation();
                    }

                    batch.Add(TableOperation.Delete(entity));

                    if (batch.Count == 100)
                    {
                        table.ExecuteBatch(batch);
                        batches[entity.PartitionKey] = new TableBatchOperation();
                    }
                }

                foreach (var batch in batches.Values)
                {
                    if (batch.Count > 0)
                    {
                        table.ExecuteBatch(batch);
                    }
                }
            }

            ProcessEntities(table, processor, partitionKey);
        }
示例#14
0
        /// <summary>
        /// Clear the Log.
        /// </summary>
        public void ClearLog()
        {
            CloudTable             table = _cloudTableClient.GetTableReference(LogsTable);
            string                 partitionKeyFilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "0");
            TableQuery <LogEntity> query = (new TableQuery <LogEntity>()).Where(partitionKeyFilter);

            LogEntity[] logEntities = table.ExecuteQuery <LogEntity>(query).ToArray();

            //var query = (from e in _context.CreateQuery<LogEntity>(LogsTable).AsTableServiceQuery()
            //             where e.PartitionKey.Equals("0")
            //             select e).AsTableServiceQuery();
            //IList<LogEntity> logEntities = QueryHelper<LogEntity>.All(query);
            var batchOperation = new TableBatchOperation();
            int count          = 0;

            foreach (LogEntity logEntity in logEntities)
            {
                batchOperation.Delete(logEntity);
                if (count > 98)
                {
                    table.ExecuteBatch(batchOperation);
                    batchOperation = new TableBatchOperation();
                    count          = 0;
                }
                else
                {
                    count++;
                }
            }
            if (batchOperation.Count > 0)
            {
                table.ExecuteBatch(batchOperation);
            }
        }
示例#15
0
        private static int Delete(string partitionKey, CloudTable itemTable, List <string> itemKeys)
        {
            int numOperations = 0;
            var batch         = new TableBatchOperation();

            foreach (var itemKey in itemKeys)
            {
                var entity = new DynamicTableEntity(partitionKey, itemKey);
                entity.ETag = "*";
                entity.Properties.Add("IsActive", new EntityProperty(false));
                batch.Add(TableOperation.Merge(entity));

                if (batch.Count == 100)
                {
                    itemTable.ExecuteBatch(batch);
                    batch.Clear();
                    numOperations++;
                }
            }

            if (batch.Count > 0)
            {
                itemTable.ExecuteBatch(batch);
                numOperations++;
            }

            return(numOperations);
        }
示例#16
0
        private static int Insert(string partitionKey, CloudTable itemTable, IEnumerable <Item> items)
        {
            int numOperations = 0;
            var batch         = new TableBatchOperation();

            foreach (var item in items)
            {
                batch.Add(TableOperation.Insert(item));

                if (batch.Count == 100)
                {
                    itemTable.ExecuteBatch(batch);
                    batch.Clear();
                    numOperations++;
                }
            }

            if (batch.Count > 0)
            {
                itemTable.ExecuteBatch(batch);
                numOperations++;
            }

            return(numOperations);
        }
        /// <summary>
        /// Process azure table storage
        /// </summary>
        public void ProcessTableStorage()
        {
            try
            {
                // Parse the connection string and return a reference to the storage account.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                    CloudConfigurationManager.GetSetting("StorageConnectionString"));

                // Create the table client.
                CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

                // Retrieve a reference to the table.
                CloudTable table = tableClient.GetTableReference("azuretablesample");

                Console.ForegroundColor = ConsoleColor.Green;

                // Create the table if it doesn't exist.
                if (table.CreateIfNotExists())
                {
                    Console.WriteLine("Azure table storage successfully created.\n");
                }

                TableBatchOperation batchOperation = new TableBatchOperation();

                //Add Car items
                if (CreateCarItems(out batchOperation))
                {
                    table.ExecuteBatch(batchOperation);
                    Console.WriteLine("Car items successfully added.\n");
                }

                batchOperation = new TableBatchOperation();

                //Add Coffee items
                if (CreateCoffeeItems(out batchOperation))
                {
                    table.ExecuteBatch(batchOperation);
                    Console.WriteLine("Coffee items successfully added.\n");
                }

                // Display Car Items
                GetAndDisplayCarItems(table);


                // Display Coffee Items
                GetAndDisplayCoffeeItems(table);
            }
            catch (Exception exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Error occurred !");
                Console.WriteLine(exception);
            }
            Console.ReadKey();
        }
示例#18
0
        public IList <TableResult> DeleteBatch(IEnumerable <ITableEntity> entities)
        {
            TableBatchOperation batch = new TableBatchOperation();

            foreach (ITableEntity tableEntity in entities)
            {
                batch.Add(TableOperation.Delete(tableEntity));
            }

            return(Table.ExecuteBatch(batch));
        }
        /// <summary>
        /// Creates new entities in the table using batching
        /// </summary>
        /// <param name="entities">The entities to store in the table</param>
        public void CreateEntities(IEnumerable <T> entities)
        {
            var batchOperation = new TableBatchOperation();

            foreach (var entity in entities)
            {
                batchOperation.Insert(entity);
            }

            _cloudTable.ExecuteBatch(batchOperation);
        }
示例#20
0
        public void SaveLeaves(List <Leaf> leaves)
        {
            var batchOp = new TableBatchOperation();

            foreach (Leaf leaf in leaves)
            {
                batchOp.InsertOrReplace(leaf);
            }

            var result = table.ExecuteBatch(batchOp);
        }
示例#21
0
        /// <summary>
        /// Creates new entities in the table using batching
        /// </summary>
        /// <param name="entities">The entities to store in the table</param>
        public void CreateEntities(IEnumerable <T> entities)
        {
            Validate.Null(entities, "entities");
            var batchOperation = new TableBatchOperation();

            foreach (var entity in entities)
            {
                batchOperation.Insert(entity);
            }

            cloudTable.ExecuteBatch(batchOperation);
        }
示例#22
0
        static void Main(string[] args)
        {
            var data = Csv.Load(@"Data\Salaries.csv");

            var storageAccountName = ConfigurationManager.AppSettings.Get("storageAccountName");
            var storageAccountKey  = ConfigurationManager.AppSettings.Get("storageAccountKey");

            CloudStorageAccount storageAccount = new CloudStorageAccount(
                new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                    storageAccountName, storageAccountKey), true);

            // Create the table client.
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Get a reference to a table named "Salary"
            CloudTable salaryTable = tableClient.GetTableReference("Salary");

            salaryTable.CreateIfNotExists();

            // Create the batch operation.
            TableBatchOperation batchOperation = new TableBatchOperation();

            int maxOperations = Microsoft.WindowsAzure.Storage.Table.Protocol.TableConstants.TableServiceBatchMaximumOperations;
            int rowsInBatch   = 0;

            foreach (DataRow row in data.Rows)
            {
                var salary = new SalaryEntity()
                {
                    LeagueId = row["lgID"].ToString(),
                    YearId   = int.Parse(row["yearID"].ToString()),
                    PlayerId = row["playerID"].ToString(),
                    TeamId   = row["teamID"].ToString(),
                    Salary   = int.Parse(row["salary"].ToString())
                };
                batchOperation.Insert(salary);

                rowsInBatch++;
                if (rowsInBatch >= maxOperations)
                {
                    salaryTable.ExecuteBatch(batchOperation);
                    batchOperation = new TableBatchOperation();
                    rowsInBatch    = 0;
                }
            }

            if (rowsInBatch >= 1)
            {
                salaryTable.ExecuteBatch(batchOperation);
            }
        }
示例#23
0
        public void InsertBatch(IEnumerable <T> objs)
        {
            List <List <T> > chunks = GetChunks(objs);

            foreach (var chunk in chunks)
            {
                var batchOperation = new TableBatchOperation();
                foreach (var obj in chunk)
                {
                    batchOperation.Insert(obj);
                }
                Table.ExecuteBatch(batchOperation);
            }
        }
示例#24
0
        /// <summary>
        /// Reduces the size of the Log to the specified size (or less).
        /// </summary>
        /// <param name="size">The size to shrink the log to (in bytes).</param>
        private void CutLog(int size)
        {
            size = size * 1024;

            CloudTable             table = _cloudTableClient.GetTableReference(LogsTable);
            string                 partitionKeyFilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "0");
            TableQuery <LogEntity> query = (new TableQuery <LogEntity>()).Where(partitionKeyFilter);

            LogEntity[] logEntities = table.ExecuteQuery <LogEntity>(query).ToArray();

            //var query = (from e in _context.CreateQuery<LogEntity>(LogsTable).AsTableServiceQuery()
            //             where e.PartitionKey.Equals("0")
            //             select e).AsTableServiceQuery();
            //LogEntity[] logEntities = QueryHelper<LogEntity>.All(query).ToArray();
            int estimatedSize = logEntities.Length * EstimatedLogEntrySize;

            if (size < estimatedSize)
            {
                int difference      = estimatedSize - size;
                int entriesToDelete = difference / EstimatedLogEntrySize;
                // Add 10% to avoid 1-by-1 deletion when adding new entries
                entriesToDelete += entriesToDelete / 10;

                if (entriesToDelete > 0)
                {
                    var batchOperation = new TableBatchOperation();

                    int count = 0;
                    for (int i = 0; i < entriesToDelete; i++)
                    {
                        batchOperation.Delete(logEntities[i]);
                        if (count > 98)
                        {
                            table.ExecuteBatch(batchOperation);
                            batchOperation = new TableBatchOperation();
                            count          = 0;
                        }
                        else
                        {
                            count++;
                        }
                    }
                    if (batchOperation.Count > 0)
                    {
                        table.ExecuteBatch(batchOperation);
                    }
                }
            }
        }
        public void DeleteEntities(CloudTable table, string partition = null)
        {
            if (!table.Exists())
            {
                return;
            }

            TableQuery query = new TableQuery();

            if (partition != null)
            {
                query.FilterString = string.Format("PartitionKey eq '{0}'", partition);
            }

            var entities = table.ExecuteQuery(query);

            if (entities.Any())
            {
                var batch = new TableBatchOperation();
                foreach (var entity in entities)
                {
                    batch.Delete(entity);
                }
                table.ExecuteBatch(batch);
            }
        }
示例#26
0
        // batching is not supported for query unless you do a range query
#if FALSE
        private IList <TableResult> BatchContains(CloudTable table, HashEntity[] entries)
        {
            // Create the batch operation.
            TableBatchOperation batchOperation = new TableBatchOperation();

            // The following code  generates test data for use during the query samples.
            foreach (var entry in entries)
            {
                batchOperation.Retrieve <HashEntity>(entry.PartitionKey, entry.RowKey);
            }

            // Execute the batch operation.
            IList <TableResult> results = table.ExecuteBatch(batchOperation);

            foreach (var result in results)
            {
                var r = result.Result as HashEntity;
                if (r == null)
                {
                    continue;
                }

                // propagate info to outer collection
                var entry = entries.First(x => x.RowKey.SequenceEqual(r.RowKey));
                entry.FoundInDB = true;
            }

            Interlocked.Add(ref TotalUpdated, results.Count);
            if ((TotalUpdated % 1000) == 0)
            {
                WriteColor(ConsoleColor.Cyan, $"DB read {TotalUpdated:N0} entries. {(TotalUpdated) / UploadedSW.Elapsed.TotalSeconds} per second. Task time: {UploadedSW.Elapsed}");
            }

            return(results);
        }
示例#27
0
        static void Main(string[] args)
        {
            string storageconnection =
                System.Configuration.ConfigurationManager.AppSettings.Get("StorageConnectionString");
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageconnection);

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            CloudTable       table       = tableClient.GetTableReference("FirstTestTable");

            table.CreateIfNotExists();



            //CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
            //CloudQueue myqueue = queueClient.GetQueueReference("queue");
            //myqueue.CreateIfNotExists();

            //CloudQueueMessage newmessage = new CloudQueueMessage("This is the fifth message!");
            //myqueue.AddMessage(newmessage);

            //CloudQueueMessage oldmessage = myqueue.GetMessage();
            //Console.WriteLine(oldmessage.AsString);



            TableBatchOperation tbo    = new TableBatchOperation();
            CarEntity           newcar = new CarEntity(124, 2012, "BMW", "X1", "Black");

            tbo.Insert(newcar);
            newcar = new CarEntity(125, 2012, "Honda", "Civic", "Yellow");
            tbo.Insert(newcar);
            newcar = new CarEntity(126, 2013, "BMW", "X1", "White");
            tbo.Insert(newcar);
            newcar = new CarEntity(127, 2014, "BMW", "X1", "Silver");
            tbo.Insert(newcar);
            table.ExecuteBatch(tbo);



            //TableOperation retrieve = TableOperation.Retrieve<CarEntity>("car", "123");
            //TableResult result = table.Execute(retrieve);

            //if (result.Result == null)
            //{
            //    Console.WriteLine("not found");
            //}
            //else
            //{
            //    Console.WriteLine("found the car " + ((CarEntity)result.Result).Make + " " + ((CarEntity)result.Result).Model);
            //}

            TableQuery <CarEntity> carquery = new TableQuery <CarEntity>().Take(4);

            foreach (CarEntity thiscar in table.ExecuteQuery(carquery))
            {
                Console.WriteLine(thiscar.Year.ToString() + " " + thiscar.Make + " " + thiscar.Model + " " + thiscar.Color);
            }

            Console.ReadKey();
        }
示例#28
0
        // POST api/values
        public string Post([FromBody] List <Data> values)
        {
            try
            {
                // 構成ファイルから Azure Storage への接続文字列を取得
                string setting = CloudConfigurationManager.GetSetting("StorageConnectionString");
                CloudStorageAccount account = CloudStorageAccount.Parse(setting);

                // Create batch.
                TableBatchOperation batch = new TableBatchOperation();
                foreach (var value in values)
                {
                    // Add Insert operation to batch.
                    batch.Add(TableOperation.Insert(new EventEntity(value)));
                }

                // Create the table client.
                CloudTableClient client = account.CreateCloudTableClient();

                // Get the CloudTable object reference that represents the "event" table.
                CloudTable table = client.GetTableReference("event");
                table.CreateIfNotExists();

                // Execute batch operation.
                table.ExecuteBatch(batch);

                return("OK");
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }
        /// <summary>
        /// Emit a batch of log events, running to completion synchronously.
        /// </summary>
        /// <param name="events">The events to emit.</param>
        /// <remarks>Override either <see cref="PeriodicBatchingSink.EmitBatch"/> or <see cref="PeriodicBatchingSink.EmitBatchAsync"/>,
        /// not both.</remarks>
        protected override void EmitBatch(IEnumerable <LogEvent> events)
        {
            var operation = new TableBatchOperation();

            var first = true;

            foreach (var logEvent in events)
            {
                if (first)
                {
                    //check to make sure the partition key is not the same as the previous batch
                    if (partitionKey != logEvent.Timestamp.Ticks)
                    {
                        batchRowId   = 0;                        //the partitionkey has been reset
                        partitionKey = logEvent.Timestamp.Ticks; //store the new partition key
                    }
                    first = false;
                }

                var logEventEntity = new LogEventEntity(logEvent, _formatProvider, partitionKey);
                logEventEntity.RowKey += "|" + batchRowId;
                operation.Add(TableOperation.Insert(logEventEntity));

                batchRowId++;
            }
            _table.ExecuteBatch(operation);
        }
示例#30
0
        private static void InsertMultipleRecords(CloudTable table)
        {
            var batchOperation = new TableBatchOperation();

            var newOrder1 = new OrderEntity("Wolfgang", "20180405")
            {
                OrderNumber = "111",
                ShippedDate = Convert.ToDateTime("2018/04/07"),
                Status      = "pending"
            };
            var newOrder2 = new OrderEntity("Wolfgang", "20180406")
            {
                OrderNumber = "222",
                ShippedDate = Convert.ToDateTime("2018/04/08"),
                Status      = "open"
            };
            var newOrder3 = new OrderEntity("Wolfgang", "20180407")
            {
                OrderNumber = "333",
                ShippedDate = Convert.ToDateTime("2018/04/09"),
                Status      = "shipped"
            };

            batchOperation.Insert(newOrder1);
            batchOperation.Insert(newOrder2);
            batchOperation.Insert(newOrder3);

            table.ExecuteBatch(batchOperation);
        }
        void ClearTable(CloudTable table)
        {
            var deviceIds = _deviceService.GetDeviceIds();

            foreach (var partitionKey in deviceIds)
            {
                TableBatchOperation batchDelete = new TableBatchOperation();

                // gets all the entities in the table for this partition key
                string partitionCondition = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey);
                List<DynamicTableEntity> entities = table.ExecuteQuery(new TableQuery().Where(partitionCondition)).ToList();

                entities.ForEach(e =>
                {
                    batchDelete.Add(TableOperation.Delete(e));

                    // Azure has a limit on batch operations
                    if (batchDelete.Count == 100)
                    {
                        table.ExecuteBatch(batchDelete);
                        batchDelete = new TableBatchOperation();
                    }
                });

                // flush out whatever is left
                if (batchDelete.Count > 0)
                {
                    table.ExecuteBatch(batchDelete);
                }
            }
        }