如何使用 Azure.Data.Tables 列出表(按前缀)?

问题描述 投票:0回答:3

我用

Azure.Data.Tables
。 我发现我很可能应该使用
Azure.Data.Tables.TableServiceClient.QueryAsync(...)
(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'".

但是过滤字符串的完整文档在哪里,更具体地说,如何按前缀列出表格?在

Microsoft.Azure.Cosmos.Table.CloudTableClient.ListTables
中,似乎可以轻松完成(docs),但微软建议转向
Azure.Data.Tables

c# azure azure-table-storage
3个回答
3
投票

从 Azure.Data.Tables SDK 12.4.0 版本开始,这是不可能的。

但是,我确实在 GitHub 上的 SDK 存储库上发现了一个问题:https://github.com/Azure/azure-sdk-for-net/issues/24414,这给我指出了一个解决方法:https://github .com/NuGet/Insights/blob/6d23681b17d7c131f7f2ab25b111fc4bcb421ba6/src/ExplorePackages.Logic/Storage/TableExtensions.cs.

来自第二个链接:

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);
}

1
投票

似乎方法

listTables()
已添加到
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( )


0
投票

这是在 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;
        }

© www.soinside.com 2019 - 2024. All rights reserved.