如何在Azure Blob上实现快速搜索? [英] How to implement fast search on Azure Blob ?

查看:143
本文介绍了如何在Azure Blob上实现快速搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我完成了编写上传文件(文本文件)到azure blob存储的代码。现在我想提供基于文本文件内容的搜索。对于前者如果我搜索Hello,则包含Hello字样的文件名称应出现在搜索结果中。这里我的搜索代码

  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 ;
使用 var stream = blockBlob.OpenRead())
{
使用(StreamReader reader = new StreamReader(stream))
{
while ((line = reader.ReadLine())!= null
{
if (line.IndexOf(searchText)!= -1)
{
Console.WriteLine( 行: + lineno + => + blockBlob.Name);
}
lineno ++;
}
}
}
}
}
Console.WriteLine( SEARCH COMPLETE);
Console.ReadLine();
}
}





以上代码正常运行,但速度太慢。有没有办法更快地完成它或改进上面的代码。



谢谢。

解决方案

< pre lang =c#> private async static void 搜索(字符串 searchText,CloudBlockBlob blockBlob)
{
string text = await blockBlob.DownloadTextAsync();
if (text.IndexOf(searchText)!= -1)
{
Console.WriteLine(blockBlob.Name);
}
}





在异步模式下一起下载所有文本可能会更快。


Hi 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 improve above code.

Thank You.

解决方案

private async static void Search(string searchText, CloudBlockBlob blockBlob)
{
     string text = await blockBlob.DownloadTextAsync();
     if (text.IndexOf(searchText) != -1)
     {
          Console.WriteLine(blockBlob.Name);
     }
}



Downloading all text together in async mode maybe make faster.


这篇关于如何在Azure Blob上实现快速搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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