locked
Microsoft Azure storage table query example? RRS feed

  • Question

  • I can't seem to find any examples of doing a query against a Microsoft Azure storage table (not SQL Azure) using .Net framework 4 and SDK 1.3. My understanding is that System.Data.Services is no longer the way to go. Can someone provide a basic code example that is current? Vb.Net is preferred, though C# works too. Even better if your example is a query of the WADPerformanceCountersTable.
    Saturday, February 19, 2011 4:45 AM

Answers

  • How about something like this:

    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;
    
    ...
    
    var tables = CloudStorageAccount.Parse("...").CreateCloudTableClient();
    var context = tables.GetDataServiceContext();
    
    // get all customers in "Partition1"
    var customers = (from customer in context.CreateQuery<Customer>("CustomersTable")
      where customer.PartitionKey == "Partition1").AsTableServiceQuery(); // AsTableServiceQuery automatically handles paging
    

    You'll need to add a reference to System.Data.Services.Client.

    If you want to query WADPerformanceCountersTable, you'll first need to define a class that has the properties you're interested in.  (Then put that in instead of Customer.)

    • Marked as answer by Drevo3000 Tuesday, February 22, 2011 2:36 AM
    Sunday, February 20, 2011 5:31 AM
    • Marked as answer by Drevo3000 Tuesday, February 22, 2011 6:49 AM
    Tuesday, February 22, 2011 4:31 AM

All replies

  • How about something like this:

    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;
    
    ...
    
    var tables = CloudStorageAccount.Parse("...").CreateCloudTableClient();
    var context = tables.GetDataServiceContext();
    
    // get all customers in "Partition1"
    var customers = (from customer in context.CreateQuery<Customer>("CustomersTable")
      where customer.PartitionKey == "Partition1").AsTableServiceQuery(); // AsTableServiceQuery automatically handles paging
    

    You'll need to add a reference to System.Data.Services.Client.

    If you want to query WADPerformanceCountersTable, you'll first need to define a class that has the properties you're interested in.  (Then put that in instead of Customer.)

    • Marked as answer by Drevo3000 Tuesday, February 22, 2011 2:36 AM
    Sunday, February 20, 2011 5:31 AM
  • Thanks Steve! Your posts, including this one, have helped me numerous times. The reason I was getting picky about a solution targeting .net 4, was because I was getting a VS10 error tip saying something about needing a reference to the 3.5 version of System.Dara.Services.Client. Turns out I was referencing System.Data.Services, and not explicitly System.Data.Services.Client. Changing my reference fixed it.

    By the way, any reason not to use a generic structure for table properties as found at http://encloudify.blogspot.com/2011/01/generic-tablestorage.html?

     

     

    Tuesday, February 22, 2011 1:38 AM
  • I don't think I understand the purpose of the code in that blog post... I suppose you could do that (to avoid doing CreateQuery<Customer>)?
    Tuesday, February 22, 2011 2:11 AM
  • I guess I was thinking that would make it so that there wouldn't be a need to write the properties out for each table. Upon further review, not the case. I guess the real problem is that I've become to lazy with Visual Studio's dataset designer to recognize this...
    Tuesday, February 22, 2011 2:36 AM
    • Marked as answer by Drevo3000 Tuesday, February 22, 2011 6:49 AM
    Tuesday, February 22, 2011 4:31 AM
  • Thanks Ray! Looks fantastic!
    Tuesday, February 22, 2011 6:49 AM
  • Steven,

    the whole idea around for this DynamicDataContext is to avoid the implementation per "table type" and make a generic TableStorage class. I needed it for querying the performancecounters of my roles. You see the WADperformanceCountersTable below and the application of the DynamicDataContext at the bottom.

     

        public class WADPerformanceCountersTable : TableServiceEntity

        {

            #region Properties

            Int64 eventTickCount;

     

            public Int64 EventTickCount

            {

                get { return eventTickCount; }

                set { eventTickCount = value; }

            }

            String deploymentId;

     

            public String DeploymentId

            {

                get { return deploymentId; }

                set { deploymentId = value; }

            }

            String role;

     

            public String Role

            {

                get { return role; }

                set { role = value; }

            }

            String roleInstance;

     

            public String RoleInstance

            {

                get { return roleInstance; }

                set { roleInstance = value; }

            }

            String counterName;

     

            public String CounterName

            {

                get { return counterName; }

                set { counterName = value; }

            }

            String counterValue;

     

            public String CounterValue

            {

                get { return counterValue; }

                set { counterValue = value; }

            }

     

     

     

    #endregion

     

            public WADPerformanceCountersTable()

            {

                //PartitionKey = "a";

                RowKey = string.Format("{0:10}_{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks, Guid.NewGuid());

            }

        }

                Microsoft.WindowsAzure.StorageCredentialsAccountAndKey sca = new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey("blabla",

                            "YOURKEYGOESHERE");

                Microsoft.WindowsAzure.CloudStorageAccount csa = new Microsoft.WindowsAzure.CloudStorageAccount(sca, true);

     

                var PerformanceCountersContext = new DynamicDataContext<WADPerformanceCountersTable>(csa);

                var performanceCounters = (from perfCounterEntry in PerformanceCountersContext.Load()

                                           where (perfCounterEntry.EventTickCount >= fromTicks) &&

                                           (perfCounterEntry.EventTickCount <= toTicks) &&

                                           (perfCounterEntry.CounterName.CompareTo(PerformanceCounterName) == 0)

                                           select perfCounterEntry).ToList<WADPerformanceCountersTable>()

    Monday, March 7, 2011 11:08 AM