Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the environment building and Java testing in Couchbase?

2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the environment building and Java testing in Couchbase. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Couchbase Server (formerly Membase) is a distributed document-oriented NoSQL database management system that combines the simplicity and reliability of CouchDB and the high performance of Memcached.

There is a comparison between MongoDB and Couchbase online. Please refer to:

Http://www.infoworld.com/d/data-management/nosql-showdown-mongodb-vs-couchbase-214912

Couchbase Home Page: http://www.couchbase.com/

The goal is to build a simple Couchbase environment and do read-write testing in the Java language.

1. Preparatory work

1) download Couchbase Server. The version used in this article is version 2.1.1 for win64, which cannot be installed on some machines due to kernel problems.

2) download Java-related class libraries

Related download link: http://www.couchbase.com/download

two。 Install Couchbase Server

1) the installation process is simple, just download and install it on the official website.

2) after the installation, the Couchbase Console program will automatically open the http://localhost:8091 address, which may not be opened at this time, and the most likely problem is that the port is occupied. (you can use cmd to check whether it is occupied or not.)

At this point, open http://localhost:8091 and you can automatically CouchServer the webconsole installation page.

(interface diagram)

3. Write examples for testing

Create a new Project with eclipse or other IDE and import all the jar packages in the previously downloaded Couchbase-Java-Client-1.1.9.zip.

Create a new write test class:

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 {/ / document key public static final String KEY = "testkey1" / / expiration time of the document (use 0 to persist forever) / / expiration time (in milliseconds 0 * *) public static final int EXP_TIME = 0 / / document value public static final String VALUE = "{\" myname\ ":\" EugeneQiu\ "," + "\" updated\ ":\" 2013-08-23 13:47:00\ "," + "\" description\ ":\" Just a simple test\ "," + "\" myblog\ ":\" http://my.oschina.net/EugeneQiu\"}"; Public static void main (String args []) {List uris = new LinkedList (); / Server address (which can be viewed in the Couchbase background Server NODES) uris.add (URI.create ("http://127.0.0.1:8091/pools")); CouchbaseClient client = null; try {/ / View client = new CouchbaseClient (uris," default ",") in the Data Buckets of the Couchbase background) } catch (IOException e) {System.err.println ("IOException connecting to Couchbase:" + e.getMessage ()); System.exit (1);} OperationFuture setOp = client.set (KEY, EXP_TIME, VALUE); / / check whether 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 client client.shutdown (3, TimeUnit.SECONDS); System.exit (0);}} 3 seconds after completing the operation

After running, seeing the word Set Succeeded indicates that the setting is successful.

Write a test class to read:

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 uris = new LinkedList (); uris.add (URI.create ("http://127.0.0.1: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 ("testkey1"); System.out.println (o); client.shutdown (3, TimeUnit.SECONDS) System.exit (0);}}

Since the time set for writing in the example is *, the correct output here should be:

We can also find the key we just set in the Data buckets in the background of Couchbase webconsole. (the mislayout of the components in the picture is the compatibility problem that I forced to shrink the web page.)

So far, a simple build and test has been completed.

On the Couchbase environment building and Java testing is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 211

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report