Couchbase environment construction and Java Test

Source: Internet
Author: User

Couchbase Server is a cluster-based, document-based database system. For more information, see:

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

Couchbase home page: http://www.couchbase.com/

The goal of this article is to build a simple couchbase environment and perform read/write tests in Java.

1. Preparations

1) download the couchbase server. The version used in this article is 1.8.1 for Win32, and 2.0 cannot be installed on my machine, causing kernel problems.

2) download the Java-related class library

2. Install couchbase Server

1) Detailed installation process, see: http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-getting-started-install-win.html

2) After the installation is complete, the couchbase console program will automatically open the http: // localhost: 8091 address, which may not be opened at this time. The most likely problem is that the port is occupied.

The default port of couchbase is 8091. Run the following command to troubleshoot the problem:

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

The port is occupied by a process with process no. 1751. Run the following command:

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

Open the task manager and end the corresponding program based on the process ID. Open the server/bin directory under the couchbase installation target, for example, D: \ Program Files \ couchbase \ Server \ bin after executing the service_start.bat batch processing command, check whether the couchbaseserver service is started in the service.

Then, open http: // localhost: 8091 to automatically install the webconsole of couchserver. For details, refer to: Installing.

3.compile the example to test all the jar packages in couchbase-java-client-1.1.4.zip.

New write test class:

Import Java. io. ioexception; import java.net. uri; import Java. util. using list; 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 {// document key public static final string key = "beer_wrath"; // expiration time of the document (use 0 to persist forever) // expiration time (unit: milliseconds 0 permanent) public static final int exp_time = 0; // Document Value: 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 region list <URI> (); // server address (which can be viewed in couchbase backend server nodes) uris. add (URI. create ("http: // 192.168.10.15: 8091/pools"); couchbaseclient client = NULL; try {// check 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); // check whether the setting is successful. 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 (); // close the Client client 3 seconds after the operation is completed. shutdown (3, timeunit. seconds); system. exit (0 );}}

After running, the set succeeded text indicates that the setting is successful. Write test classes for reading:

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);}}

Since the time set in the write example is permanent, the correct output here should be:

{"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"}

We can also view the key we just set in the Data buckets of the couchbase webconsole background.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.