歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Membase Java 教程

Membase Java 教程

日期:2017/2/28 16:08:56   编辑:Linux教程
Membase 是一個NOSQL數據庫。它被設計為在MEMCACHED(一個流行的內存緩存工具)之後的一個持久化存儲。Membase相對比較新但是已經在高性能NOSQL世界有了一個扎實的落腳點。

本文是一個通過JAVA使用Membase數據庫的快速教程。


安裝MEMBASE


參考Membase.org的說明


客戶端


與Membase的交互類似與Memcached的交互。我們將會使用SpyMemcached Java 客戶端。請從 這兒下載。


代碼


本項目中所有用到的代碼都提供在GitHub : https://github.com/sujee/membase-tutorial
這一個elipse工程,並且已經可以運行。


讓我們開始


這兒的JAVA代碼-它將一串key,value寫入Membase並且再將他們讀取出來。

// MembaseTest1 package tutorial;import java.net.InetSocketAddress;import net.spy.memcached.MemcachedClient;/*** Write / Read from Membase* * @author sujee**/public class MembaseTest1{    static int MAX = 100;    static String server = "localhost";    static int port = 11211;     public static void main(String[] args) throws Exception    {        MemcachedClient cache = new MemcachedClient(new InetSocketAddress(server, port));        cache.flush(); // 清除所有         long t1 = System.currentTimeMillis();        for (int i = 0; i < MAX; i++)        {            String s = new Integer(i).toString();            // key : integer converted to String (keys are always string)            // time to live : in seconds, 3600 seconds (1h), 0 means no expiration            // value : actual integer. This can be an object. Our integer will be converted to 'Integer'            // class by 'auto boxing' proess            Object o = cache.set(s, 0, i);            System.out.println("cache put : " + s + " : " + i + ", result " + o);        }        long t2 = System.currentTimeMillis();        System.out.println("Time for " + MAX + " puts is " + (t2 - t1) + " ms");         t1 = System.currentTimeMillis();        int nulls = 0;        for (int i = 0; i < MAX; i++)        {            String s = new Integer(i).toString();            Object o = cache.get(s);            System.out.println("Cache get : " + s + " : " + o);            if (o == null)                nulls++;        }        t2 = System.currentTimeMillis();        cache.shutdown();        System.out.println("Time for " + MAX + " gets is " + (t2 - t1) + " ms. nulls " + nulls);    }} 你可以在eclipse中運行這個文件(MembaseTest1)。或者從命令行執行
         sh compile.sh          sh run.sh              or           java -cp classes/:lib/memcached-2.5.jar tutorial.MembaseTest1
Membase運行在本地,端口為11211(默認的memcached端口)

我們在一開始就清洗了整個數據集,這樣我們就可以有個干淨的初始環境。

Set

cache.set (string_key, expiration_time, object_value)

我們的鍵是一串字符型的數字,我們的對象是是整數型對象。

下面是輸出樣例:


2010-11-29 23:36:33.234 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2010-11-29 23:36:33.240 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@b34bed0
cache put : 0 : 0, result net.spy.memcached.internal.OperationFuture@578088c0
cache put : 1 : 1, result net.spy.memcached.internal.OperationFuture@37922221
cache put : 2 : 2, result net.spy.memcached.internal.OperationFuture@5afec107
cache put : 3 : 3, result net.spy.memcached.internal.OperationFuture@b32e13d
cache put : 4 : 4, result net.spy.memcached.internal.OperationFuture@39617189
cache put : 5 : 5, result net.spy.memcached.internal.OperationFuture@2c64f6cd
...
...



Spy Memcache 客戶端緩存了很多操作,這樣可以提升性能。你可以看到cache.set方法返回了一個 'OperartionFuture' 對象。key-value最終將會在Membase中持久化。

Get

現在我們試著看看我們剛寫的讀取語句。 我們用了同樣的客戶端。輸出結果就像下面所示:

Cache get : 96 : 96
Cache get : 97 : 97
Cache get : 98 : 98
Cache get : 99 : 99
Time for 100 gets is 77 ms. nulls 0

我們密切注意NULL值。我們這次不應該獲得NULL,如我們所希望的null個數為0。

代碼也記錄了時間戳,我們可以看到操作是多麼得快。



模擬多個客戶端


在上面的例子中我們從同一個客戶端進行讀寫。但在實際場景中,多個客戶端都會對Membase進行讀寫。現在我們用兩個不同的客戶端連接模擬這種情況。
在這個版本中,我們使用一個連接進行寫操作另一個連接進行讀操作。

// MembaseTest2package tutorial; import java.net.InetSocketAddress;import net.spy.memcached.MemcachedClient; /*** simulates writing / reading from two different clients*/public class MembaseTest2{    static int MAX = 1000;    static String server = "localhost";    static int port = 11211;     public static void main(String[] args) throws Exception    {        MemcachedClient cache = new MemcachedClient(new InetSocketAddress(server, port));        cache.flush(); // clear all         long t1 = System.currentTimeMillis();        for (int i = 0; i < MAX; i++)        {            String s = new Integer(i).toString();              // key : integer converted to String (keys are always string)            // time to live : in seconds, 3600 seconds (1h), 0 means no expiration            // value : actual integer. This can be an object. Our integer will be converted to 'Integer'            // class by 'auto boxing' proess            Object o = cache.set(s, 0, i);            System.out.println("cache put : " + s + " : " + i + ", result " + o);        }        long t2 = System.currentTimeMillis();        cache.shutdown(); // close the client        System.out.println("Time for " + MAX + " puts is " + (t2 - t1) + " ms");         // open another connection        cache = new MemcachedClient(new InetSocketAddress(server, port));         t1 = System.currentTimeMillis();        int nulls = 0;        for (int i = 0; i < MAX; i++)        {            String s = new Integer(i).toString();            Object o = cache.get(s);            System.out.println("Cache get : " + s + " : " + o);            if (o == null)                nulls++;        }        t2 = System.currentTimeMillis();        cache.shutdown();         System.out.println("Time for " + MAX + " gets is " + (t2 - t1) + " ms. nulls " + nulls + "\n");    }}
java -cp classes/:lib/memcached-2.5.jar tutorial.MembaseTest2

Cache get : 0 : 0
Cache get : 1 : 1
.......
.....
Cache get : 997 : null
Cache get : 998 : null
Cache get : 999 : null
Time for 1000 gets is 540 ms. nulls 42


現在就能看到一個情況,當我們讀取剛剛設置的值的時候,就會有NULL產生的情況了。

那麼這兒到底發生了些什麼了?

請記住,Spy Memcache客戶端為了提高性能緩存了不少操作。當我們調用“shutdown”的時候客戶端沒有把所有的緩存值寫入數據庫就退出了。所以這些鍵值就很簡單的丟失了。

讓我們修正這個問題。

當我們做完SET操作, 讓我們更優雅的關閉這個客戶端。我們給他10秒的時間再退出。希望這樣可以讓客戶端有機會把所有的緩存操作都執行完成。

......long t2 = System.currentTimeMillis();cache.shutdown(10, TimeUnit.SECONDS); // graceful shutdownSystem.out.println("Time for " + MAX + " puts is " + (t2 - t1) + " ms"); // open another connectioncache = new MemcachedClient(new InetSocketAddress(server, port));......

java -cp classes/:lib/memcached-2.5.jar tutorial.MembaseTest3

output:
...
...
Cache get : 998 : 998
Cache get : 999 : 999
Time for 1000 gets is 500 ms. nulls 0

沒有任何NULL值。我們獲得了所有寫入的值。

所以現在我們很成功的模擬了多個客戶端的情況。有一件事情需要記住,客戶端做了緩存所以需要優雅的關閉它保證沒有數據丟失。
Copyright © Linux教程網 All Rights Reserved