在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

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

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

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

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

馬哥Linux運(yùn)維 ? 來源:cc ? 2019-01-25 14:26 ? 次閱讀

搜索是大數(shù)據(jù)領(lǐng)域里常見的需求。Splunk和ELK分別是該領(lǐng)域在非開源和開源領(lǐng)域里的領(lǐng)導(dǎo)者。本文利用很少的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)不存在。

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

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)是個(gè)數(shù)組(實(shí)際上是個(gè)位圖,用1/0來記錄數(shù)據(jù)是否存在),初始化是沒有任何內(nèi)容,所以全部置False。實(shí)際的使用當(dāng)中,該數(shù)組的長(zhǎng)度是非常大的,以保證效率。

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

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

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

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

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

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)建了一個(gè)容量為10的的布隆過濾器

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

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

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

分詞

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

主要分割

主要分割使用空格來分詞,實(shí)際的分詞邏輯中,還會(huì)有其它的分隔符。例如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

次要分割

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

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

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

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

搜索

好了,有個(gè)分詞和布隆過濾器這兩個(gè)利器的支撐后,我們就可以來實(shí)現(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代表一個(gè)擁有搜索功能的索引集合

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

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

為每一個(gè)事件生成一個(gè)unqie id,這里就是序號(hào)

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

當(dāng)一個(gè)詞被搜索的時(shí)候,會(huì)做以下的邏輯

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

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

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

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

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ù)雜的搜索

更進(jìn)一步,在搜索過程中,我們想用And和Or來實(shí)現(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(求合集)的操作。

運(yùn)行結(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ù)搜索的基本原理,包括布隆過濾器,分詞和倒排表。如果大家真的想要利用這代碼來實(shí)現(xiàn)真正的搜索功能,還差的太遠(yuǎn)。所有的內(nèi)容來自于Splunk Conf2017。大家如果有興趣可以去看網(wǎng)上的視頻

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

    關(guān)注

    0

    文章

    119

    瀏覽量

    13386
  • python
    +關(guān)注

    關(guān)注

    56

    文章

    4807

    瀏覽量

    85037
  • 大數(shù)據(jù)
    +關(guān)注

    關(guān)注

    64

    文章

    8908

    瀏覽量

    137791

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

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

收藏 人收藏

    評(píng)論

    相關(guān)推薦

    參加搜索引擎營(yíng)銷SEM培訓(xùn)的好處?

    參加搜索引擎營(yíng)銷SEM培訓(xùn)進(jìn)入搜索引擎行業(yè),跟隨各大搜索引擎起成長(zhǎng);4. 可以通過校友會(huì)認(rèn)識(shí)大量業(yè)界專家和從業(yè)者,為日后的職業(yè)發(fā)展廣聚人脈資源;北京鼎泰恒業(yè)網(wǎng)絡(luò)營(yíng)銷培訓(xùn)學(xué)校010-5
    發(fā)表于 04-11 14:21

    基于網(wǎng)格技術(shù)的并行搜索引擎

    研究現(xiàn)有網(wǎng)格技術(shù)和搜索技術(shù),分析并行搜索引擎的優(yōu)點(diǎn)和不足,提出基于網(wǎng)格技術(shù)的并行搜索引擎解決方案,其中包含個(gè)3 層結(jié)構(gòu)的應(yīng)用框架和
    發(fā)表于 03-30 10:09 ?23次下載

    搜索引擎查詢?nèi)罩镜木垲?/a>

    隨著搜索引擎技術(shù)和網(wǎng)絡(luò)數(shù)據(jù)挖掘技術(shù)的發(fā)展,怎樣從搜索引擎查詢?nèi)罩局姓业接杏玫男畔⒊蔀檠芯繜狳c(diǎn)。該文在討論Beeferman提出的算法及Chan對(duì)其改進(jìn)的算法的優(yōu)缺點(diǎn)后,提出
    發(fā)表于 04-02 08:49 ?27次下載

    教育網(wǎng)BBS搜索引擎設(shè)計(jì)與實(shí)現(xiàn)

    BBS 是教育網(wǎng)的大特色,也是傳統(tǒng)搜索引擎搜索的盲點(diǎn),本文系統(tǒng)介紹了根據(jù)教育網(wǎng)BBS 的特點(diǎn)建立BBS 搜索引擎的關(guān)鍵技術(shù)和實(shí)現(xiàn)方法。關(guān)鍵
    發(fā)表于 06-17 11:28 ?14次下載

    主題搜索引擎的研究

    介紹了將開源的全文檢索工具包Lucene嵌入到自己的搜索引擎中來滿足開發(fā)主題搜索引擎的需求。并基于Lucene中文分詞的不足設(shè)計(jì)了個(gè)比較完善的中文分詞器,然后將其引入具體應(yīng)
    發(fā)表于 07-05 16:30 ?11次下載

    網(wǎng)絡(luò)搜索引擎,網(wǎng)絡(luò)搜索引擎的工作原理

    網(wǎng)絡(luò)搜索引擎,網(wǎng)絡(luò)搜索引擎的工作原理 21 世紀(jì)是信息時(shí)代,隨著信息科學(xué)技術(shù)的不斷發(fā)展,網(wǎng)絡(luò)已成為人們生活中的重要組成部分,網(wǎng)上
    發(fā)表于 03-26 15:51 ?1473次閱讀

    基于JAVA技術(shù)的搜索引擎的研究與實(shí)現(xiàn)

    本文還利用Java技術(shù)對(duì)搜索引擎的三個(gè)核心部分即網(wǎng)絡(luò)蜘蛛、網(wǎng)頁索引搜索進(jìn)行了實(shí)現(xiàn)索引
    發(fā)表于 05-07 14:14 ?35次下載
    基于JAVA技術(shù)的<b class='flag-5'>搜索引擎</b>的研究與<b class='flag-5'>實(shí)現(xiàn)</b>

    個(gè)大規(guī)模超文本網(wǎng)絡(luò)搜索引擎剖析(英文版)

    個(gè)大規(guī)模超文本網(wǎng)絡(luò)搜索引擎剖析(英文版)
    發(fā)表于 04-30 14:09 ?0次下載

    垂直搜索引擎是什么_垂直搜索引擎有哪些

    垂直搜索引擎是針對(duì)某一個(gè)行業(yè)的專業(yè)搜索引擎,是搜索引擎的細(xì)分和延伸,是對(duì)網(wǎng)頁庫(kù)中的某類專門的信息進(jìn)行次整合,定向分字段抽取出需要的
    發(fā)表于 01-04 17:19 ?7960次閱讀

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

    搜索大數(shù)據(jù)領(lǐng)域里常見的需求。Splunk和ELK分別是該領(lǐng)域在非開源和開源領(lǐng)域里的領(lǐng)導(dǎo)者。本文利用很少的Python代碼實(shí)現(xiàn)
    的頭像 發(fā)表于 03-06 17:26 ?4791次閱讀

    介紹五個(gè)具有高級(jí)功能的搜索引擎

    數(shù)據(jù)庫(kù)里存儲(chǔ)的大量的信息對(duì)標(biāo)準(zhǔn)的搜索引擎來說是不可見的,標(biāo)準(zhǔn)的搜索引擎只是索引網(wǎng)站上的內(nèi)容,從個(gè)
    的頭像 發(fā)表于 04-04 09:13 ?7191次閱讀

    python爬蟲入門教程之python爬蟲視頻教程分布式爬蟲打造搜索引擎

    本文檔的主要內(nèi)容詳細(xì)介紹的是python爬蟲入門教程之python爬蟲視頻教程分布式爬蟲打造搜索引擎
    發(fā)表于 08-28 15:32 ?29次下載

    搜索引擎到人工智能看大數(shù)據(jù)應(yīng)用發(fā)展史

    我們對(duì)大數(shù)據(jù)技術(shù)的使用也經(jīng)歷了個(gè)發(fā)展過程。從最開始的 Google 在搜索引擎中開始使用大數(shù)據(jù)技術(shù),到現(xiàn)在無處不在的各種人工智能應(yīng)用,伴隨
    的頭像 發(fā)表于 01-08 16:33 ?3289次閱讀

    大數(shù)據(jù)是如何優(yōu)化企業(yè)搜索引擎

    企業(yè)網(wǎng)站將比以往任何時(shí)候都更多地使用大數(shù)據(jù)大數(shù)據(jù)搜索引擎優(yōu)化(SEO)中起著非常重要的作用。
    發(fā)表于 12-28 10:24 ?2276次閱讀

    NAS下搭建linux命令搜索引擎教程

    前面寫到了程序?qū)S玫膙scode,今天再來介紹款程序佬專用的搜索引擎——Linux命令搜索引擎。該引擎專用于搜索Linux下的各種命令,畢
    的頭像 發(fā)表于 02-24 11:33 ?1155次閱讀
    NAS下搭建linux命令<b class='flag-5'>搜索引擎</b>教程
    主站蜘蛛池模板: 在线午夜影院 | 在线观看免费视频国产 | 伊人久久大香线蕉观看 | 经典三级一区在线播放 | 美女扒开腿让男生桶爽网站 | 男男np主受高h啪啪肉 | 亚洲色图图片 | 手机看片1024免费视频 | 一起射综合网 | 爱草视频 | 天天碰视频 | 黄网免费观看 | 青青伊人91久久福利精品 | 男人操女人的网站 | 亚洲国产福利精品一区二区 | 国产成人福利夜色影视 | 神马影院午夜在线 | 久久天天躁夜夜躁狠狠躁2015 | 在线视频网址 | 亚洲毛片免费在线观看 | 丁香六月婷婷激情 | 在线观看免费国产 | 天堂一区二区在线观看 | 免费大片a一级一级 | 国产色丁香久久综合 | 永久在线观看视频 | xxⅹ丰满妇女与善交 | 午夜干b| 美日韩毛片 | 成人久久网| 69国产视频 | 91成人在线免费视频 | 阿v视频在线观看免费播放 爱爱视频天天干 | 亚洲成年人免费网站 | 老师您的兔子好软水好多动漫视频 | 一级特黄性色生活片一区二区 | 亚洲欧美综合一区二区三区四区 | 黄色顶级视频 | 青青青草国产 | 免费人成a大片在线观看动漫 | 性免费网站 |