如何通过代码从 Azure blob 存储中删除事件中心分区?
How can I remove event hub partitions from Azure blob storage through code?
我在 C# Winforms 项目中使用 Azure 事件中心。
我创建 EventProcessorHost 和 EventReciever 对象来执行从事件中心检索消息并显示它们的工作。
我的消息检索过程的一部分涉及在我的表单打开时在我的事件中心创建一个新的使用者组。 (我只是将消费者组名称设为新的 GUID)。
所有这些^ 都有效。
窗体关闭时,使用者组将从事件中心删除,这可以通过门户查看事件中心来验证。
但是,消费者组用于执行事件中心工作的分区对象仍然存在存储帐户中。
通过 CloudBerry 浏览器时,我看到了这个:
其中每个 GUID 都是一个消费者组。在我开发的最后几个月这里有数百个,但是一个事件中心一次只能包含 20 个活跃的消费者组。
每个使用者组文件夹内有 4 个文件,其中包含与该使用者组使用的 4 个分区中的每一个相关的信息。
是否有对事件中心对象(EventReceiver、EventProcessorHost 等)的 API 调用可以自动为我清理这些对象?我已经查看但未找到任何内容,并且事件中心的文档目前很少。
我查看了 EventProcessorHost.PartitionManagerOptions.SkipBlobContainerCreation = true 但这没有帮助。
如果没有,是否需要设置存储帐户的设置以避免这种垃圾堆积?
谢谢!
我最终成功了。
这实际上只是从存储帐户中删除 blob,稍作改动。
首先,在创建 IEventProcessor 对象时,您需要存储它们的租用信息:
Task IEventProcessor.OpenAsync(PartitionContext context)
{
Singleton.Instance.AddLease(context.Lease);
Singleton.Instance.ShowUIRunning();
return Task.FromResult<object>(null);
}
其中 "Singleton" 只是我创建的一个单例对象,多个线程可以在其中转储它们的信息。单例的 'Add Lease' 实现:
public void AddLease(Lease l)
{
if (!PartitionIdToLease.ContainsKey(l.PartitionId))
{
PartitionIdToLease.Add(l.PartitionId, l.Token);
}
else
PartitionIdToLease[l.PartitionId] = l.Token;
}
其中 'PartitionIdToLease' 是一个
Dictionary<string, string>
现在,删除代码:
CloudStorageAccount acc = CloudStorageAccount.Parse("Your Storage Account Connection String");
CloudBlobClient client = acc.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference("Name of Event Hub");
CloudBlobDirectory directory = container.GetDirectoryReference("Name of Folder");
foreach (IListBlobItem item in directory.ListBlobs())
{
if (item is CloudBlockBlob)
{
CloudBlockBlob cb = item as CloudBlockBlob;
AccessCondition ac = new AccessCondition();
string partitionNumber = cb.Name.Substring(cb.Name.IndexOf('/') + 1); //We want the name of the file only, and cb.Name gives us "Folder/Name"
ac.LeaseId = Singleton.Instance.PartitionIdToLease[partitionNumber];
cb.ReleaseLease(ac);
cb.DeleteIfExists();
}
}
所以现在每次我的应用程序关闭时,它都会负责删除它在存储帐户中生成的垃圾。
希望这对某人有所帮助
我可能误会了你,或者你在写这个问题的时候不能这样做,但至少今天,你不能只使用 EventProcessorHost 而不设置检查点吗?
这样,不会在存储帐户中创建任何 blob,您也不需要清理任何东西。 Here's a small example
我在 C# Winforms 项目中使用 Azure 事件中心。
我创建 EventProcessorHost 和 EventReciever 对象来执行从事件中心检索消息并显示它们的工作。
我的消息检索过程的一部分涉及在我的表单打开时在我的事件中心创建一个新的使用者组。 (我只是将消费者组名称设为新的 GUID)。
所有这些^ 都有效。
窗体关闭时,使用者组将从事件中心删除,这可以通过门户查看事件中心来验证。
但是,消费者组用于执行事件中心工作的分区对象仍然存在存储帐户中。
通过 CloudBerry 浏览器时,我看到了这个:
其中每个 GUID 都是一个消费者组。在我开发的最后几个月这里有数百个,但是一个事件中心一次只能包含 20 个活跃的消费者组。
每个使用者组文件夹内有 4 个文件,其中包含与该使用者组使用的 4 个分区中的每一个相关的信息。
是否有对事件中心对象(EventReceiver、EventProcessorHost 等)的 API 调用可以自动为我清理这些对象?我已经查看但未找到任何内容,并且事件中心的文档目前很少。
我查看了 EventProcessorHost.PartitionManagerOptions.SkipBlobContainerCreation = true 但这没有帮助。
如果没有,是否需要设置存储帐户的设置以避免这种垃圾堆积?
谢谢!
我最终成功了。
这实际上只是从存储帐户中删除 blob,稍作改动。
首先,在创建 IEventProcessor 对象时,您需要存储它们的租用信息:
Task IEventProcessor.OpenAsync(PartitionContext context)
{
Singleton.Instance.AddLease(context.Lease);
Singleton.Instance.ShowUIRunning();
return Task.FromResult<object>(null);
}
其中 "Singleton" 只是我创建的一个单例对象,多个线程可以在其中转储它们的信息。单例的 'Add Lease' 实现:
public void AddLease(Lease l)
{
if (!PartitionIdToLease.ContainsKey(l.PartitionId))
{
PartitionIdToLease.Add(l.PartitionId, l.Token);
}
else
PartitionIdToLease[l.PartitionId] = l.Token;
}
其中 'PartitionIdToLease' 是一个
Dictionary<string, string>
现在,删除代码:
CloudStorageAccount acc = CloudStorageAccount.Parse("Your Storage Account Connection String");
CloudBlobClient client = acc.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference("Name of Event Hub");
CloudBlobDirectory directory = container.GetDirectoryReference("Name of Folder");
foreach (IListBlobItem item in directory.ListBlobs())
{
if (item is CloudBlockBlob)
{
CloudBlockBlob cb = item as CloudBlockBlob;
AccessCondition ac = new AccessCondition();
string partitionNumber = cb.Name.Substring(cb.Name.IndexOf('/') + 1); //We want the name of the file only, and cb.Name gives us "Folder/Name"
ac.LeaseId = Singleton.Instance.PartitionIdToLease[partitionNumber];
cb.ReleaseLease(ac);
cb.DeleteIfExists();
}
}
所以现在每次我的应用程序关闭时,它都会负责删除它在存储帐户中生成的垃圾。
希望这对某人有所帮助
我可能误会了你,或者你在写这个问题的时候不能这样做,但至少今天,你不能只使用 EventProcessorHost 而不设置检查点吗?
这样,不会在存储帐户中创建任何 blob,您也不需要清理任何东西。 Here's a small example