I. Introduction
Nosql (not only SQL) has been on fire for a long time and has not been available for research. Recently, a project has certain requirements on Cache. I would like to take this opportunity to get started with nosql. There are many database systems that support nosql, such as redis and MongoDB. Each system has its own advantages and disadvantages. Based on the actual situation, we chose couchbase as the starting point for learning. Here there are several database comparisons: http://www.csdn.net/article/2013-04-15/2814886-nosql-benchmark
Couchbase is the product of the combination of membase and couchdb nosql databases. It is a distributed document-oriented nosql database management system with the simplicity and reliability of couchdb and the high performance of memcached. (Searched online ). This article describes how to build a couchbase environment and provides a helloworld example of a Java console Program (available on the official website ). Official learning address: http://docs.couchbase.com/
2. Preparations
(1) download couchbase server 2.5.1 http://www.couchbase.com/couchbase-server/overview
(2) install couchbase2.5.1, and make the corresponding configuration; can refer to: http://blog.csdn.net/qq415734794/article/details/7865826
(3) download couchbase client libraries: http://www.couchbase.com/communities/java/getting-started
Iii. Environment Construction
(1) Installation
After downloading the installation package, double-click to install
(2) Configuration
After the installation is complete, the address http: // localhost: 8091/index.html will be automatically opened, click setup, and configure as prompted, note that the memory size, sample buckets check beer-sample, and other user names and passwords are set by default. The following page is displayed:
In this way, the couchbase server is installed.
4. helloworld
(1) Create a New Java project in eclipse -- hellocouchbase
(2) create a Lib, add the corresponding jar package of the couchbase client listed below, and add it to build path:
For Maven projects, you can directly add dependency to Pom. xml:
<dependency> <groupId>com.couchbase.client</groupId> <artifactId>couchbase-client</artifactId> <version>1.4.3</version></dependency>
(3) create -- com. wzhang. App. hellocouchbase In the src directory
(4) Add the following code:
Public static void main (string [] ARGs) {arraylist <URI> nodes = new arraylist <URI> (); nodes. add (URI. create ("http: // 127.0.0.1: 8091/pools"); couchbaseclient client = NULL; try {client = new couchbaseclient (nodes, "default ","");} catch (exception e) {system. err. println ("error connecting to couchbase:" + E. getmessage (); system. exit (1);} // Add a record, key-Hello, value-couchbase try {client. set ("hello ", "Couchbase! "). Get ();} catch (exception e) {system. out. println (E. getstacktrace ();} // retrieve the key we just saved as Hello value string result = (string) client. get ("hello"); system. out. println (result); // closes the Client client. shutdown ();}
Description of the source code:
- Connect, The couchbaseclient object uses the nodes parameter to implement multi-server (cluster)
- GET/set, set is used to create and update, get is used and read to query the value in the database
- Disconnect: Call the shutdown method to shut down the server instance. If no parameter is set, the server will not close until all processing is completed. When you can pass in a given time as a parameter, such as client. shutdown (1, timeunit. seconds );
(5) running result:
(6) view the newly written value in databuckets on the Web console:
Click button "document ":
V. Source Code and related downloads
Source Code address: hellocouchbase.rar
Couchbase SERVER + couchbase client library Baidu Network Disk address: http://pan.baidu.com/s/1kTr7S7l
Learning Experience: it is quite easy to use couchbase to create a helloworld. Of course, couchbase also has a wealth of content to study in depth.