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

基于Python語言的大數據搜索引擎

開發 后端 大數據
搜索是大數據領域里常見的需求。Splunk和ELK分別是該領域在非開源和開源領域里的領導者。本文利用很少的Python代碼實現了一個基本的數據搜索功能,試圖讓大家理解大數據搜索的基本原理。

搜索是大數據領域里常見的需求。Splunk和ELK分別是該領域在非開源和開源領域里的***。本文利用很少的Python代碼實現了一個基本的數據搜索功能,試圖讓大家理解大數據搜索的基本原理。

[[270206]]

布隆過濾器 (Bloom Filter)

***步我們先要實現一個布隆過濾器。

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

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

  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.  def hash_value(self, value): 
  13.  """Hash the value provided and scale it to fit the BF size""" 
  14.  return hash(value) % self.size 
  15.  def add_value(self, value): 
  16.  """Add a value to the BF""" 
  17.  h = self.hash_value(value) 
  18.  self.values[h] = True 
  19.  def might_contain(self, value): 
  20.  """Check if the value might be in the BF""" 
  21.  h = self.hash_value(value) 
  22.  return self.values[h] 
  23.  def print_contents(self): 
  24.  """Dump the contents of the BF for debugging purposes""" 
  25.  print self.values 
  • 基本的數據結構是個數組(實際上是個位圖,用1/0來記錄數據是否存在),初始化是沒有任何內容,所以全部置False。實際的使用當中,該數組的長度是非常大的,以保證效率。
  • 利用哈希算法來決定數據應該存在哪一位,也就是數組的索引
  • 當一個數據被加入到布隆過濾器的時候,計算它的哈希值然后把相應的位置為True
  • 當檢查一個數據是否已經存在或者說被索引過的時候,只要檢查對應的哈希值所在的位的True/Fasle

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

我們看看這段 code是如何運行的:

  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)) 

結果:

  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 

首先創建了一個容量為10的的布隆過濾器 

基于python語言的大數據搜索引擎

然后分別加入 ‘dog’,‘fish’,‘cat’三個對象,這時的布隆過濾器的內容如下: 

基于python語言的大數據搜索引擎

然后加入‘bird’對象,布隆過濾器的內容并沒有改變,因為‘bird’和‘fish’恰好擁有相同的哈希。 

基于python語言的大數據搜索引擎

***我們檢查一堆對象('dog', 'fish', 'cat', 'bird', 'duck', 'emu')是不是已經被索引了。結果發現‘duck’返回True,2而‘emu’返回False。因為‘duck’的哈希恰好和‘dog’是一樣的。 

基于python語言的大數據搜索引擎

分詞

下面一步我們要實現分詞。 分詞的目的是要把我們的文本數據分割成可搜索的最小單元,也就是詞。這里我們主要針對英語,因為中文的分詞涉及到自然語言處理,比較復雜,而英文基本只要用標點符號就好了。

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

  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.  # enumerate() will give us (0, s[0]), (1, s[1]), ... 
  12.  for idx, ch in enumerate(s): 
  13.  if ch in major_breaks: 
  14.  segment = s[last+1:idx] 
  15.  results.add(segment) 
  16.  last = idx 
  17.  # The last character may not be a break so always capture 
  18.  # the last segment (which may end up being "", but yolo)  
  19.  segment = s[last+1:] 
  20.  results.add(segment) 
  21.  return results 

主要分割

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

  • ] < > ( ) { } | ! ; , ' " * s & ? + %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.  for idx, ch in enumerate(s): 
  11.  if ch in minor_breaks: 
  12.  segment = s[last+1:idx] 
  13.  results.add(segment) 
  14.  segment = s[:idx] 
  15.  results.add(segment) 
  16.  last = idx 
  17.  segment = s[last+1:] 
  18.  results.add(segment) 
  19.  results.add(s) 
  20.  return results 

次要分割

次要分割和主要分割的邏輯類似,只是還會把從開始部分到當前分割的結果加入。例如“1.2.3.4”的次要分割會有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 

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

我們看看這段 code是如何運行的:

  1. for term in segments('src_ip = 1.2.3.4'): 
  2.  print term 
  3. src 
  4. 1.2 
  5. 1.2.3.4 
  6. src_ip 
  7. 1.2.3 
  8. ip 

搜索

好了,有個分詞和布隆過濾器這兩個利器的支撐后,我們就可以來實現搜索的功能了。

上代碼:

  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.  def add_event(self, event): 
  7.  """Adds an event to this object""" 
  8.  # Generate a unique ID for the event, and save it 
  9.  event_id = len(self.events) 
  10.  self.events.append(event) 
  11.  # Add each term to the bloomfilter, and track the event by each term 
  12.  for term in segments(event): 
  13.  self.bf.add_value(term) 
  14.  if term not in self.terms: 
  15.  self.terms[term] = set() 
  16.  self.terms[term].add(event_id) 
  17.  def search(self, term): 
  18.  """Search for a single term, and yield all the events that contain it""" 
  19.  # In Splunk this runs in O(1), and is likely to be in filesystem cache (memory) 
  20.  if not self.bf.might_contain(term): 
  21.  return 
  22.  # In Splunk this probably runs in O(log N) where N is the number of terms in the tsidx 
  23.  if term not in self.terms: 
  24.  return 
  25.  for event_id in sorted(self.terms[term]): 
  26.  yield self.events[event_id] 
  • Splunk代表一個擁有搜索功能的索引集合
  • 每一個集合中包含一個布隆過濾器,一個倒排詞表(字典),和一個存儲所有事件的數組
  • 當一個事件被加入到索引的時候,會做以下的邏輯
  • 為每一個事件生成一個unqie id,這里就是序號
  • 對事件進行分詞,把每一個詞加入到倒排詞表,也就是每一個詞對應的事件的id的映射結構,注意,一個詞可能對應多個事件,所以倒排表的的值是一個Set。倒排表是絕大部分搜索引擎的核心功能。
  • 當一個詞被搜索的時候,會做以下的邏輯
  • 檢查布隆過濾器,如果為假,直接返回
  • 檢查詞表,如果被搜索單詞不在詞表中,直接返回
  • 在倒排表中找到所有對應的事件id,然后返回事件的內容

我們運行下看看把:

  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. for event in s.search('1.2.3.4'): 
  6.  print event 
  7. print '-' 
  8. for event in s.search('src_ip'): 
  9.  print event 
  10. print '-' 
  11. for event in s.search('ip'): 
  12.  print event 
  13. src_ip = 1.2.3.4 
  14. dst_ip = 1.2.3.4 
  15. src_ip = 1.2.3.4 
  16. src_ip = 5.6.7.8 
  17. src_ip = 1.2.3.4 
  18. src_ip = 5.6.7.8 
  19. dst_ip = 1.2.3.4 

是不是很贊!

更復雜的搜索

更進一步,在搜索過程中,我們想用And和Or來實現更復雜的搜索邏輯。

上代碼:

  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.  def add_event(self, event): 
  7.  """Adds an event to this object""" 
  8.  # Generate a unique ID for the event, and save it 
  9.  event_id = len(self.events) 
  10.  self.events.append(event) 
  11.  # Add each term to the bloomfilter, and track the event by each term 
  12.  for term in segments(event): 
  13.  self.bf.add_value(term) 
  14.  if term not in self.terms: 
  15.  self.terms[term] = set() 
  16.  self.terms[term].add(event_id) 
  17.  def search_all(self, terms): 
  18.  """Search for an AND of all terms""" 
  19.  # Start with the universe of all events... 
  20.  results = set(range(len(self.events))) 
  21.  for term in terms: 
  22.  # If a term isn't present at all then we can stop looking 
  23.  if not self.bf.might_contain(term): 
  24.  return 
  25.  if term not in self.terms: 
  26.  return 
  27.  # Drop events that don't match from our results 
  28.  results = results.intersection(self.terms[term]) 
  29.  for event_id in sorted(results): 
  30.  yield self.events[event_id] 
  31.  def search_any(self, terms): 
  32.  """Search for an OR of all terms""" 
  33.  results = set() 
  34.  for term in terms: 
  35.  # If a term isn't present, we skip it, but don't stop 
  36.  if not self.bf.might_contain(term): 
  37.  continue 
  38.  if term not in self.terms: 
  39.  continue 
  40.  # Add these events to our results 
  41.  results = results.union(self.terms[term]) 
  42.  for event_id in sorted(results): 
  43.  yield self.events[event_id] 

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

運行結果如下:

  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. for event in s.search_all(['src_ip''5.6']): 
  6.  print event 
  7. print '-' 
  8. for event in s.search_any(['src_ip''dst_ip']): 
  9.  print event 
  10. src_ip = 5.6.7.8 
  11. src_ip = 1.2.3.4 
  12. src_ip = 5.6.7.8 
  13. dst_ip = 1.2.3.4 

總結

以上的代碼只是為了說明大數據搜索的基本原理,包括布隆過濾器,分詞和倒排表。如果大家真的想要利用這代碼來實現真正的搜索功能,還差的太遠。

責任編輯:未麗燕 來源: 今日頭條
相關推薦

2011-06-20 18:23:06

SEO

2010-03-11 19:06:52

Python編程語言

2017-11-27 13:39:29

Python大數據搜索引擎

2014-06-23 15:12:29

大數據

2018-07-05 22:38:23

大數據搜索引擎SEO

2009-02-19 09:41:36

搜索引擎搜狐百度

2009-09-22 16:23:52

搜索引擎

2017-08-07 08:15:31

搜索引擎倒排

2020-03-20 10:14:49

搜索引擎倒排索引

2010-06-13 16:27:28

搜索引擎

2016-12-26 13:41:19

大數據搜索引擎工作原理

2022-10-08 09:13:18

搜索引擎?站

2012-09-07 13:22:21

搜索搜狗

2010-04-20 11:43:46

2016-08-18 00:54:59

Python圖片處理搜索引擎

2015-08-04 10:50:03

大數據時代搜索引擎

2012-12-06 14:45:59

2015-08-31 10:41:58

搜索引擎Google云應用

2020-02-24 08:52:08

開源索引YaCy

2019-11-20 11:58:44

大數據搜索引擎技術
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 人人干人人玩 | 国产精品久久7777777 | 在线永久看片免费的视频 | 日韩在线 | 亚洲电影一区 | 欧美一区二区三区的 | 欧美视频第二页 | 久久久久久久久久影视 | 日韩一区二区三区在线观看 | 在线视频一区二区三区 | 美女黄网 | 久久久婷| 在线成人免费视频 | 亚洲成人一区二区 | 亚洲成人免费av | 日韩有码一区二区三区 | 人人看人人草 | 四色成人av永久网址 | 欧美激情一区二区 | 成人免费视频网 | 大伊人久久 | 中文字幕在线视频免费观看 | 久久久久久久电影 | 国产精品久久二区 | 日本a网站 | 日韩免费一区二区 | 国产成人一区二区 | 精品视频一区二区三区在线观看 | 国产福利91精品一区二区三区 | 日本超碰 | 国产精品久久久久久亚洲调教 | 国产不卡一区 | 国产成人午夜高潮毛片 | 日韩欧美一区二区三区免费观看 | 久久亚洲经典 | av在线播放一区二区 | 在线一区二区国产 | 成年人免费网站 | 国产福利精品一区 | 亚洲一区二区三区 | 国产亚洲成av人片在线观看桃 |