有没有办法优化我的搜索blob程序? [英] Is there any way to optimize my search blob program?

查看:139
本文介绍了有没有办法优化我的搜索blob程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我编写此代码以根据内容搜索blob项(文本文件)。例如:如果我搜索Good,那么包含Good or good字样的文件应该出现在搜索结果中。我的代码正在运行,但我想优化它。



Hello
I write this code to search the blob items (text files) on the basis of there content. For ex : if I search for "Good", then the files that contains "Good or good" word the name of that files should appear in search result. My code is working but i want to optimize it.

class BlobSearch
    {
        public static int num = 1;
        static void Main(string[] args)
        {
            string accountName = "accountName";
            string accessKey = "accesskey";
            string azureConString = "DefaultEndpointsProtocol=https;AccountName=" + accountName + ";AccountKey=" + accessKey;
			string blob = "MyBlobContainer";
            string searchText = string.Empty;

            Console.WriteLine("Type and enter to search : ");
            searchText = Console.ReadLine();

            CloudStorageAccount account = CloudStorageAccount.Parse(azureConString);
            CloudBlobClient blobClient = account.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = blobClient.GetContainerReference(blob);

            blobContainer.FetchAttributes();
            var blobItemList = blobContainer.ListBlobs();
            GetBlobList(searchText, blobContainer, blobItemList);

            Console.ReadLine();
        }

              private static async void GetBlobList(string searchText, CloudBlobContainer blobContainer, IEnumerable<IListBlobItem> blobItemList)
        {
            foreach (var item in blobItemList)
            {               
                CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(item.Uri.ToString());
                if (blockBlob.Name.Contains(".txt"))
                {
                    await Search(searchText, blockBlob);
                }
            }
        }


        private async static Task Search(string searchText, CloudBlockBlob blockBlob)
        {
            string text = await blockBlob.DownloadTextAsync();
            if (text.ToLower().IndexOf(searchText.ToLower()) != -1)
            {
                Console.WriteLine("Result : " + num + " => " + blockBlob.Name.Substring(blockBlob.Name.LastIndexOf('/') + 1));
                num++;
            }
        }
    }





我认为



I think

blobContainer.ListBlobs();

阻止代码,因为搜索结果在加载所有blob项之前不起作用。无论如何要优化它或我的代码中的任何其他地方。



谢谢

is blocking code because search result not work until all the blob items loaded. Is there anyway to optimize it or anywhere else in my code.

Thanks

推荐答案

如果可能的话你可以继续旋转多个线程,将blob项目作为异步进程处理!



u可以使用以下代码,它肯定会提高性能:



if its possible you can go on with spinning up multiple thread , to process the blob items as async process !

u can use the following code , it will for sure improve the performance :

async public static Task ListBlobsSegmentedInFlatListing()
{
    // Retrieve storage account from connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));

    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference("myblobs");

    //List blobs in pages.
    Console.WriteLine("List blobs in pages:");

    //List blobs with a paging size of 10, for the purposes of the example. 
    //The first call does not include the continuation token.
    BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(
            "", true, BlobListingDetails.All, 10, null, null, null);

    //Enumerate the result segment returned.
    int i = 0;
    if (resultSegment.Results.Count<IListBlobItem>() > 0) { Console.WriteLine("Page {0}:", ++i); }
    foreach (var blobItem in resultSegment.Results)
    {
        Console.WriteLine("\t{0}", blobItem.StorageUri.PrimaryUri);
    }
    Console.WriteLine();

    //Get the continuation token, if there are additional pages of results.
    BlobContinuationToken continuationToken = resultSegment.ContinuationToken;

    //Check whether there are more results and list them in pages of 10 while a continuation token is returned.
    while (continuationToken != null)
    {
        //This overload allows control of the page size. 
        //You can return all remaining results by passing null for the maxResults parameter, 
        //or by calling a different overload.
        resultSegment = await container.ListBlobsSegmentedAsync(
                "", true, BlobListingDetails.All, 10, continuationToken, null, null);
        if (resultSegment.Results.Count<IListBlobItem>() > 0) { Console.WriteLine("Page {0}:", ++i); }
        foreach (var blobItem in resultSegment.Results)
        {
            Console.WriteLine("\t{0}", blobItem.StorageUri.PrimaryUri);
        }
        Console.WriteLine();

        //Get the next continuation token.
        continuationToken = resultSegment.ContinuationToken;
    }
}


这篇关于有没有办法优化我的搜索blob程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆