本文介绍了如何实现在Azure的Blob快速搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我与编写代码上传文件(文本文件),以蔚蓝的Blob存储完成。现在我想以文本文件的内容提供搜索。外汇。如果我搜索你好,那么文件中包含名为你好字样应会出现在搜索结果中。在这里,我的代码搜索
类BlobSearch
{
静态无效的主要(字串[] args)
{
串SEARCHTEXT =你好;
CloudStorageAccount帐户= CloudStorageAccount.Parse(azureConString);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference(MyBlobContainer);
blobContainer.FetchAttributes();
VAR blobItemList = blobContainer.ListBlobs();
的foreach(在blobItemList VAR项)
{
串线=的String.Empty;
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(item.Uri.ToString());
如果(blockBlob.Name.Contains(TXT))
{
INT LINENO = 1;使用
(VAR流= blockBlob.OpenRead())
{使用(StreamReader的读者=新的StreamReader(流))
{
,而((行=读者
.ReadLine())= NULL)
{
如果(line.IndexOf(SEARCHTEXT)= - 1)
{
Console.WriteLine(!行:+ LINENO +=>中+ blockBlob.Name);
}
LINENO ++;
}
}
}
}
}
Console.WriteLine(搜索完成);
到Console.ReadLine();
}
}
上面的代码工作,但实在是太慢了。有没有办法做到这一点更快,可提高上述代码。
解决方案
//获得BLOB数据
cloudBlob cloudBlob = blobContainer.GetBlobReference(blobName);
字符串文本= cloudBlob.DownloadText();
也许在下载一气呵成比一个循环读取一行行得更快?
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.
解决方案
// get blob data
CloudBlob cloudBlob = blobContainer.GetBlobReference(blobName);
string text = cloudBlob.DownloadText();
Maybe downloading it in one go is faster than reading line by line in a loop?
这篇关于如何实现在Azure的Blob快速搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!