David S.
David S.

Reputation: 6105

How to list tables (by prefix) with Azure.Data.Tables?

I use Azure.Data.Tables. I found that most likely I should use Azure.Data.Tables.TableServiceClient.QueryAsync(...) (docs) to list tables, however I cannot find any documentation on the syntax of the filter string. I could avoid specifying a filter string and do the filtering on client side but it's not a proper way to do it.

There is only one example in the docs:

filter
String
Returns only tables that satisfy the specified filter. For example, the following would filter tables with a Name of 'foo': "TableName eq 'foo'".

But where is the full documentation for the filter string, and more specifically, how can I list tables by prefix? In Microsoft.Azure.Cosmos.Table.CloudTableClient.ListTables it seems it can be done easily (docs) but microsoft recommends moving to Azure.Data.Tables.

Upvotes: 4

Views: 2880

Answers (3)

user160357
user160357

Reputation: 1526

Here's working method tested in Azure.Data.Tables SDK 12.8.1

        /// <summary>
        /// Gets the names of all the tables in azure storage
        /// </summary>
        public static List<string> GetAllTableNames()
        {
            string connectionString = Secrets.API_STORAGE; // Replace with your storage account's connection string
            var serviceClient = new TableServiceClient(connectionString);
            var tableResponses =  serviceClient.Query();
            var tableNames = new List<string>();
            foreach (var table in tableResponses)
            {
                tableNames.Add(table.Name);
            }
            return tableNames;
        }

Upvotes: 1

Augustas
Augustas

Reputation: 1217

Seems method listTables() has been added to TableServiceClient :

PagedIterable<TableItem> tableItems = tableServiceClient.listTables();
tableItems.forEach(tableItem -> System.out.printf("Retrieved table with name '%s'.%n", tableItem.getName()));

https://learn.microsoft.com/en-us/java/api/com.azure.data.tables.tableserviceclient?view=azure-java-stable#com-azure-data-tables-tableserviceclient-listtables()

Upvotes: 1

Gaurav Mantri
Gaurav Mantri

Reputation: 136379

As of version 12.4.0 of Azure.Data.Tables SDK, it is not possible.

However I did find an issue on SDK repository on GitHub: https://github.com/Azure/azure-sdk-for-net/issues/24414 which pointed me to a workaround here: https://github.com/NuGet/Insights/blob/6d23681b17d7c131f7f2ab25b111fc4bcb421ba6/src/ExplorePackages.Logic/Storage/TableExtensions.cs.

From the 2nd link:

public static AsyncPageable<TableItem> GetTablesAsync(this TableServiceClient client, string prefix)
{
    var prefixQuery = TableClient.CreateQueryFilter<TableItem>(
        x => x.TableName.CompareTo(prefix) >= 0
          && x.TableName.CompareTo(prefix + char.MaxValue) <= 0);

    return client.GetTablesAsync(filter: prefixQuery);
}

Upvotes: 3

Related Questions