Couchbase之環境搭建與Java小試

來源:互聯網
上載者:User

Couchbase Server 是一個叢集化的、基於文檔的資料庫系統,網上有MongoDB與Couchbase的對比,請參考:

http://www.javaworld.com/javaworld/jw-03-2013/130321-mongodb-vs-couchbase-nosql.html

Couchbase首頁:http://www.couchbase.com/

本文的目標是搭建簡單的Couchbase環境並用Java語言進行讀寫測試。

1.準備工作

1)下載 Couchbase Server ,本文用到的版本是1.8.1 for win32 ,2.0在我的機器上裝不上,核心問題。

2)下載 Java相關類庫

2.安裝Couchbase Server

1)安裝詳細過程請參考:http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-getting-started-install-win.html

2)安裝完成後Couchbase Console程式會自動開啟http://localhost:8091地址,這時候有可能會打不開,最有可能出現的問題就是連接埠被佔用了。

Couchbase預設連接埠是:8091,運行如下命令排查: 

netstat -ano|findstr "8091"TCP    127.0.0.1:9050         0.0.0.0:0              LISTENING       1751

連接埠被進程號為1751的進程佔用,繼續執行下面命令:

tasklist|findstr "1751"spool.exe                     1751 Console                 0     16,064 K

開啟工作管理員根據進程ID結束掉相應程式。這時開啟Couchbase安裝目標下的server/bin目錄,形如:D:\Program Files\Couchbase\Server\bin執行service_start.bat批處理命令後,在服務裡檢查CouchbaseServer服務是否啟動。

這時再開啟http://localhost:8091就能自動CouchServer 的webconsole安裝頁面了,參考:http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-getting-started-setup.html進行安裝配置。

3.編寫例子進行測試用eclipse或其它IDE建立Project,匯入之前下載的Couchbase-Java-Client-1.1.4.zip中的所有jar包。

建立寫入測試類別:

import java.io.IOException;import java.net.URI;import java.util.LinkedList;import java.util.List;import java.util.concurrent.ExecutionException;import java.util.concurrent.TimeUnit;import net.spy.memcached.internal.OperationFuture;import com.couchbase.client.CouchbaseClient;public class Main {  //文檔key  public static final String KEY = "beer_Wrath";  // expiration time of the document (use 0 to persist forever)  //到期時間(單位毫秒  0 永久)  public static final int EXP_TIME = 0;  //文檔值  public static final String VALUE =          "{\"name\":\"Wrath\",\"abv\":9.0,"              + "\"type\":\"beer\",\"brewery_id\":\"110f1a10e7\","              + "\"updated\":\"2010-07-22 20:00:20\","              + "\"description\":\"WRATH Belgian-style \","              + "\"style\":\"Other Belgian-Style Ales\","              + "\"category\":\"Belgian and French Ale\"}";  public static void main(String args[]) {    List<URI> uris = new LinkedList<URI>();    //伺服器位址(可在Couchbase後台Server NODES中查看)    uris.add(URI.create("http://192.168.10.15:8091/pools"));    CouchbaseClient client = null;    try {      //在Couchbase背景Data Buckets中查看      client = new CouchbaseClient(uris, "default", "");    } catch (IOException e) {      System.err.println("IOException connecting to Couchbase: " + e.getMessage());      System.exit(1);    }    OperationFuture<Boolean> setOp = client.set(KEY, EXP_TIME, VALUE);    //檢查是否設定成功    try {      if (setOp.get().booleanValue()) {        System.out.println("Set Succeeded");      } else {        System.err.println("Set failed: " + setOp.getStatus().getMessage());      }    } catch (InterruptedException e) {      System.err.println("InterruptedException while doing set: " + e.getMessage());    } catch (ExecutionException e) {      System.err.println("ExecutionException while doing set: " + e.getMessage());    }    System.out.println();    //完成操作後3秒後關閉client    client.shutdown(3, TimeUnit.SECONDS);    System.exit(0);  }}

運行後看到Set Succeeded字樣表示設定成功。編寫測試類別進行讀取:

import java.io.IOException;import java.net.URI;import java.util.LinkedList;import java.util.List;import java.util.concurrent.TimeUnit;import com.couchbase.client.CouchbaseClient;public class Client{public static void main(String[] args){    List<URI> uris = new LinkedList<URI>();    uris.add(URI.create("http://192.168.10.15:8091/pools"));    CouchbaseClient client = null;    try {      client = new CouchbaseClient(uris, "default", "");    } catch (IOException e) {      System.err.println("IOException connecting to Couchbase: " + e.getMessage());      System.exit(1);    }    Object o  = client.get("beer_Wrath");    System.out.println(o);    client.shutdown(3, TimeUnit.SECONDS);    System.exit(0);}}

由於寫入例子中設定的時間是永久,所以這裡正確的輸出應該是:

{"name":"Wrath","abv":9.0,"type":"beer","brewery_id":"110f1a10e7","updated":"2010-07-22 20:00:20","description":"WRATH Belgian-style ","style":"Other Belgian-Style Ales","category":"Belgian and French Ale"}

我們還能在Couchbase webconsole背景Data buckets中查看到我們剛才設定的key.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.