成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

用Python實(shí)現(xiàn)一個(gè)大數(shù)據(jù)搜索引擎

開發(fā) 后端 大數(shù)據(jù)
搜索是大數(shù)據(jù)領(lǐng)域里常見的需求。Splunk和ELK分別是該領(lǐng)域在非開源和開源領(lǐng)域里的領(lǐng)導(dǎo)者。本文利用很少的Python代碼實(shí)現(xiàn)了一個(gè)基本的數(shù)據(jù)搜索功能,試圖讓大家理解大數(shù)據(jù)搜索的基本原理。

[[211336]]

搜索是大數(shù)據(jù)領(lǐng)域里常見的需求。Splunk和ELK分別是該領(lǐng)域在非開源和開源領(lǐng)域里的***。本文利用很少的Python代碼實(shí)現(xiàn)了一個(gè)基本的數(shù)據(jù)搜索功能,試圖讓大家理解大數(shù)據(jù)搜索的基本原理。

布隆過濾器 (Bloom Filter)

***步我們先要實(shí)現(xiàn)一個(gè)布隆過濾器。

布隆過濾器是大數(shù)據(jù)領(lǐng)域的一個(gè)常見算法,它的目的是過濾掉那些不是目標(biāo)的元素。也就是說如果一個(gè)要搜索的詞并不存在與我的數(shù)據(jù)中,那么它可以以很快的速度返回目標(biāo)不存在。

讓我們看看以下布隆過濾器的代碼:

  1. class Bloomfilter(object): 
  2.     ""
  3.     A Bloom filter is a probabilistic data-structure that trades space for accuracy 
  4.     when determining if a value is in a set.  It can tell you if a value was possibly 
  5.     added, or if it was definitely not added, but it can't tell you for certain that 
  6.     it was added. 
  7.     ""
  8.     def __init__(self, size): 
  9.         """Setup the BF with the appropriate size""" 
  10.         self.values = [False] * size 
  11.         self.size = size 
  12.   
  13.     def hash_value(self, value): 
  14.         """Hash the value provided and scale it to fit the BF size""" 
  15.         return hash(value) % self.size 
  16.   
  17.     def add_value(self, value): 
  18.         """Add a value to the BF""" 
  19.         h = self.hash_value(value) 
  20.         self.values[h] = True 
  21.   
  22.     def might_contain(self, value): 
  23.         """Check if the value might be in the BF""" 
  24.         h = self.hash_value(value) 
  25.         return self.values[h] 
  26.   
  27.     def print_contents(self): 
  28.         """Dump the contents of the BF for debugging purposes""" 
  29.         print self.values 
  • 基本的數(shù)據(jù)結(jié)構(gòu)是個(gè)數(shù)組(實(shí)際上是個(gè)位圖,用1/0來記錄數(shù)據(jù)是否存在),初始化是沒有任何內(nèi)容,所以全部置False。實(shí)際的使用當(dāng)中,該數(shù)組的長度是非常大的,以保證效率。
  • 利用哈希算法來決定數(shù)據(jù)應(yīng)該存在哪一位,也就是數(shù)組的索引
  • 當(dāng)一個(gè)數(shù)據(jù)被加入到布隆過濾器的時(shí)候,計(jì)算它的哈希值然后把相應(yīng)的位置為True
  • 當(dāng)檢查一個(gè)數(shù)據(jù)是否已經(jīng)存在或者說被索引過的時(shí)候,只要檢查對應(yīng)的哈希值所在的位的True/Fasle

看到這里,大家應(yīng)該可以看出,如果布隆過濾器返回False,那么數(shù)據(jù)一定是沒有索引過的,然而如果返回True,那也不能說數(shù)據(jù)一定就已經(jīng)被索引過。在搜索過程中使用布隆過濾器可以使得很多沒有***的搜索提前返回來提高效率。

我們看看這段 code是如何運(yùn)行的:

  1. bf = Bloomfilter(10) 
  2. bf.add_value('dog'
  3. bf.add_value('fish'
  4. bf.add_value('cat'
  5. bf.print_contents() 
  6. bf.add_value('bird'
  7. bf.print_contents() 
  8. # Note: contents are unchanged after adding bird - it collides 
  9. for term in ['dog''fish''cat''bird''duck''emu']: 
  10.     print '{}: {} {}'.format(term, bf.hash_value(term), bf.might_contain(term)) 

結(jié)果:

 

  1. [FalseFalseFalseFalseTrueTrueFalseFalseFalseTrue
  2. [FalseFalseFalseFalseTrueTrueFalseFalseFalseTrue
  3. dog: 5 True 
  4. fish: 4 True 
  5. cat: 9 True 
  6. bird: 9 True 
  7. duck: 5 True 
  8. emu: 8 False 

首先創(chuàng)建了一個(gè)容量為10的的布隆過濾器

然后分別加入 ‘dog’,‘fish’,‘cat’三個(gè)對象,這時(shí)的布隆過濾器的內(nèi)容如下:

然后加入‘bird’對象,布隆過濾器的內(nèi)容并沒有改變,因?yàn)?lsquo;bird’和‘fish’恰好擁有相同的哈希。

***我們檢查一堆對象(’dog’, ‘fish’, ‘cat’, ‘bird’, ‘duck’, ’emu’)是不是已經(jīng)被索引了。結(jié)果發(fā)現(xiàn)‘duck’返回True,2而‘emu’返回False。因?yàn)?lsquo;duck’的哈希恰好和‘dog’是一樣的。

分詞

下面一步我們要實(shí)現(xiàn)分詞。 分詞的目的是要把我們的文本數(shù)據(jù)分割成可搜索的最小單元,也就是詞。這里我們主要針對英語,因?yàn)橹形牡姆衷~涉及到自然語言處理,比較復(fù)雜,而英文基本只要用標(biāo)點(diǎn)符號(hào)就好了。

下面我們看看分詞的代碼:

 

  1. def major_segments(s): 
  2.     ""
  3.     Perform major segmenting on a string.  Split the string by all of the major 
  4.     breaks, and return the set of everything found.  The breaks in this implementation 
  5.     are single characters, but in Splunk proper they can be multiple characters. 
  6.     A set is used because ordering doesn't matter, and duplicates are bad. 
  7.     ""
  8.     major_breaks = ' ' 
  9.     last = -1 
  10.     results = set() 
  11.   
  12.     # enumerate() will give us (0, s[0]), (1, s[1]), ... 
  13.     for idx, ch in enumerate(s): 
  14.         if ch in major_breaks: 
  15.             segment = s[last+1:idx] 
  16.             results.add(segment) 
  17.   
  18.             last = idx 
  19.   
  20.     # The last character may not be a break so always capture 
  21.     # the last segment (which may end up being "", but yolo)     
  22.     segment = s[last+1:] 
  23.     results.add(segment) 
  24.   
  25.     return results 

主要分割

主要分割使用空格來分詞,實(shí)際的分詞邏輯中,還會(huì)有其它的分隔符。例如Splunk的缺省分割符包括以下這些,用戶也可以定義自己的分割符。

] < >( ) { } | ! ; , ‘ ” * \n \r \s \t & ? + %21 %26 %2526 %3B %7C %20 %2B %3D — %2520 %5D %5B %3A %0A %2C %28 %29

  1. def minor_segments(s): 
  2.     ""
  3.     Perform minor segmenting on a string.  This is like major 
  4.     segmenting, except it also captures from the start of the 
  5.     input to each break. 
  6.     ""
  7.     minor_breaks = '_.' 
  8.     last = -1 
  9.     results = set() 
  10.   
  11.     for idx, ch in enumerate(s): 
  12.         if ch in minor_breaks: 
  13.             segment = s[last+1:idx] 
  14.             results.add(segment) 
  15.   
  16.             segment = s[:idx] 
  17.             results.add(segment) 
  18.   
  19.             last = idx 
  20.   
  21.     segment = s[last+1:] 
  22.     results.add(segment) 
  23.     results.add(s) 
  24.   
  25.     return results 

次要分割

次要分割和主要分割的邏輯類似,只是還會(huì)把從開始部分到當(dāng)前分割的結(jié)果加入。例如“1.2.3.4”的次要分割會(huì)有1,2,3,4,1.2,1.2.3

  1. def segments(event): 
  2.     """Simple wrapper around major_segments / minor_segments""" 
  3.     results = set() 
  4.     for major in major_segments(event): 
  5.         for minor in minor_segments(major): 
  6.             results.add(minor) 
  7.     return results 

分詞的邏輯就是對文本先進(jìn)行主要分割,對每一個(gè)主要分割在進(jìn)行次要分割。然后把所有分出來的詞返回。

我們看看這段 code是如何運(yùn)行的:

  1. for term in segments('src_ip = 1.2.3.4'): 
  2.         print term 

 

  1. src 
  2. 1.2 
  3. 1.2.3.4 
  4. src_ip 
  5. 1.2.3 
  6. ip 

搜索

好了,有個(gè)分詞和布隆過濾器這兩個(gè)利器的支撐后,我們就可以來實(shí)現(xiàn)搜索的功能了。

上代碼:

  1. class Splunk(object): 
  2.     def __init__(self): 
  3.         self.bf = Bloomfilter(64) 
  4.         self.terms = {}  # Dictionary of term to set of events 
  5.         self.events = [] 
  6.      
  7.     def add_event(self, event): 
  8.         """Adds an event to this object""" 
  9.   
  10.         # Generate a unique ID for the event, and save it 
  11.         event_id = len(self.events) 
  12.         self.events.append(event) 
  13.   
  14.         # Add each term to the bloomfilter, and track the event by each term 
  15.         for term in segments(event): 
  16.             self.bf.add_value(term) 
  17.   
  18.             if term not in self.terms: 
  19.                 self.terms[term] = set() 
  20.             self.terms[term].add(event_id) 
  21.   
  22.     def search(self, term): 
  23.         """Search for a single term, and yield all the events that contain it""" 
  24.          
  25.         # In Splunk this runs in O(1), and is likely to be in filesystem cache (memory) 
  26.         if not self.bf.might_contain(term): 
  27.             return 
  28.   
  29.         # In Splunk this probably runs in O(log N) where N is the number of terms in the tsidx 
  30.         if term not in self.terms: 
  31.             return 
  32.   
  33.         for event_id in sorted(self.terms[term]): 
  34.             yield self.events[event_id] 
  • Splunk代表一個(gè)擁有搜索功能的索引集合
  • 每一個(gè)集合中包含一個(gè)布隆過濾器,一個(gè)倒排詞表(字典),和一個(gè)存儲(chǔ)所有事件的數(shù)組
  • 當(dāng)一個(gè)事件被加入到索引的時(shí)候,會(huì)做以下的邏輯
    • 為每一個(gè)事件生成一個(gè)unqie id,這里就是序號(hào)
    • 對事件進(jìn)行分詞,把每一個(gè)詞加入到倒排詞表,也就是每一個(gè)詞對應(yīng)的事件的id的映射結(jié)構(gòu),注意,一個(gè)詞可能對應(yīng)多個(gè)事件,所以倒排表的的值是一個(gè)Set。倒排表是絕大部分搜索引擎的核心功能。
  • 當(dāng)一個(gè)詞被搜索的時(shí)候,會(huì)做以下的邏輯
    • 檢查布隆過濾器,如果為假,直接返回
    • 檢查詞表,如果被搜索單詞不在詞表中,直接返回
    • 在倒排表中找到所有對應(yīng)的事件id,然后返回事件的內(nèi)容

我們運(yùn)行下看看把:

  1. s = Splunk() 
  2. s.add_event('src_ip = 1.2.3.4'
  3. s.add_event('src_ip = 5.6.7.8'
  4. s.add_event('dst_ip = 1.2.3.4'
  5.   
  6. for event in s.search('1.2.3.4'): 
  7.     print event 
  8. print '-' 
  9. for event in s.search('src_ip'): 
  10.     print event 
  11. print '-' 
  12. for event in s.search('ip'): 
  13.     print event 
  14.  
  15. src_ip = 1.2.3.4 
  16. dst_ip = 1.2.3.4 
  17. src_ip = 1.2.3.4 
  18. src_ip = 5.6.7.8 
  19. src_ip = 1.2.3.4 
  20. src_ip = 5.6.7.8 
  21. dst_ip = 1.2.3.4 

是不是很贊!

更復(fù)雜的搜索

更進(jìn)一步,在搜索過程中,我們想用And和Or來實(shí)現(xiàn)更復(fù)雜的搜索邏輯。

上代碼:

  1. class SplunkM(object): 
  2.     def __init__(self): 
  3.         self.bf = Bloomfilter(64) 
  4.         self.terms = {}  # Dictionary of term to set of events 
  5.         self.events = [] 
  6.      
  7.     def add_event(self, event): 
  8.         """Adds an event to this object""" 
  9.   
  10.         # Generate a unique ID for the event, and save it 
  11.         event_id = len(self.events) 
  12.         self.events.append(event) 
  13.   
  14.         # Add each term to the bloomfilter, and track the event by each term 
  15.         for term in segments(event): 
  16.             self.bf.add_value(term) 
  17.             if term not in self.terms: 
  18.                 self.terms[term] = set() 
  19.              
  20.             self.terms[term].add(event_id) 
  21.   
  22.     def search_all(self, terms): 
  23.         """Search for an AND of all terms""" 
  24.   
  25.         # Start with the universe of all events... 
  26.         results = set(range(len(self.events))) 
  27.   
  28.         for term in terms: 
  29.             # If a term isn't present at all then we can stop looking 
  30.             if not self.bf.might_contain(term): 
  31.                 return 
  32.             if term not in self.terms: 
  33.                 return 
  34.   
  35.             # Drop events that don't match from our results 
  36.             results = results.intersection(self.terms[term]) 
  37.   
  38.         for event_id in sorted(results): 
  39.             yield self.events[event_id] 
  40.   
  41.   
  42.     def search_any(self, terms): 
  43.         """Search for an OR of all terms""" 
  44.         results = set() 
  45.   
  46.         for term in terms: 
  47.             # If a term isn't present, we skip it, but don't stop 
  48.             if not self.bf.might_contain(term): 
  49.                 continue 
  50.             if term not in self.terms: 
  51.                 continue 
  52.   
  53.             # Add these events to our results 
  54.             results = results.union(self.terms[term]) 
  55.   
  56.         for event_id in sorted(results): 
  57.             yield self.events[event_id] 

利用Python集合的intersection和union操作,可以很方便的支持And(求交集)和Or(求合集)的操作。

運(yùn)行結(jié)果如下:

  1. s = SplunkM() 
  2. s.add_event('src_ip = 1.2.3.4'
  3. s.add_event('src_ip = 5.6.7.8'
  4. s.add_event('dst_ip = 1.2.3.4'
  5.   
  6. for event in s.search_all(['src_ip''5.6']): 
  7.     print event 
  8. print '-' 
  9. for event in s.search_any(['src_ip''dst_ip']): 
  10.     print event 
  11.  
  12. src_ip = 5.6.7.8 
  13. src_ip = 1.2.3.4 
  14. src_ip = 5.6.7.8 
  15. dst_ip = 1.2.3.4 

 

責(zé)任編輯:龐桂玉 來源: 計(jì)算機(jī)與網(wǎng)絡(luò)安全
相關(guān)推薦

2024-02-27 07:33:32

搜索引擎Rust模型

2021-09-13 06:03:42

CSS 技巧搜索引擎

2019-07-10 13:17:07

大數(shù)據(jù)搜索代碼

2020-12-31 09:20:51

Redis搜索引擎

2020-10-28 11:40:08

MySQL索引數(shù)據(jù)庫

2016-08-18 00:54:59

Python圖片處理搜索引擎

2022-02-25 09:41:05

python搜索引擎

2021-08-24 10:02:21

JavaScript網(wǎng)頁搜索 前端

2024-11-05 16:40:24

JavaScript搜索引擎

2011-06-20 18:23:06

SEO

2018-07-05 22:38:23

大數(shù)據(jù)搜索引擎SEO

2020-12-10 11:18:47

Redis搜索引擎Java

2010-03-10 09:28:41

Python標(biāo)準(zhǔn)庫

2021-08-09 10:36:49

Python搜索引擎命令

2020-03-20 10:14:49

搜索引擎倒排索引

2014-06-23 15:12:29

大數(shù)據(jù)

2017-08-07 08:15:31

搜索引擎倒排

2009-02-19 09:41:36

搜索引擎搜狐百度

2010-04-20 11:43:46

2022-10-08 09:13:18

搜索引擎?站
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 中文字幕在线观看视频一区 | 日韩中文字幕高清 | 福利在线观看 | 在线免费观看色 | 丁香婷婷久久久综合精品国产 | 亚洲国产成人精品女人久久久 | 国产有码 | 中文字幕一区二区三区日韩精品 | 精品欧美一区二区三区久久久 | 成人性生交大免费 | 亚洲一区有码 | 99福利视频 | 日韩精品一区二区三区中文字幕 | 日韩乱码一二三 | 午夜久久久久久久久久一区二区 | 风间由美一区二区三区在线观看 | 好婷婷网 | 精品国产一区二区在线 | 国产91在线视频 | 99精品久久99久久久久 | 青青艹在线视频 | www.av在线 | 日韩一区二区三区在线 | 国产精品一区在线 | 中文在线视频观看 | 欧美性生活一区二区三区 | 手机在线观看 | 亚洲成人精品 | av手机免费在线观看 | 一级片视频免费 | 四虎影院一区二区 | 久久久久久久久久久爱 | 精品乱码一区二区三四区 | 欧美日韩在线看 | 日韩欧美国产综合 | 免费在线黄 | 曰韩三级 | 国产视频精品视频 | 高清久久久 | 黄色在线免费播放 | 久久aⅴ乱码一区二区三区 91综合网 |