Couchbase之环境搭建与Java小试

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.




©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页