使用此代码块
try
{
StorageCredentials creds = new StorageCredentials(accountName, accountKey);
CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
CloudTableClient client = account.CreateCloudTableClient();
CloudTable table = client.GetTableReference("serviceAlerts");
TableOperation retrieveOperation = TableOperation.Retrieve<ServiceAlertsEntity>("ServiceAlerts", "b9ccd839-dd99-4358-b90f-46781b87f933");
TableResult query = table.Execute(retrieveOperation);
if (query.Result != null)
{
outline = outline + ((ServiceAlertsEntity) query.Result).alertMessage + " * ";
}
else
{
Console.WriteLine("No Alerts");
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
我能够使用检索中提到的分区和rowkey检索单个记录。
有没有办法可以获取存储在ServiceAlerts分区中的所有记录?
我已尝试使用通配符(*)作为第二个参数
TableOperation retrieveOperation = TableOperation.Retrieve<ServiceAlertsEntity>(
"ServiceAlerts","b9ccd839-dd99-4358-b90f-46781b87f933");
但它不会返回任何内容。
答案 0 :(得分:8)
您需要指定一个TableQuery,这将为您提供所有实体,或者您可以指定TableQuery.GenerateFilterCondition
来过滤行。
TableQuery<ServiceAlertsEntity> query = new TableQuery<ServiceAlertsEntity>();
foreach (ServiceAlertsEntity entity in table.ExecuteQuery(query))
{
Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
entity.Field1, entity.Field2);
}
答案 1 :(得分:5)
如果您需要进一步控制所返回的记录,您可以使用ExecuteQuerySegmentedAsync
一次检索一页(大约1,000条记录)的数据。
var alerts = new List<ServiceAlertsEntity>();
var query = new TableQuery<ServiceAlertsEntity>();
TableContinuationToken continuationToken = null;
do
{
var page = await table.ExecuteQuerySegmentedAsync(query, continuationToken);
continuationToken = page.ContinuationToken;
alerts.AddRange(page.Results);
}
while (continuationToken != null);
或者,如果您需要限制结果,例如通过Partition Key,您可以通过在上面的代码中向查询添加Where
子句来添加过滤条件。
var pk = "abc";
var filterPk = TableQuery.GenerateFilterCondition(
nameof(ServiceAlertsEntity.PartitionKey),
QueryComparisons.Equal, pk);
var query = new TableQuery<ServiceAlertsEntity>().Where(filterPk);
答案 2 :(得分:1)
答案 3 :(得分:0)
我已经使用通用类完成了相同的过程。使用功能应用程序,我已经做到了这一点。请找到以下代码。 首先,我创建了一个功能应用程序 MyEventhubTrigger.cs
cv::equalizeHist()
现在我已经在Azure存储帐户中创建了表存储。并在属性文件中添加相同的字段。这是 StorageTable.cs
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.ServiceBus;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using AzureMonitorFunctions.WebHook;
namespace MyFunctions
{
public static class MyEventhubTrigger
{
[FunctionName("MyEventhubTrigger")]
public static async void Run([EventHubTrigger("%EventHubName%", Connection = "EventHubConnectionString", ConsumerGroup = "devicestatuscg")] string myEventHubMessage, DateTime enqueuedTimeUtc, Int64 sequenceNumber, string offset, ILogger log)
{
log.LogInformation($"C# Event Hub trigger function processed a message: {myEventHubMessage}");
try
{
StorageTableOperation<StorageTable> tableObj = new StorageTableOperation<StorageTable>("EventhubMessages");
Eventhubstorage Eventhubdada = tableObj.GetTableData("Eventhub", "DeviceStatus");
if (Eventhubdada.EnqueuedTime < enqueuedTimeUtc)
{
Eventhubdada.EnqueuedTime = enqueuedTimeUtc;
Eventhubdada.Offset = offset;
Eventhubdada.Sequence = sequenceNumber;
await tableObj.UpdateTableData("Eventhub", "DeviceStatus", Eventhubdada);
}
}
}
catch (Exception ex)
{
log.LogInformation(ex.ToString());
}
}
}
}
现在,我创建了一个类,该类可以采用任何TableEntity属性类,并执行以下操作Get / insert / Update。查找 StorageTableOperation.cs
的以下代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Table;
namespace MyFunctions
{
public class StorageTable : TableEntity
{
public StorageTable()
{
}
public StorageTable(string PKey, string RKey)
{
PartitionKey = PKey;
RowKey = RKey;
}
public DateTime EnqueuedTime { get; set; }
public Int64 Sequence { get; set; }
public string Offset { get; set; }
}
}