[英]Get all records from azure table storage
Using this code block 使用此代码块
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);
}
I am able to retrieve the single record with the partition and rowkey mentioned in the retrieve. 我能够使用检索中提到的分区和行键来检索单个记录。
Is there a way I can get all the records that are stored in the partition of ServiceAlerts? 有没有一种方法可以获取存储在ServiceAlerts分区中的所有记录?
I have tried a wildcard (*) for the second parameter 我为第二个参数尝试了通配符(*)
TableOperation retrieveOperation = TableOperation.Retrieve<ServiceAlertsEntity>(
"ServiceAlerts","b9ccd839-dd99-4358-b90f-46781b87f933");
but it does not return anything. 但它不返回任何东西。
If you need further control over the records being returned, you can use ExecuteQuerySegmentedAsync
to retrieve data a page (around 1,000 records) at a time. 如果需要进一步控制返回的记录,则可以使用
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);
Or if you need to restrict your results, eg by Partition Key, you can add a filter condition by adding a Where
clause to the query in the above code. 或者,如果您需要限制结果(例如通过分区键),则可以通过在上述代码中的查询中添加
Where
子句来添加过滤条件。
var pk = "abc";
var filterPk = TableQuery.GenerateFilterCondition(
nameof(ServiceAlertsEntity.PartitionKey),
QueryComparisons.Equal, pk);
var query = new TableQuery<ServiceAlertsEntity>().Where(filterPk);
You need to specify a TableQuery, this will give you all entities or you can specify a TableQuery.GenerateFilterCondition
to filter the rows. 您需要指定一个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);
}
You're using wrong class. 您使用了错误的课程。 Use TableQuery to retrieve multiple results.
使用TableQuery检索多个结果。 https://msdn.microsoft.com/en-us/library/azure/microsoft.windowsazure.storage.table.tablequery.aspx
https://msdn.microsoft.com/zh-CN/library/azure/microsoft.windowsazure.storage.table.tablequery.aspx
I have done the same process using generic class. 我已经使用通用类完成了相同的过程。 Using function app i have achieved this.
使用功能应用程序,我已经做到了这一点。 please find the following codes.
请找到以下代码。 First i have created a function app MyEventhubTrigger.cs
首先,我创建了一个功能应用程序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());
}
}
}
}
Now i have create table storage in azure storage account. 现在我已经在Azure存储帐户中创建了表存储。 And add the same field in a property file.
并在属性文件中添加相同的字段。 which is StorageTable.cs
这是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; }
}
}
Now I have created a class which can take any TableEntity property class and do the following operation Get/insert/Update. 现在,我创建了一个可以接受任何TableEntity属性类的类,并执行以下操作Get / insert / Update。 Find the following code for StorageTableOperation.cs
查找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.