How to get the latest record using RowKey or Timestamp in Azure table storage

The hard part of RowKey is string , which has a value similar to Mon Nov 14 12:26:42 2016

I tried the request using Timestamp , for example

 var lowerlimit = DateTime.UtcNow; // its should be nearer to table timestamp data. TableQuery<TemperatureEntity> query2 = new TableQuery<TemperatureEntity>().Where(TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThanOrEqual,lowerlimit)); var test = table.ExecuteQuery(query2); 

MyEntity.cs

  public class MyEntity : TableEntity { public MyEntity(string partitionKey, string rowKey) { this.PartitionKey = partitionKey; this.RowKey = rowKey; } public MyEntity() { } public Int64 DevideId { get; set; } public string RowKey { get; set; } } 

// below the request gives the full data of Program.cs

 // Retrieve the storage account from the connection string. CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); // Create the table client. CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); // Create the CloudTable object that represents the "TemperatureData" table. CloudTable table = tableClient.GetTableReference("TemperatureData"); // retrive data TableQuery<TemperatureEntity> query = new TableQuery<TemperatureEntity>(); var data = table.ExecuteQuery(query); 

enter image description here

+5
source share
2 answers

Neo

If you need the last entry in your section, using a date string for a row string is not a good approach, as the table store stores entities in ascending order based on the Row Key.

If at the current point you can change the value of your string, use DateTime.UtcNow.Ticks :

 var invertedTimeKey = DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks 

With this approach, when querying your table, you can make 1 record corresponding to the last.

If you cannot change the value of your row key, you will need to extract all the entries in this section, that is, load all of it into memory, and then arrange them using a timestamp to get the last. If you have a lot of entries, this is definitely not a good approach.

 var lastResult = results.OrderByDescending(r => r.Timestamp).FirstOrDefault(); 
+11
source

The Azure Table Service does not support Order By functionality, so with the current setup, you only need to download all entities and sort them back chronologically on the client side. This, obviously, is not an optimal solution when the number of objects in the table becomes large.

Another option (which will require you to reverse engineer the application) is to convert the date / time value to inverse ticks:

 var rowKey = (DateTime.MaxValue.Ticks - DateTimeValueForRowKey.Ticks).ToString("d19") 

This ensures that the latest entries are added to the top of the table, not the bottom of the table. To get the last record, you just need to take the 1st entity from the table.

+4
source

Source: https://habr.com/ru/post/1259740/


All Articles