azure-table-storage - 从 Azure 表存储中选择 MAX(值)
问题描述
如何从表存储中提取 MAX(值)?例如,什么等价于
select MAX(Timestamp) FROM DailyUsers
(但 DailyUsers 是 Azure 表存储而不是 SQL 表)
解决方案
If you're using c# code, you can follow the steps below:
1.Create a .net framework console project, and install the WindowsAzure.Storage nuget package, version 9.3.3.
2.Define an entity class for your table storage. The code like below, and it's just a sample, you can modify it to meet your need:
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.Then in the program.cs, write the code below. And to get the max date record, you can use this line of code: OrderByDescending(r=>r.Timestamp).FirstOrDefault()
.
Sample code in 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();
}
}
}
Please feel free to let me know if you have more concerns.
推荐阅读
- flux - 圆形几何的解析通量
- c# - C# & SQL Server:如果字符串值为空,如何在命令参数中插入 DBNull.Value?
- jupyter-notebook - 如何让 HoloViews 在 Google Colabs 笔记本中显示?
- php - 从多维数组中获取特定信息
- amazon-web-services - 为什么我应该使用 AWS RDS/DynamoDB 而不是将所有内容都放在 EC2 中?
- reactjs - 在第二次拖动 react-beautiful-dnd 时拖动项目时的奇怪行为
- ios - 处理 tableView 中的异步 Firestore 数据读取 - iOS
- javascript - jQuery - 在加载时运行更改功能
- reactjs - componentWillRecieveProps 不会导致渲染
- c# - SignalR:对方法名称感到困惑 - CamelCase 与 lowerCamelCase - 有什么意义?