How to implement fast search on Azure Blob?

Posted by Vicky on Programmers See other posts from Programmers or by Vicky
Published on 2014-06-06T11:13:44Z Indexed on 2014/06/06 15:41 UTC
Read the original article Hit count: 330

Filed under:
|

I am done with writing the code to upload files (text files) to azure blob storage. Now I want to provide search based on text files content. For ex. If I search for "Hello" then the name of files that contains "Hello" words should appear in search result. Here my code to search

class BlobSearch
{
    static void Main(string[] args)
    {
        string searchText = "Hello"; 
        CloudStorageAccount account = CloudStorageAccount.Parse(azureConString);
        CloudBlobClient blobClient = account.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = blobClient.GetContainerReference("MyBlobContainer");

        blobContainer.FetchAttributes();

        var blobItemList = blobContainer.ListBlobs();

        foreach (var item in blobItemList)
        {
            string line = string.Empty;
            CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(item.Uri.ToString());

            if(blockBlob.Name.Contains(".txt"))
            {
                int lineno = 1;

                using (var stream = blockBlob.OpenRead())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        while ((line = reader.ReadLine()) != null)
                        {
                            if (line.IndexOf(searchText) != -1)
                            {                                    
                                Console.WriteLine("Line : " + lineno  +" => "+ blockBlob.Name);
                            }
                            lineno++;
                        }
                    }
                }                   
            }                
        }
        Console.WriteLine("SEARCH COMPLETE");
        Console.ReadLine();
    }
}

Above code is working but it is too slow. Is there any way to do it faster or Can improve above code.

© Programmers or respective owner

Related posts about c#

Related posts about Azure