Aerospike API操作Map


 

Aerospike是一個高性能、可擴展、可靠性強的NoSQL解決方案,支持RAM和SSD作爲存儲介質,並專門針對SSD特殊優化,廣泛應用於實時競價等實時計算領域。官方保證99%的操作在1ms內完成,並提供集羣數據自動Rebalance、集羣感知客戶端等功能,且支持超大規模數據集(100T級別)的存儲。

作爲KV存儲,Aerospike提供多種數據類型,其操作方式和Redis比較類似。除基礎功能之外,Aerospike還支持AMC控制檯、API等多種監控方式,有集羣QPS、健康度、負載等多項監控指標,對運維比較友好。支持集羣內數據的自動Rebalance,和Redis集羣方案相比,維護成本下降不少,高負載下AS比redis略快,參考https://www.infoq.cn/article/2015%2F02%2Faerospikedb-redis-aws-nosql

Aerospike一般操作可以通過官方渠道獲取到例子信息

https://github.com/aerospike/aerospike-client-java/tree/master/examples

下面是官方沒有的map操作例子:

 

import com.aerospike.client.AerospikeClient;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.Value;
import com.aerospike.client.cdt.MapOperation;
import com.aerospike.client.cdt.MapPolicy;
import com.aerospike.client.cdt.MapReturnType;
import com.aerospike.client.policy.WritePolicy;

import java.util.List;

public class AerospikeApiTest {

    public static  void main(String args[]){
        final AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);

        WritePolicy policy = new WritePolicy();
        policy.socketTimeout = 50;  // 50 millisecond timeout.
        Key key =  new Key("test", "demoset", "mapkey");

        //map中寫入1個值
        Record record = client.operate(policy, key, MapOperation.put(MapPolicy.Default, "mapbin", Value.get("key4"), Value.get("val1")));

        //map中一次寫入2個值
        record = client.operate(policy, key, MapOperation.put(MapPolicy.Default, "mapbin", Value.get("key3"), Value.get("val1")),
                MapOperation.put(MapPolicy.Default, "mapbin", Value.get("key4"), Value.get("val1")));

        //刪除map中的key,並查詢map的長度
        List list = client.operate(policy, key, MapOperation.removeByKey("mapbin", Value.get("key4"), MapReturnType.COUNT),
                MapOperation.size("mapbin")).getList("mapbin");
        System.out.println(list.get(1));
        //如果爲空map刪除數據
        if (((Long)list.get(1)) == 0) {
            client.delete(policy, key);
        }
    }
}

上面代碼在高併發下,刪除可能會有問題,如刪除某個key時,正好有對key的寫操作,就會造成誤刪,歡迎留言更嚴謹的刪除方案。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章