如何通过代码从Azure blob存储中删除事件中心分区?

时间:2015-11-30 20:27:15

标签: azure azure-storage-blobs azure-eventhub

我在C#Winforms项目中使用Azure Event Hub。

我创建了EventProcessorHost和EventReciever对象,以执行从事件中心检索消息并显示它们的工作。

我的部分邮件检索过程涉及在打开表单时在事件中心上创建新的使用者组。 (我只是将使用者组名称设为新的GUID)。

所有这些^都有效。

当表单关闭时,将从事件中心删除使用者组,并通过门户网站查看事件中心来验证这一点。

,使用者组用于在存储帐户中执行事件中心工作的分区对象仍然存在

通过CloudBerry资源管理器时,我看到了:

enter image description here

每个GUID都是一个消费者群体。在我开发的最后几个月里,有数百个,但事件中心一次只能包含20个活跃的消费者群体。

每个使用者组文件夹中有4个文件,其中包含与该使用者组使用的4个分区中的每个分区相关的信息。

在事件中心对象(EventReceiver,EventProcessorHost等)上是否有API调用可以自动清除这些对象?我看过但没有找到任何内容,事件中心的文档目前很少。

我查看了EventProcessorHost.PartitionManagerOptions.SkipBlobContainerCreation = true,但这没有帮助。

如果没有,是否需要设置存储帐户的设置以避免垃圾堆积?

谢谢!

2 个答案:

答案 0 :(得分:2)

我最终让这个工作。

这实际上只是稍微改变了从存储帐户中删除blob。

首先,在创建IEventProcessor对象时,您需要存储其租借信息:

    Task IEventProcessor.OpenAsync(PartitionContext context)
        {
        Singleton.Instance.AddLease(context.Lease);
        Singleton.Instance.ShowUIRunning();
        return Task.FromResult<object>(null);
        }

其中&#34; Singleton&#34;我只创建了一个单独的对象,多个线程可以转储它们的信息。 Singleton&#39;添加租约&#39;实现:

    public void AddLease(Lease l)
        {
        if (!PartitionIdToLease.ContainsKey(l.PartitionId))
            {
            PartitionIdToLease.Add(l.PartitionId, l.Token);
            }
        else
            PartitionIdToLease[l.PartitionId] = l.Token;
        }

Where&#39; PartitionIdToLease&#39;是

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();
                }
            }

所以现在每次我的应用程序关闭时,它都负责删除它在存储帐户中生成的垃圾。

希望这有助于某人

答案 1 :(得分:0)

我可能会误解你或者在你写这个问题的时候你可能不会这样做,但至少在今天,你不能在不设置检查点的情况下使用EventProcessorHost吗?

这样,在存储帐户中不会创建任何blob,您也不需要清理任何内容。 Here's a small example

相关问题