【问题标题】:Select MAX(value) from Azure Table Storage从 Azure 表存储中选择 MAX(值)
【发布时间】:2019-11-18 15:20:31
【问题描述】:

如何从表存储中提取 MAX(值)? 例如,什么是

select MAX(Timestamp) FROM DailyUsers

(但 DailyUsers 是 Azure 表存储而不是 SQL 表)

【问题讨论】:

标签: azure-table-storage


【解决方案1】:

如果您使用的是 c# 代码,您可以按照以下步骤操作:

1.创建一个.net框架控制台项目,安装WindowsAzure.Storagenuget包,版本9.3.3。

2.为您的表存储定义一个entity class。如下代码,只是一个示例,您可以根据需要对其进行修改:

using Microsoft.WindowsAzure.Storage.Table;

namespace ConsoleApp6
{
    public class CustomerEntity : TableEntity
    {
        public CustomerEntity()
        {
        }

        public CustomerEntity(string lastName, string firstName)
        {
            PartitionKey = lastName;
            RowKey = firstName;
        }

        public string FirstProperty { get; set; }
        public string SecondProperty { get; set; }
    }
}

3.然后在program.cs中,编写如下代码。而要获取最大日期记录,可以使用这行代码:OrderByDescending(r=>r.Timestamp).FirstOrDefault()

program.cs 中的示例代码:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Linq;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            string connstr = "DefaultEndpointsProtocol=https;xxxxxx;EndpointSuffix=core.windows.net";
            var storageAccount = CloudStorageAccount.Parse(connstr);
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            CloudTable table = tableClient.GetTableReference("your_table_name");

            TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>();

            //if you want to get the max date value from a specified PartitionKey, you can uncomment the following code.
            //string myfilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "r1");
            //query.FilterString = myfilter;

            var items = table.ExecuteQuery(query).OrderByDescending(r=>r.Timestamp).FirstOrDefault();

            Console.WriteLine(items.PartitionKey+","+items.RowKey+","+items.Timestamp);

            Console.ReadLine();
        }
    }
}

如果您有更多疑虑,请随时告诉我。

【讨论】:

  • 请注意,上面的代码会在排序前将整个表下载到客户端,这可能会非常慢,如果你的表很大,可能会导致 OutOfMemory。
  • @ZhaoxingLu-Microsoft,是的,是客户端orderby,你知道服务器端orderby怎么做吗?
  • 即使指定了分区键(取消注释您提到的行),它仍然必须在整个分区中工作。
  • @Ivan Yang,我认为做服务器端orderby是不可能的。
  • 很抱歉,但我认为这不能回答我的问题。 RowKey ge '20170805' 可用于查询 RowKeys 大于等于 20170805。我只是想知道是否有查询,而不是 c# 项目,因为查询需要放入 json 文件(azure datafactory pipeline json)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
  • 2015-11-11
相关资源
最近更新 更多