溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Aerospike的簡介及使用方法是什么

發(fā)布時間:2021-11-10 17:48:34 來源:億速云 閱讀:463 作者:柒染 欄目:大數(shù)據(jù)

Aerospike的簡介及使用方法是什么,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

寫在前沿

工作幾年,一直都沒有去體系化的學(xué)習(xí),很多東西沒有復(fù)雜的工作場景經(jīng)驗,最后還是決定報了拉勾的高薪訓(xùn)練營,在這里也是實實在在的學(xué)習(xí)到了很多,學(xué)完掌握程度也比之前深了很多,而且還有定期的內(nèi)推,多了更多的機會,真的對我有了很大的幫助提升。特別感謝溫柔可愛的小竹子班主任和認(rèn)真負(fù)責(zé)帥氣的老可樂導(dǎo)師給予我的幫助!

Aerospike介紹

Aerospike(簡稱AS)是一個分布式,可擴展的鍵值存儲的NoSQL數(shù)據(jù)庫。 T級別大數(shù)據(jù)高并發(fā)的結(jié)構(gòu)化數(shù)據(jù)存儲 讀寫操作達微妙級,99%的響應(yīng)可在1毫秒內(nèi)實現(xiàn) 采用混合架構(gòu),索引存儲在內(nèi)存中,而數(shù)據(jù)可存儲在機械硬盤(HDD)或固態(tài)硬盤(SSD) 上(也可存儲在 內(nèi)存) AS內(nèi)部在訪問SSD屏蔽了文件系統(tǒng)層級,直接訪問地址,保證了數(shù)據(jù)的讀取速度。 AS同時支持二級索引與Client聚合,支持簡單的sql操作(aql),相比于其他nosql數(shù)據(jù)庫,有一定優(yōu) 勢。

Aerospike應(yīng)用場景

Aerospike作為一個大容量的NoSql解決方案,并未在國內(nèi)廠中廣泛商使用。它適合對容量要求比較 大,QPS相對低一些的場景,目前主要集中于互聯(lián)網(wǎng)廣告行業(yè)使用(國外)

個性化推薦廣告應(yīng)用

個性化推薦廠告是建立在了和掌握消費者獨特的偏好和習(xí)性的基礎(chǔ)之上,對消費者的購買需求做出準(zhǔn)確 的預(yù) 或引導(dǎo),在合適的位置、合適的時間,以合適的形式向消費者呈現(xiàn)與其需求高度吻合的廣告,以此 來促進用戶的消費行為。

Aerospike的簡介及使用方法是什么

用戶行為日志收集系統(tǒng)收集日志之后推送到ETL做數(shù)據(jù)的清洗和轉(zhuǎn)換 把ETL過后的數(shù)據(jù)發(fā)送到推薦引擎計算每個消費者的推薦結(jié)果,其中推薦邏輯包括規(guī)則和算法兩部分 具體的規(guī)則有用戶最近瀏覽、加入購物車、加入收藏等,

算法則包括商品相似性、用戶相似性、文本相似性、圖片相似性等算法。 把推薦引擎的結(jié)果存入Aerospike集群中,并提供給廣告投放引擎實時獲取

實時競價廣告應(yīng)用

當(dāng)用戶瀏覽一個加入SSP(供應(yīng)方平臺)的站點時,SSP會把此次請求發(fā)送到AD EXCHANGE(廣告交易 平臺),然后ADX會把這次請求發(fā)送給多家DSP,DSP(需求方平臺)根據(jù)自身的DMP(數(shù)據(jù)管理平 臺),通過對次用戶的了解程度進行競價,最終競價勝出的DSP獲得展現(xiàn)廣告的機會。 DSP競價(RTB:實時競價)勝出的關(guān)鍵是DMP能夠根據(jù)用戶的歷史瀏覽等數(shù)據(jù)分析和定位用戶屬性, 其中實時競價廣告中非常重要的一個環(huán)節(jié)就是UserProfile(用戶畫像)。 分別通過HDFS和HBASE對日志進行離線和實時的分析,然后把用戶畫像的標(biāo)簽(tag : 程序猿、宅男...)結(jié) 果存入高性能的Nosql數(shù)據(jù)庫Aerospike中,同時把數(shù)據(jù)備份到異地數(shù)據(jù)中心。前端廣告投放請求通過 決策引擎(投放引擎)向用戶畫像數(shù)據(jù)庫中讀取相應(yīng)的用戶畫像數(shù)據(jù),然后根據(jù)競價算法出價進行競 價。競價成功之后就可以展現(xiàn)廣告了。而在競價成功之后,具體給用戶展現(xiàn)什么樣的廣告,就是有上面 說的個性化推薦廣告來完成的。 Aerospike的簡介及使用方法是什么

Aerospike與Redis對比

Aerospike是NoSQL的數(shù)據(jù)存儲,Redis是緩存 Aerospike是多線程的,而 Redis 是單線程的 Redis 需要開發(fā)人員自己管理分片并提供分片算法用于在各分片之間平衡數(shù)據(jù); client: hash 一致性hash codis :代理處理sharding RedisCluster: hash槽 而 AerospikeDB 可以自動處理相當(dāng)于分片的工作; 在 Redis 中,為了增加吞吐量,需要增加 Redis 分片的數(shù)量,并重構(gòu)分片算法及重新平衡數(shù)據(jù),這通常 需要停機;

而在 AerospikeDB 中,可以動態(tài)增加數(shù)據(jù)卷和吞吐量,無需停機,并且 AerospikeDB 可以自動平衡數(shù) 據(jù)和流量; 在 Redis 中,如果需要復(fù)制及故障轉(zhuǎn)移功能,則需要開發(fā)人員自己在應(yīng)用程序?qū)油綌?shù)據(jù); 而在 AerospikeDB 中,只需設(shè)置復(fù)制因子,然后由 AerospikeDB 完成同步復(fù)制操作,保持即時一致 性;而且 AerospikeDB 可以透明地完成故障轉(zhuǎn)移; Redis是在內(nèi)存中運行的 ,AerospikeDB在內(nèi)存中存儲索引,在HDD、SSD中保存數(shù)據(jù),也可以在內(nèi)存 中

Aerospike架構(gòu)

Aerospike的簡介及使用方法是什么

Aerospike分為三個層次: Client層: 對Aerospike Server中的數(shù)據(jù)進行訪問。 包括CRUD、批量操作和基于二級索引的查詢 是一個“智能”客戶端,支持C/C++、Java、GoLang、Python、C#、Php、Ruby、JavaScript等絕大部分 主流語言。 追蹤節(jié)點感知數(shù)據(jù)存儲位置,當(dāng)節(jié)點啟動或停止時立即感知集群配置變化。 具有高效性、穩(wěn)定性和內(nèi)部的連接池 Distribution層:

負(fù)責(zé)管理集群內(nèi)部數(shù)據(jù)的平衡分布、備份、容錯和不同集群之間的數(shù)據(jù)同步。主要包含三個模塊:

  • Cluster Management Module 用于追蹤集群節(jié)點。關(guān)鍵算法是確定哪些節(jié)點是集群的一部分的Paxos-like一致投票過程。 Aerospike實現(xiàn)專門的心跳檢測(主動與被動),用于監(jiān)控節(jié)點間的連通性。

  • Data Migration Module 當(dāng)有節(jié)點添加或移除時,該模塊保證數(shù)據(jù)的重新分布,按照系統(tǒng)配置的復(fù)制因子確保每個數(shù)據(jù)塊跨 節(jié)點和跨數(shù)據(jù)中心復(fù)制。

  • Transaction Processing Module 確保讀寫的一致性與隔離性,寫操作先寫副本在寫主庫。該模塊包括: Sync/Async Replication(同步/異步復(fù)制):為保證寫一致性,在提交數(shù)據(jù)之前向所有副本傳播更 新并將結(jié)果返回客戶端。 Proxy (代理):集群重配置期間客戶端可能出現(xiàn)短暫過期,透明代理請求到其他節(jié)點。 Duplicate Resolution(副本解析):當(dāng)集群從活動分區(qū)恢復(fù)時,解決不同數(shù)據(jù)副本之間的沖突。

Data層: 負(fù)責(zé)數(shù)據(jù)的存儲,Aerospike 屬于弱語法的key-value數(shù)據(jù)庫。數(shù)據(jù)存儲模式如下:

Aerospike的簡介及使用方法是什么

namespace 命名空間,數(shù)據(jù)存在命名空間中,相當(dāng)于RDBMS中的database,最多可設(shè)置32個。 一個namespace可關(guān)聯(lián)多塊SSD,一塊SSD只關(guān)聯(lián)一個namespace,每個namespace下包含4096個分 片 set 集合,類似于數(shù)據(jù)庫表,一個namespace最多1023個set record 記錄,類似數(shù)據(jù)庫中的一行記錄, 采用弱語法(Schema-Less)的方式,bin可隨時增加或減少 key 主鍵,類型于數(shù)據(jù)表中的主鍵

bin 類似于數(shù)據(jù)庫字段,支持Java基本數(shù)據(jù)類型:List、Map、Blob, 一個namespace下最多32767個bin 索引:Aerospike Index包含主索引(Primary Index)和二級索引(Second Index),索引存儲在內(nèi)存 中,并不保存在硬盤里 一級索引(Primary-Index) 一級索引位于主鍵(key),采用hash表與紅黑樹的混合 二級索引(Secondary-Index) 二級索引位于非主鍵上,這允許一對多關(guān)系。采用哈希表和B-tree的混合 磁盤:Aerospike可以直接訪問硬盤中的raw blocks(原始塊)直接尋址,并特別優(yōu)化了Aerospike的最 小化讀、大塊寫和并行SSD來增加響應(yīng)速度和吞吐量。

Aerospike的使用
Aerospike的安裝

1、從網(wǎng)上下載aerospike-server-community-5.0.0.7-el6.tgz

wget https://www.aerospike.com/download/server/latest/artifact/el6

下載aerospike-server-community的最新版 2、解壓aerospike-server-community-5.0.0.7-el6.tgz

tar -zxvf aerospike-server-community-5.0.0.7-el6.tgz
mv aerospike-server-community-5.0.0.7-el6 aerospike-server

3、安裝aerospike-server和aerospike-tools

cd aerospike-server
./asinstall

4、驗證是否安裝成功

[root@192 aerospike-server]# yum list installed | grep aerospike
aerospike-server-community.x86_64 5.0.0.7-1.el6 installed
aerospike-tools.x86_64 3.26.2-1.el6 installed
#卸載aerospike
[root@localhost ~]# rpm -e aerospike-server-community.x86_64
[root@localhost ~]# rpm -e aerospike-tools.x86_64
[root@localhost ~]# rm -rf /etc/aerospike/

5、aerospike-server啟動、停止、重啟、狀態(tài)

systemctl start aerospike
systemctl stop aerospike
systemctl restart aerospike
systemctl status aerospike

6、aerospike-server管理:asadm

asadm 進入管理端
Admin> info
Admin> i net

7、aerospike-server操作 :aql

aql> show namespaces
+------------+
| namespaces |
+------------+
| "test" |
| "bar" |
Aerospike的基本概念

namespace 策略容器,類似RDBMS中的database,可以設(shè)置副本數(shù)、內(nèi)存大小、有效時長、存儲引 擎、文件存儲位置。 ? Sets 類似RDBMS中的表。 ? Records 類似RDBMS中的行 ? Bin 類似RDBMS中 的列,一行可以有多個bins。

Aerospike的數(shù)據(jù)操作
    --主鍵 bins 插入可以不同
    INSERT INTO <ns>[.<set>] (PK, <bins>) VALUES (<key>, <values>)
    DELETE FROM <ns>[.<set>] WHERE PK = <key>
    <ns> is the namespace for the record.
    <set> is the set name for the record.
    <key> is the record's primary key.
    <bins> is a comma-separated list of bin names.
    <values> is comma-separated list of bin values
    沒有update
    當(dāng)insert 同一pk時,數(shù)據(jù)為修改
    Examples:
    INSERT INTO test.demo (PK, foo, bar) VALUES ('key1', 123, 'abc')
    DELETE FROM test.demo WHERE PK = 'key1'
    insert into test.user1 (PK,name,age,sex,address) VALUES (2,'zhaoyun',21,
    'M','beijing')
    insert into test.user2(pk,name,sex,age) values(1,'zhangfei','M',23)
    -- pk都是1 則是對原紀(jì)錄的修改
    insert into test.user2(pk,name,sex,age) values(1,'diaochan','F',18)
    QUERY
    SELECT <bins> FROM <ns>[.<set>]
    SELECT <bins> FROM <ns>[.<set>] WHERE <bin> = <value>
    SELECT <bins> FROM <ns>[.<set>] WHERE <bin> BETWEEN <lower> AND <upper>
    SELECT <bins> FROM <ns>[.<set>] WHERE PK = <key>
    SELECT <bins> FROM <ns>[.<set>] IN <indextype> WHERE <bin> = <value>
    SELECT <bins> FROM <ns>[.<set>] IN <indextype> WHERE <bin> BETWEEN
    <lower> AND <upper>
    <ns> is the namespace for the records to be queried.
    <set> is the set name for the record to be queried.
    <key> is the record's primary key.
    <bin> is the name of a bin.
    <value> is the value of a bin.
    <indextype> is the type of a index user wants to query.
    (LIST/MAPKEYS/MAPVALUES)
    <bins> can be either a wildcard (*) or a comma-separated list of bin
    names.
    <lower> is the lower bound for a numeric range query.
    <upper> is the lower bound for a numeric range query.
    Examples:
        SELECT * FROM test.demo
        SELECT * FROM test.demo WHERE PK = 'key1'
        SELECT foo, bar FROM test.demo WHERE PK = 'key1'
        SELECT foo, bar FROM test.demo WHERE foo = 123
        SELECT foo, bar FROM test.demo WHERE foo BETWEEN 0 AND 999
        select * from test.user2 where name='zhaoyun'
        --沒有建立索引 ,不能查詢
        Error: (201) AEROSPIKE_ERR_INDEX_NOT_FOUND
        create index idx_1 on test.user2(name) string
        select * from test.user2 where name='zhaoyun'
        +-----------+-----+-----+-----------+
        | name | sex | age | address |
        +-----------+-----+-----+-----------+
        | "zhaoyun" | "M" | 21 | "beijing" |
        +-----------+-----+-----+-----------+
        CREATE INDEX <index> ON <ns>[.<set>] (<bin>) NUMERIC|STRING|GEO2DSPHERE
        CREATE LIST/MAPKEYS/MAPVALUES INDEX <index> ON <ns>[.<set>] (<bin>)
        NUMERIC|STRING|GEO2DSPHERE
        CREATE INDEX idx_foo ON test.demo (foo) NUMERIC
        DROP INDEX test.demo idx_foo
Aerospike的客戶端(Java)

pom.xml引入aerospike-client

<!-- https://mvnrepository.com/artifact/com.aerospike/aerospike-client -->
    <dependency>
        <groupId>com.aerospike</groupId>
        <artifactId>aerospike-client</artifactId>
        <version>4.4.9</version>
    </dependency>

aerospike-client的API使用

    //IP+port
    AerospikeClient client=new AerospikeClient("192.168.127.128",3000);
    //寫策略
    WritePolicy wp=new WritePolicy();
    //超時時間
    wp.setTimeout(1000);
    /*
    key
    */
    Key k1=new Key("test","user1",1);
    /*
    bins
    */
    // KV
    Bin b11=new Bin("name","zhangfei");
    Bin b12=new Bin("sex","M");
    Bin b13=new Bin("age",23);
    //寫值
    client.put(wp,k1,b11,b12,b13);
    //讀值
    Record r1=client.get(wp,k1,"name","age","sex");
    System.out.println(r1);
    System.out.println("===================================");
    Key k2=new Key("test","user1",2);
    /*
    bins
    */
    // KV
    Bin b21=new Bin("name","diaochan");
    Bin b22=new Bin("sex","F");
    Bin b23=new Bin("age",21);
    //寫值
    client.put(wp,k2,b21,b22,b23);
    /*
    取得指定key的數(shù)據(jù)
    */
    //批量執(zhí)行策略
    BatchPolicy bp=new BatchPolicy(wp);
    //key的數(shù)組
    Key[] ks={k1,k2};
    //循環(huán)輸出
    for(Record r:client.get(bp,ks)){
    System.out.println(r);
    }
Aerospike集群實現(xiàn)
Aerospike集群管理

集群處理節(jié)點成員身份,并確保當(dāng)前成員和所有集群中的節(jié)點保持一致。包括:集群視圖、節(jié)點發(fā)現(xiàn)和 視圖改變

集群視圖

每個Aerospike節(jié)點都會自動分配唯一的節(jié)點標(biāo)識符,是由MAC地址和監(jiān)聽端口組成的。包括: cluster_key:是一個隨機生成的8字節(jié)值,用于標(biāo)識集群視圖的實例。 succession_list:是作為集群一部分的唯一節(jié)點標(biāo)識符集合。

節(jié)點發(fā)現(xiàn)

節(jié)點間通過心跳消息來檢測節(jié)點的有效或失效 集群中的每個節(jié)點維護一個鄰接表,是最近向該節(jié)點發(fā)送心跳消息的其他節(jié)點列表 如果心跳超時,則表示該節(jié)點失效,從鄰接表中刪除 Aerospike還采用其他信息作為備用二次心跳機制 集群中的每個節(jié)點通過計算平均消息丟失來評估每個相鄰節(jié)點的運行狀態(tài)評分

視圖改變

領(lǐng)接表一旦發(fā)生改變,就會觸發(fā)運行一個Paxos共識算法來確定一個新的集群視圖。 Aerospike把鄰接表中節(jié)點標(biāo)識符最高的節(jié)點當(dāng)Proposer,并承擔(dān)Proposal的角色 Proposal提成新的集群視圖,如果被接受,則節(jié)點開始重新分配數(shù)據(jù)(Rebalence)

Aerospike數(shù)據(jù)分布

Aerospike有智能分區(qū)算法,即把用戶輸入的key在內(nèi)部根據(jù)RIPEMD-160算法,重新hash出一個key并 取前20位,然后相對均衡的把數(shù)據(jù)分布到各個節(jié)點之上。并且滿足namespace配置文件的配置(例如 保存多少個備份、是存在磁盤還是存在內(nèi)存中)。 每個Digest Space(namespace)被分為4096個不重疊的分區(qū)(partitions),它是Aerospike數(shù)據(jù)存 儲的最小單位

Aerospike的簡介及使用方法是什么

如上,一個4個節(jié)點的集群,每個節(jié)點存儲1/4數(shù)據(jù)的主節(jié)點,同時也存儲1/4數(shù)據(jù)的副本。如果節(jié)點1不 可訪問,節(jié)點1的副本將被拷貝到其他節(jié)點上。 復(fù)制因子( replication factor)是一個配置參數(shù),不能超過集群節(jié)點數(shù)。副本越多可靠性越高。 作為必須經(jīng)過所有數(shù)據(jù)副本的寫請求也越高。實踐中,大部分部署使用的數(shù)據(jù)因子為2(一份主數(shù)據(jù)和 一個副本)。 同步復(fù)制保證即時一致性,沒有數(shù)據(jù)丟失。在提交數(shù)據(jù)并返回結(jié)果給客戶端之前,寫事務(wù) 被傳播到所有副本。 主成功同時備成功后,客戶端認(rèn)為是成功 在集群重新配置期間,當(dāng)Aerospike智能終端發(fā)送請求到那些短暫過時的錯誤節(jié)點時,Aerospike智能集 群會透明的代理請求至正確的節(jié)點。

Aerospike集群配置和部署

有兩種方式可以搭建集群:Multicast組播方式(UDP)和Mesh網(wǎng)格方式(TCP) Multicast組播方式(UDP)

heartbeat {
    mode multicast
    multicast-group 239.1.139.1
    port 3000
    address 192.168.127.131
    interval 150
    timeout 10
}

udp是不可靠協(xié)議,所以有可能會造成節(jié)點脫落,而且網(wǎng)絡(luò)有可能不支持組播 Mesh網(wǎng)格方式(TCP)

heartbeat {
    mode mesh
    # add current node address here
    address 192.168.127.131
    port 3000
    # add all cluster node address here
    mesh-seed-address-port 192.168.127.131 3002
    mesh-seed-address-port 192.168.127.128 3002
    interval 150
    timeout 10
}
集群部署

在192.168.127.128上安裝Aerospike后,修改配置文件/etc/aerospike/aerospike.conf

vim /etc/aerospike/aerospike.conf
service {
    user root
    group root
    paxos-single-replica-limit 1 # Number of nodes where the replica count
    is automatically reduced to 1.
    pidfile /var/run/aerospike/asd.pid
    proto-fd-max 15000
}
    logging {
    # Log file must be an absolute path.
    file /var/log/aerospike/aerospike.log {
    context any info
	}
}
network {
    service {
        address any
        port 3000
        access-address 192.168.127.128 3002
    }
    heartbeat {
        mode mesh
        address 192.168.127.128
        port 3002
        #all cluster
        mesh-seed-address-port 192.168.127.128 3002
        mesh-seed-address-port 192.168.127.131 3002
        # To use unicast-mesh heartbeats, remove the 3 lines above, and
        see
        # aerospike_mesh.conf for alternative.
        interval 150
        timeout 10		
        }
    fabric {
        address any
        port 3001
    }
    info {
        address any
        port 3003
    	}
    }
    namespace test {
    replication-factor 2
    memory-size 256M
    storage-engine memory
    namespace bar {
        replication-factor 2
        memory-size 256M
        storage-engine memory
	}

在192.168.127.131上安裝Aerospike后,修改配置文件

public class GuavaDemo {
service {
user root
group root
paxos-single-replica-limit 1 # Number of nodes where the replica count
is automatically reduced to 1.
pidfile /var/run/aerospike/asd.pid
proto-fd-max 15000
}
}
logging {
# Log file must be an absolute path.
file /var/log/aerospike/aerospike.log {
context any info
}
}
network {
service {
address any
port 3000
access-address 192.168.127.131 3002
}
heartbeat {
mode mesh
address 192.168.127.131
port 3002
#all cluster
mesh-seed-address-port 192.168.127.131 3002
mesh-seed-address-port 192.168.127.128 3002
# To use unicast-mesh heartbeats, remove the 3 lines above, and
see
# aerospike_mesh.conf for alternative.
interval 150
timeout 10
}
fabric {
address any
port 3001
}
info {
address any
port 3003
}
}
namespace test {
replication-factor 2
memory-size 256M
storage-engine memory
}
namespace bar {
replication-factor 2
memory-size 256M
storage-engine memory
}
Aerospike集群的訪問
    Host[] hosts = new Host[]{
        new Host("192.168.127.128", 3000),
        new Host("192.168.127.131", 3000)
    };
    ClientPolicy policy = new ClientPolicy();
    AerospikeClient client = new AerospikeClient(policy, hosts);
    //寫策略
    WritePolicy wp = new WritePolicy();
    //超時時間
    wp.setTimeout(500);
    Key key1 = new Key("test", "SUser", "11");
    Bin bin11 = new Bin("name", "zhangfei-c");
    Bin bin12 = new Bin("age", 25);
    Bin bin13 = new Bin("sex", "M-c");
    client.put(wp, key1, bin11, bin12, bin13);
    Key key2 = new Key("test", "SUser", "22");
    Bin bin21 = new Bin("name", "zhaoyun-c");
    Bin bin22 = new Bin("age", 24);
    Bin bin23 = new Bin("sex", "M-c");
    client.put(wp, key2, bin21, bin22, bin23);
    Record r1 = client.get(wp, key1, "name", "age", "sex");
    System.out.println(r1);

看完上述內(nèi)容,你們掌握Aerospike的簡介及使用方法是什么的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI