女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

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

馬哥Linux運維 ? 來源:未知 ? 作者:鄧佳佳 ? 2018-03-06 17:26 ? 次閱讀

前言

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

布隆過濾器 (Bloom Filter)

第一步我們先要實現(xiàn)一個布隆過濾器。

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

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

classBloomfilter(object):

A Bloom filter is a probabilistic data-structure that trades space for accuracy

when determining if a value is in a set.It can tell you if a value was possibly

added, or if it was definitely not added, but it can't tell you for certain that

it was added.

"""

def __init__(self,size):

"""Setup the BF with the appropriate size"""

self.values = [False] * size

self.size = size

def hash_value(self,value):

"""Hash the value provided and scale it to fit the BF size"""

returnhash(value) % self.size

def add_value(self,value):

"""Add a value to the BF"""

h = self.hash_value(value)

self.values[h] = True

def might_contain(self,value):

"""Check if the value might be in the BF"""

h = self.hash_value(value)

returnself.values[h]

def print_contents(self):

"""Dump the contents of the BF for debugging purposes"""

print self.values

基本的數(shù)據(jù)結(jié)構(gòu)是個數(shù)組(實際上是個位圖,用1/0來記錄數(shù)據(jù)是否存在),初始化是沒有任何內(nèi)容,所以全部置False。實際的使用當(dāng)中,該數(shù)組的長度是非常大的,以保證效率。

利用哈希算法來決定數(shù)據(jù)應(yīng)該存在哪一位,也就是數(shù)組的索引

當(dāng)一個數(shù)據(jù)被加入到布隆過濾器的時候,計算它的哈希值然后把相應(yīng)的位置為True

當(dāng)檢查一個數(shù)據(jù)是否已經(jīng)存在或者說被索引過的時候,只要檢查對應(yīng)的哈希值所在的位的True/Fasle

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

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

bf = Bloomfilter(10)

bf.add_value('dog')

bf.add_value('fish')

bf.add_value('cat')

bf.print_contents()

bf.add_value('bird')

bf.print_contents()

# Note: contents are unchanged after adding bird - it collides

forterm in['dog','fish','cat','bird','duck','emu']:

print'{}: {} {}'.format(term,bf.hash_value(term),bf.might_contain(term))

結(jié)果:

[False,False,False,False,True,True,False,False,False,True]

[False,False,False,False,True,True,False,False,False,True]

dog: 5True

fish: 4True

cat: 9True

bird: 9True

duck: 5True

emu: 8False

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

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

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

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

分詞

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

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

def major_segments(s):

"""

Perform major segmenting on a string.Split the string by all of the major

breaks, and return the set of everything found.The breaks in this implementation

are single characters, but in Splunk proper they can be multiple characters.

A set is used because ordering doesn't matter, and duplicates are bad.

"""

major_breaks = ' '

last = -1

results = set()

# enumerate() will give us (0, s[0]), (1, s[1]), ...

foridx,ch inenumerate(s):

ifch inmajor_breaks:

segment = s[last+1:idx]

results.add(segment)

last = idx

# The last character may not be a break so always capture

# the last segment (which may end up being "", but yolo)

segment = s[last+1:]

results.add(segment)

returnresults

主要分割

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

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

def minor_segments(s):

"""

Perform minor segmenting on a string.This is like major

segmenting, except it also captures from the start of the

input to each break.

"""

minor_breaks = '_.'

last = -1

results = set()

foridx,ch inenumerate(s):

ifch inminor_breaks:

segment = s[last+1:idx]

results.add(segment)

segment = s[:idx]

results.add(segment)

last = idx

segment = s[last+1:]

results.add(segment)

results.add(s)

returnresults

次要分割

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

def segments(event):

"""Simple wrapper around major_segments / minor_segments"""

results = set()

formajor inmajor_segments(event):

forminor inminor_segments(major):

results.add(minor)

returnresults

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

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

forterm insegments('src_ip = 1.2.3.4'):

print term

src

1.2

1.2.3.4

src_ip

3

1

1.2.3

ip

2

=

4

搜索

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

上代碼:

classSplunk(object):

def __init__(self):

self.bf = Bloomfilter(64)

self.terms = {}# Dictionary of term to set of events

self.events = []

def add_event(self,event):

"""Adds an event to this object"""

# Generate a unique ID for the event, and save it

event_id = len(self.events)

self.events.append(event)

# Add each term to the bloomfilter, and track the event by each term

forterm insegments(event):

self.bf.add_value(term)

ifterm notinself.terms:

self.terms[term] = set()

self.terms[term].add(event_id)

def search(self,term):

"""Search for a single term, and yield all the events that contain it"""

# In Splunk this runs in O(1), and is likely to be in filesystem cache (memory)

ifnotself.bf.might_contain(term):

return

# In Splunk this probably runs in O(log N) where N is the number of terms in the tsidx

ifterm notinself.terms:

return

forevent_id insorted(self.terms[term]):

yield self.events[event_id]

Splunk代表一個擁有搜索功能的索引集合

每一個集合中包含一個布隆過濾器,一個倒排詞表(字典),和一個存儲所有事件的數(shù)組

當(dāng)一個事件被加入到索引的時候,會做以下的邏輯

為每一個事件生成一個unqie id,這里就是序號

對事件進行分詞,把每一個詞加入到倒排詞表,也就是每一個詞對應(yīng)的事件的id的映射結(jié)構(gòu),注意,一個詞可能對應(yīng)多個事件,所以倒排表的的值是一個Set。倒排表是絕大部分搜索引擎的核心功能。

當(dāng)一個詞被搜索的時候,會做以下的邏輯

檢查布隆過濾器,如果為假,直接返回

檢查詞表,如果被搜索單詞不在詞表中,直接返回

在倒排表中找到所有對應(yīng)的事件id,然后返回事件的內(nèi)容

我們運行下看看把:

s = Splunk()

s.add_event('src_ip = 1.2.3.4')

s.add_event('src_ip = 5.6.7.8')

s.add_event('dst_ip = 1.2.3.4')

forevent ins.search('1.2.3.4'):

print event

print'-'

forevent ins.search('src_ip'):

print event

print'-'

forevent ins.search('ip'):

print event

src_ip = 1.2.3.4

dst_ip = 1.2.3.4

-

src_ip = 1.2.3.4

src_ip = 5.6.7.8

-

src_ip = 1.2.3.4

src_ip = 5.6.7.8

dst_ip = 1.2.3.4

是不是很贊!

更復(fù)雜的搜索

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

上代碼:

classSplunkM(object):

def __init__(self):

self.bf = Bloomfilter(64)

self.terms = {}# Dictionary of term to set of events

self.events = []

def add_event(self,event):

"""Adds an event to this object"""

# Generate a unique ID for the event, and save it

event_id = len(self.events)

self.events.append(event)

# Add each term to the bloomfilter, and track the event by each term

forterm insegments(event):

self.bf.add_value(term)

ifterm notinself.terms:

self.terms[term] = set()

self.terms[term].add(event_id)

def search_all(self,terms):

"""Search for an AND of all terms"""

# Start with the universe of all events...

results = set(range(len(self.events)))

forterm interms:

# If a term isn't present at all then we can stop looking

ifnotself.bf.might_contain(term):

return

ifterm notinself.terms:

return

# Drop events that don't match from our results

results = results.intersection(self.terms[term])

forevent_id insorted(results):

yield self.events[event_id]

def search_any(self,terms):

"""Search for an OR of all terms"""

results = set()

forterm interms:

# If a term isn't present, we skip it, but don't stop

ifnotself.bf.might_contain(term):

continue

ifterm notinself.terms:

continue

# Add these events to our results

results = results.union(self.terms[term])

forevent_id insorted(results):

yield self.events[event_id]

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

運行結(jié)果如下:

s = SplunkM()

s.add_event('src_ip = 1.2.3.4')

s.add_event('src_ip = 5.6.7.8')

s.add_event('dst_ip = 1.2.3.4')

forevent ins.search_all(['src_ip','5.6']):

print event

print'-'

forevent ins.search_any(['src_ip','dst_ip']):

print event

src_ip = 5.6.7.8

-

src_ip = 1.2.3.4

src_ip = 5.6.7.8

dst_ip = 1.2.3.4

總結(jié)

以上的代碼只是為了說明大數(shù)據(jù)搜索的基本原理,包括布隆過濾器,分詞和倒排表。如果大家真的想要利用這代碼來實現(xiàn)真正的搜索功能,還差的太遠。所有的內(nèi)容來自于Splunk Conf2017。大家如果有興趣可以去看網(wǎng)上的視頻。

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • python
    +關(guān)注

    關(guān)注

    56

    文章

    4825

    瀏覽量

    86178

原文標(biāo)題:用 Python 實現(xiàn)一個大數(shù)據(jù)搜索引擎

文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏

    評論

    相關(guān)推薦
    熱點推薦

    微軟面臨法國反壟斷機構(gòu)調(diào)查

    依賴必應(yīng)(Bing)搜索數(shù)據(jù)的小型搜索引擎提供劣質(zhì)搜索結(jié)果,從而對這些小型搜索引擎的業(yè)務(wù)發(fā)展造成不利影響。這
    的頭像 發(fā)表于 02-11 10:57 ?501次閱讀

    OpenAI免費開放ChatGPT搜索功能

    近日,OpenAI宣布了項重大決策:向所有用戶免費開放ChatGPT搜索功能。這舉措無疑將為用戶帶來更加高效、智能的搜索體驗。 與谷歌等傳統(tǒng)搜索
    的頭像 發(fā)表于 02-06 14:35 ?432次閱讀

    LZO Data Compression,高性能LZO無損數(shù)據(jù)壓縮加速器介紹,F(xiàn)PGA&amp;ASIC

    的AXI-Stream數(shù)據(jù)總線(128-bit數(shù)據(jù)寬度) 經(jīng)典性能指標(biāo):1.1Gbps壓縮速率@1搜索引擎@200MHz內(nèi)核時鐘2.16Gbps壓縮速率@16
    發(fā)表于 01-24 23:53

    javascript:void(0) 是否影響SEO優(yōu)化

    使用 javascript:void(0) 確實可能對SEO優(yōu)化產(chǎn)生負面影響 。以下是關(guān)于 javascript:void(0) 對SEO影響的具體分析: 搜索引擎爬蟲的理解問題 搜索引擎爬蟲(如
    的頭像 發(fā)表于 12-31 16:08 ?485次閱讀

    蘋果為谷歌支付數(shù)十億美元辯護,參與搜索案反壟斷審判

    億美元。 在周于華盛頓提交的法庭文件中,蘋果的律師明確表示,公司不能依賴谷歌來維護這協(xié)議。他們強調(diào),無論谷歌是否繼續(xù)支付這筆費用,蘋果都沒有打算建立自己的搜索引擎來與谷歌競爭。這
    的頭像 發(fā)表于 12-26 10:41 ?448次閱讀

    SSR的優(yōu)勢和劣勢分析

    SSR(Server-Side Rendering,服務(wù)器端渲染)的優(yōu)勢和劣勢分析如下: SSR的優(yōu)勢 SEO友好 : 由于搜索引擎爬蟲的性質(zhì),更容易識別和抓取服務(wù)端渲染的頁面內(nèi)容,因此提升了網(wǎng)站
    的頭像 發(fā)表于 11-18 11:27 ?1258次閱讀

    阿里國際推出全球首個B2B AI搜索引擎Accio

    近日,在歐洲科技峰會Web Summit上,阿里國際正式推出了全球首個B2B領(lǐng)域的AI搜索引擎——Accio。這創(chuàng)新產(chǎn)品面向全球商家開放,標(biāo)志著阿里國際正式入局當(dāng)前備受矚目的AI Search賽道。
    的頭像 發(fā)表于 11-15 16:53 ?1107次閱讀

    阿里國際推出B2B領(lǐng)域AI搜索引擎Accio

    近日,阿里國際宣布正式進軍AI搜索領(lǐng)域,并面向全球商家推出了首個B2B領(lǐng)域的AI搜索引擎——Accio。這創(chuàng)新產(chǎn)品的推出,標(biāo)志著阿里國際在電子商務(wù)和人工智能技術(shù)結(jié)合方面邁出了重要
    的頭像 發(fā)表于 11-14 11:47 ?802次閱讀

    租用多ip云服務(wù)器可以帶來哪些好處?應(yīng)用場景有哪些?

    。 2、避免共享ip風(fēng)險: 在共享ip環(huán)境中,如果同IP下的其他網(wǎng)站受到攻擊或被搜索引擎懲罰,可能會影響你的網(wǎng)站。使用獨立的ip地址可以降低這種風(fēng)險。 3、提高安全性: 獨立的ip地址可以減少遭受DDoS攻擊和其他網(wǎng)絡(luò)攻擊的風(fēng)險,因為攻擊者更難將
    的頭像 發(fā)表于 11-04 11:33 ?441次閱讀

    Meta開發(fā)新搜索引擎,減少對谷歌和必應(yīng)的依賴

    近日,Meta正在積極進軍人工智能領(lǐng)域,并試圖跟上OpenAI的發(fā)展步伐。為實現(xiàn)目標(biāo),Meta正在開發(fā)款全新的搜索引擎,該搜索引擎具備
    的頭像 發(fā)表于 10-29 11:49 ?733次閱讀

    月訪問量超2億,增速113%!360AI搜索成為全球增速最快的AI搜索引擎

    與傳統(tǒng)搜索引擎不同,作為AI原生搜索引擎的360AI搜索基于公開網(wǎng)絡(luò)、知識庫、大模型三大支柱。借助首創(chuàng)的 CoE 技術(shù)架構(gòu),360AI搜索整合了國內(nèi)主流的16家廠商51款大模型,支持用
    的頭像 發(fā)表于 09-09 13:44 ?800次閱讀
    月訪問量超2億,增速113%!360AI<b class='flag-5'>搜索</b>成為全球增速最快的AI<b class='flag-5'>搜索引擎</b>

    恒訊科技分析:香港站群服務(wù)器為什么要做偽靜態(tài)處理呢?

    提高搜索引擎優(yōu)化(SEO)效果:偽靜態(tài)處理可以使得動態(tài)網(wǎng)頁URL看起來像是靜態(tài)網(wǎng)頁的URL,這有助于搜索引擎更好地索引網(wǎng)站內(nèi)容。搜索引擎通常偏好靜態(tài)網(wǎng)頁,因為它們認為靜態(tài)網(wǎng)頁更穩(wěn)定、內(nèi)
    的頭像 發(fā)表于 07-31 12:49 ?464次閱讀

    OpenAI推出SearchGPT原型,正式向Google搜索引擎發(fā)起挑戰(zhàn)

    在人工智能領(lǐng)域的持續(xù)探索中,OpenAI 邁出了重大步,發(fā)布了其最新的 SearchGPT 原型,直接瞄準(zhǔn)了 Google 的核心業(yè)務(wù)——搜索引擎。這舉動不僅標(biāo)志著 OpenAI 在技術(shù)上的又
    的頭像 發(fā)表于 07-26 15:11 ?747次閱讀

    微軟計劃在搜索引擎Bing中引入AI摘要功能

    近期,科技界傳來新動向,微軟緊隨百度與谷歌的步伐,宣布計劃在其搜索引擎Bing中引入先進的AI摘要功能,旨在為用戶帶來更加智能、豐富的搜索體驗。
    的頭像 發(fā)表于 07-26 14:23 ?700次閱讀

    AI搜索挑戰(zhàn)百度谷歌,重塑信息檢索的市場?

    自然語言處理技術(shù)理解用戶的復(fù)雜查詢,從而提供更為豐富和個性化的搜索結(jié)果。就像搜索引擎在過去幾十年里徹底改變了我們獲取信息的方式樣,AI搜索現(xiàn)在正站在
    的頭像 發(fā)表于 07-04 21:15 ?452次閱讀
    AI<b class='flag-5'>搜索</b>挑戰(zhàn)百度谷歌,重塑信息檢索的市場?