[英]ExecuteAsync() of Azure Table Storage failing to insert all the records
[英]Get all records from azure table storage
使用此代码块
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);
}
我能够使用检索中提到的分区和行键来检索单个记录。
有没有一种方法可以获取存储在ServiceAlerts分区中的所有记录?
我为第二个参数尝试了通配符(*)
TableOperation retrieveOperation = TableOperation.Retrieve<ServiceAlertsEntity>(
"ServiceAlerts","b9ccd839-dd99-4358-b90f-46781b87f933");
但它不返回任何东西。
如果需要进一步控制返回的记录,则可以使用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);
或者,如果您需要限制结果(例如通过分区键),则可以通过在上述代码中的查询中添加Where
子句来添加过滤条件。
var pk = "abc";
var filterPk = TableQuery.GenerateFilterCondition(
nameof(ServiceAlertsEntity.PartitionKey),
QueryComparisons.Equal, pk);
var query = new TableQuery<ServiceAlertsEntity>().Where(filterPk);
您需要指定一个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);
}
您使用了错误的课程。 使用TableQuery检索多个结果。 https://msdn.microsoft.com/zh-CN/library/azure/microsoft.windowsazure.storage.table.tablequery.aspx
我已经使用通用类完成了相同的过程。 使用功能应用程序,我已经做到了这一点。 请找到以下代码。 首先,我创建了一个功能应用程序MyEventhubTrigger.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());
}
}
}
}
现在我已经在Azure存储帐户中创建了表存储。 并在属性文件中添加相同的字段。 这是StorageTable.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; }
}
}
现在,我创建了一个可以接受任何TableEntity属性类的类,并执行以下操作Get / insert / Update。 查找StorageTableOperation.cs的以下代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Threading.Tasks;
namespace MyFunctions
{
public class StorageTableOperation<T> where T : ITableEntity
{
internal CloudTable cloudTable;
public StorageTableOperation(string tableName)
{
string connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudTableClient client = storageAccount.CreateCloudTableClient();
cloudTable = client.GetTableReference(tableName);
}
public T GetTableData(string PartitionKey,string RowKey)
{
TableOperation retOp = TableOperation.Retrieve<T>(PartitionKey, RowKey);
TableResult result = cloudTable.Execute(retOp);
T tableData = (T)result.Result;
return tableData;
}
public async Task<bool> UpdateTableData(string PartitionKey, string RowKey, T tableData)
{
try
{
TableOperation operation = TableOperation.InsertOrMerge(tableData);
TableResult tableResult = await cloudTable.ExecuteAsync(operation);
return true;
}
catch(Exception ex)
{
return false;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.