天天看點

PostgreSQL (varbit, roaring bitmap) VS pilosa(bitmap庫)

postgresql , varbit , varbitx , roaring bitmap , pilosa , bitmap , 12306 , 實時畫像 , 門禁廣告售票系統

pilosa is an open source, distributed bitmap index that dramatically accelerates queries across multiple, massive data sets.

pilosa abstracts the index from data storage and optimizes it for massive scale.

<a href="https://www.pilosa.com/about/">https://www.pilosa.com/about/</a>

pilosa 是一群開發人員為了解決一些科學計算相關的場景而設計的一個in memory bitmap資料管理軟體,可以加速一些特定的查詢。例如(哪個屬性是熱門屬性,某個屬性包含了哪些對象,哪些屬性通常會集體出現。)。

pilosa通常用在資料分析和統計領域,例如前面提到的根據屬性組合求結果集,與阿裡雲rds postgresql varbitx提供的功能非常類似。

阿裡雲rds postgresql varbitx已用于多個業務場景:

<a href="https://github.com/digoal/blog/blob/master/201705/20170502_01.md">《阿裡雲rds for postgresql varbitx插件與實時畫像應用場景介紹》</a>

<a href="https://github.com/digoal/blog/blob/master/201610/20161021_01.md">《基于阿裡雲 rds postgresql 打造實時使用者畫像推薦系統》</a>

<a href="https://github.com/digoal/blog/blob/master/201611/20161124_01.md">《阿裡雲rds for postgresql varbitx插件門禁廣告銷售系統需求剖析和實踐》</a>

<a href="https://www.pilosa.com/docs/data-model/">https://www.pilosa.com/docs/data-model/</a>

pilosa的結構為行列布爾邏輯值bit矩陣,行和列的含義存儲在boltdb的資料結構中。

PostgreSQL (varbit, roaring bitmap) VS pilosa(bitmap庫)

例如一張這樣的表

create table tbl (c1 int, c2 text, c3 numeric, c4 timestamp);

insert into tbl values (1,'abc',4.0, now());

insert into tbl values (1,'ab12c',1.0, now());

轉換為pilosa的結構,首先要對value進行屬性化轉換,轉換過程就是建立k-v的過程。屬性即k-v。

例如,以上資料可以轉換為以下屬性。

屬性建立後,轉換為pilosa存儲結構

為了便于使用,pilosa還将資料結構進行了拆分。

1、索引(index),索引是指在對原始資料進行屬性化轉換後,得到的bitmap資料。

可以對原始資料建立多個不同的屬性轉化,例如c1:1, c2:abc,ab12c, c3:1, c3:4, ...是一種,前面的例子又是另一種。不同的轉換規則,建立不同的索引。

不能跨索引查詢,隻能在一個索引内查詢。(因為跨索引的bitmap可能沒法對上号)

2、幀(frame)

為了便于查詢,可以将一個索引,根據查詢目标的不同,劃分為多個幀。

例如ranked幀,按bitcount從大到小排列,便于查詢基于行的屬性topn。

PostgreSQL (varbit, roaring bitmap) VS pilosa(bitmap庫)

例如lru幀,将最近通路的行排在前面,便于查詢活躍的行。

PostgreSQL (varbit, roaring bitmap) VS pilosa(bitmap庫)

3、時間視窗(time quantum)

可以設定index的時間視窗,設定時間視窗後,會根據資料的時間自動建立對應的時間視圖和對應的bitmap value。(if the time quantum is set to ymd, range queries down to the granularity of a day are supported.)

PostgreSQL (varbit, roaring bitmap) VS pilosa(bitmap庫)

4、屬性(attribute),前面講了,屬性就是k-v,是為了得到bitmap,提前對原始資料所做的轉換。

5、分片(slice),為了提高計算速度,将單行切分為多個分片,例如一行有1024個bit,切成兩個分片,分别對應512個bit。

在計算時,每個分片是并行獨立計算,最後合并結果的。

6、視圖(view),視圖是pilosa内部,自動維護的一個對外可見的邏輯視野,預設會建立一個與資料寫入一緻的标準視圖。(行,列與資料寫入時一緻)

6.1、反轉(inverse)視圖bitmap,例如

反轉視圖和标準視圖如下

PostgreSQL (varbit, roaring bitmap) VS pilosa(bitmap庫)

6.2、時間視窗視圖(time quantums)

當使用者設定了索引的時間視窗屬性時,會自動生成對應的時間視窗視圖

PostgreSQL (varbit, roaring bitmap) VS pilosa(bitmap庫)

以上是pilosa的資料模型,有利于統計類的查詢。

<a href="https://www.pilosa.com/use-cases/">https://www.pilosa.com/use-cases/</a>

化學相似性分析

交通資料分析

網絡流量分析

<a href="https://www.pilosa.com/docs/faq/">https://www.pilosa.com/docs/faq/</a>

pilosa是一個用于加速統計類查詢的in memory索引資料結構,允許使用者自定義屬性,建立bitmap,時間視窗視圖等。同時内置了幀、切片等特性,加速查詢。

pilosa不是資料庫,也不能代替資料庫,把它當成分析類sql的加速引擎是可以的。

使用者可以異步的方式,将資料導入pilosa,加速查詢。

pilosa支援go,python,java用戶端驅動,或者調用pilosa api。

postgresql 支援varbit類型,同時包含了一些bit的操作符,例如位移、與、或、異或、擷取位置bit等。

阿裡雲rds postgresql varbitx是varbit類型的擴充包,支援更多的bit操作,例如set bit,批量set,按數組提供的位置set,求bit count,求指定位置範圍的bit count,求特定數組提供的位置的bit count等。

詳細介紹如下

例如實時資料分析(與pilosa類似),基于varbit的人物畫像,按标簽圈人。門禁廣告銷售系統等。

pilosa是一套golang編寫的bitmap庫,通過postgresql plgo應該可以将其嫁接到postgresql中,作為一個插件庫來使用。

<a href="https://github.com/microo8/plgo">https://github.com/microo8/plgo</a>

<a href="https://github.com/dbudworth/gopgfuncs">https://github.com/dbudworth/gopgfuncs</a>

<a href="https://github.com/zeromax007/gpdb-roaringbitmap">https://github.com/zeromax007/gpdb-roaringbitmap</a>

<a href="https://github.com/digoal/blog/blob/master/201702/20170221_02.md">《postgresql bitmapand, bitmapor, bitmap index scan, bitmap heap scan》</a>

<a href="https://github.com/digoal/blog/blob/master/201610/20161021_01.md">《基于 阿裡雲 rds postgresql 打造實時使用者畫像推薦系統》</a>

<a href="https://github.com/digoal/blog/blob/master/201611/20161124_02.md">《postgresql 與 12306 搶火車票的思考》</a>

<a href="https://github.com/digoal/blog/blob/master/201611/20161124_01.md">《門禁廣告銷售系統需求剖析 與 postgresql資料庫實作》</a>

<a href="https://www.pilosa.com/">https://www.pilosa.com/</a>

<a href="https://github.com/digoal/blog/blob/master/201610/20161027_01.md">《postgresql 9.6 sharding based on fdw &amp; pg_pathman》</a>