`

Membase和Java入门

阅读更多

介绍

现在你已经安装了Membase并且可能已经创建了Membase服务器集群,现在就可以安装客户端库 spymemcached, 并且开始存储数据了。

下面是本文的纲要:

  1. 下载Java Membase客户端库,spymemcached.

  2. 创建一个功能层,并且将Membase客户端库设置为引用库(referenced library).

  3. 写一个简单的程序演示连接到Membase并且存储数据。

  4. 展示一些简单程序中没有涉及的API方法。

下载Membase客户端库

Couchbase下载客户端库。这是一个你可以用在你的Java环境中的jar文件。

Hello Membase

你可能很好奇,最简单的与Membase通信的Java程序会是什么样,你应该怎么用Java命名行工具编译并且运行它。如果有兴趣请看下面的清单1。

清单1: Main.java

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;

public class Main {

    public static void main(String[] args) {

        MemcachedClient client;

        try {
            client = new MemcachedClient(AddrUtil.getAddresses(args[0]));
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        Object spoon = client.get("spoon");

        if (spoon == null) {
            System.out.println("There is no spoon.");
            client.set("spoon", 10, "Hello World!");
        } else {
            System.out.println((String)spoon);
        }

        client.waitForQueues(1, TimeUnit.MINUTES);

        System.exit(0);
    }

}
  1. 将清单1中的代码放入一个名为Main.java的文件中。

  2. 将下载的memcached-2.6.jar文件放到同一个目录。

  3. 输入下面的命令:

    $ javac -cp memcached-2.6.jar Main.java
    $ java -cp .;memcached-2.6.jar Main 10.0.0.33:11211

当然,需要使用你自己的Memcache服务器IP地址。如果你在Linux或者MacOS上操作,将第二个命令行中的分号换成冒号。程序将会产生下面的输出:

2011-05-12 22:11:56.281 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=/10.0.0.33:11211,
#Rops=0, #Wops=0, #iq=0,
topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2011-05-12 22:11:56.284 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl
@15dfd77
There is no spoon.

这个输出的大部分内容都是客户端库产生的日志,告诉你客户端内部进行到哪一步,可以帮助你分析问题。这儿说的是一个Membase连接已经添加,并且连接状态已经改变。然后代码显示键 spoon 没有在Membase中存在。

在10秒钟之内再次运行这个程序将会产生如下输出:

2011-05-12 22:14:15.800 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=/10.0.0.33:11211, #Rops=0, #Wops=0, #iq=0,
topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2011-05-12 22:14:15.803 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl
@15dfd77
Hello World!

你再一次看到日志信息,根据这次的指示,键 spoon 的值“Hello World”在Membase中被找到了。恭喜,你已经在一个更大的世界中迈出了一小步。

Membase API 概述

Membase客户端库有很多API方法,你可以用他们实现你的分布式内存魔力。下面将MemcachedClient方法分为几个类别,为你以后的工作提供一个快速参考。

表1. 同步方法(Synchronous Methods)

decr 减少键并返回值Decrement a key and return the value.
get 从缓存中获得一个特定值Gets a particular value from the cache.
getBulk 同时获得多个值Gets many values at the same time.
gets 获得一个值以及检查和设置支持Gets a particular value with Check And Set support.
incr 增加一个键的值Increment the value of a key.

表 2. 同步检查和设置(Synchronous Check And Set)

cas 执行检查和设置操作Perform a Check And Set operation.

表3. 异步方法(Asynchronous Methods)

add 在缓存中增加一个对象,如果taAdds an object to the cache if it does not exist already.
delete 从缓存中删除一个值Deletes a value from the cache.
flush 将所有服务器上的缓存清空Clears the cache on all servers.

表4.异步检查和设置( Asynchronous Check And Set)

append Append to an existing value in the cache.
asyncCAS Check and set values of particular keys.
asyncDecr Decrement a value.
asyncGet Get a particular value.
asyncGetBulk Get many values at the same time.
asyncGets Get a value with CAS support.
asyncIncr Increment a value.

表5. 状态方法(Status Methods)

addObserver Adds an observer to watch the connection status.
getAvailableServers Returns a list of, shocker, available servers.
getNodeLocator Returns a read only instance of the node locator.
getStats Returns connection statistics.
getTranscoder Returns the default transcoder instance.
getUnavailableServers Returns a list of the servers that are not available.
getVersions Returns the versions of all connected servers.

一个改动较大的程序A More Substantial Program

如果你对改动较大的程序有兴趣,请下载样例代码和Eclipse工程,这样你就可以在Eclipse开发环境中运行了。这个程序会更具用户配置创建多个线程,每个线程都会存入(或者读取)100个随机数。每个线程都会创建一个MemcachedClient对象实例,然后执行gets()操作查找指定的keys。如果键没有被设置,gets()操作就会返回null。这种情况下线程自己会创建值并且设置到Membase中,这样会花费100毫秒去执行。这种情况模拟了一个高昂的数据库操作。你可以在文章末尾找到这个简单程序的完整源代码。

让我们讨论这个程序中的一些细节,这样你可以理解连接Membase服务器,测试已有键值对,设置一个键值的一些基础知识。这些操作将会让你知道如何开始。

清单2. 连接到一系列的Membase服务器:

65:                 MemcachedClient client = new MemcachedClient(
 66:                         AddrUtil.getAddresses(addresses));

从这几行你可以看到你需要创建一个MemcachedClient实例。有很多的方法去创建,不过我认为涉及AddrUtil 类的构造器很重要,这个类可以解析用逗号或者空格分隔的服务器地址和端口列表,例如下面所列:

host-or-ip:port host-or-ip:port

你所连接的端口将会成为MOXI端口,11211是一个有效地代理,它知道集群中的其他所有服务器并且提供快速协议接入。所以这种集群情况下,按照下面格式提供一个地址字符串会很好的进行工作:

String addresses = "10.0.0.33:11211 10.0.0.57:11211";

清单 3 is an abridged excerpt that shows the creation of an IntegerTranscoder, which is a useful class for converting objects in Membase back to integers when needed. This is for convenience and reduces type casting. You can then see a line 82 that a the gets() method is called. This returns a CASValue<T> of type integer which is useful for checking and setting a value. If the value is null it means that membase hasn't been given a value for this key. The code then sets a value. Otherwise, we can get its value and do something with it.

清单 3. 检查和设置操作

67:                 IntegerTranscoder intTranscoder = new IntegerTranscoder();
 68:

 82:                     CASValue<Integer> value = client.gets(key,
 83:                             intTranscoder);
 84:
 85:                     if (value == null) {
 86:                         // The value doesn't exist in Membase
 87:                         client.set(key, 15, rand.nextInt(), intTranscoder);
 88:
 89:                         // Simulate the value taking time to create.
 90:                         Thread.sleep(100);
 91:
 92:                         created++;
 93:
 94:                     } else {
 95:
 99:                         int v = value.getValue();
100:
107:                     }

Setting values in Membase are done asynchronously, and the application does not have to wait for these to be completed. Sometimes, though, you may want to ensure that Membase has been sent some values, and you can do this by calling client.waitForQueues() and giving it a timeout for waiting for this to occur, as shown in Listing 4.

清单4. 等待数据设置进Membase。

109:                     client.waitForQueues(1, TimeUnit.MINUTES);

How to Build and Run the Sample Application

In order to build and run the sample application, you should unzip the attached sample to a directory on your hard drive. The sample application comes with an Eclipse project file and can easily be imported into Eclipse using the following:

  1. Choose File | Import > General > Existing Projects into Workspace.

  2. In the Import Project dialog, click the Browse button next to the Select root directory text field and navigate to where you unzipped the sample application.

  3. Make sure that the GettingStarted project is selected, and hit the Finish button.

You should now have the project imported into your project explorer in Eclipse. Next you will want to run the application.

  1. Right click the GettingStarted project and select Run As | Run Configurations

  2. Double click the "Java Application" node to create a new configuration.

  3. Name this "Membase Client".

  4. Click the "Main" tab.

  5. Click the Search button and choose the com.acme.membase.gettingstarted.Main class to run.

  6. Click the "Arguments" tab.

  7. In the "Program Arguments" tab type something like: "10.0.0.33:11211" 5

  8. Click the "Apply" button.

  9. Click the "Run" button.

Running this program generates the following output the first time:

Client-2 took 37.2500 ms per key. Created 35. Retrieved 65 from cache.
Client-3 took 37.7800 ms per key. Created 35. Retrieved 65 from cache.
Client-4 took 37.7100 ms per key. Created 35. Retrieved 65 from cache.
Client-0 took 37.8300 ms per key. Created 35. Retrieved 65 from cache.
Client-1 took 37.8400 ms per key. Created 35. Retrieved 65 from cache.

Running the program a second time before 15 seconds elapses, produces this output instead:

Client-1 took 4.6700 ms per key. Created 0. Retrieved 100 from cache.
Client-3 took 4.6000 ms per key. Created 0. Retrieved 100 from cache.
Client-4 took 4.7500 ms per key. Created 0. Retrieved 100 from cache.
Client-2 took 4.7900 ms per key. Created 0. Retrieved 100 from cache.
Client-0 took 4.8400 ms per key. Created 0. Retrieved 100 from cache.

There are a few things that are interesting about the output. In the first scenario, the five threads collaborate to produce the sequence of random numbers such that the average time per key is significantly less than 100ms. Each thread is creating 35 numbers, but reading 65 from the cache.

In the second run, because the 15 second timeout has not elapsed yet, all of the random numbers were retrieved from the cache by all of the threads. Notice that reading these values from Membase only takes a few milliseconds.

Conclusion

You now know how to obtain the Membase Java client library, and write small Java programs to connect with your Membase cluster and interact with it. Congratulations, you will be able to save your servers from burning down, and impress your users with the blazing fast response that your application will be able to achieve.

分享到:
评论

相关推荐

    Membase1.7.1第二部分(共二部分)

    在安装和配置方面,Membase提供了有效的图形化界面和编程接口,包括可配置 的告警信息。 Membase的目标是提供对外的线性扩展能力,包括为了增加集群容量,可以针对统一的节点进行复制。 另外,对存储的数据进行再...

    Couchbase MemBase C#/.NET简单调用

    在C#和.NET环境中,与MemBase交互主要依赖于Couchbase的.NET客户端库,这使得.NET开发者能够方便地将MemBase集成到他们的应用程序中。本文将深入探讨如何进行Couchbase MemBase的C#/.NET简单调用。 首先,安装...

    Membase part1

    Membase part1

    NorthScale_Membase_Server_User_Guide.pdf

    NorthScale Membase Server User Guide全面覆盖了从安装到日常管理的所有方面,是运维人员和开发人员不可或缺的手册。通过本指南的学习,用户不仅可以掌握如何有效地使用该软件,还能深入了解其背后的设计理念和技术...

    nosql 入门教程

    第一部分 NoSQL入门 第1章 NoSQL的概念及适用范围 2 1.1 定义和介绍 3 1.1.1 背景与历史 3 1.1.2 大数据 5 1.1.3 可扩展性 7 1.1.4 MapReduce 8 1.2 面向列的有序存储 9 1.3 键/值存储 11 1.4 文档数据库 ...

    ns_server:Membase服务器超级管理程序

    您应该按照此处的说明使用顶级make文件和回购清单: : 运行时依赖 在启动服务器之前,您可能需要执行以下操作 确保未使用所需的端口(这些端口包括8091、11211、11212等)。 跑步 通过顶层makefile构建所有内容后...

    nosql.docx

    Membase、MongoDB 和 Riak 都是 NoSQL 家族的重要成员,各自具备独特的特性和优势。 Membase 是一个高性能、易扩展的键值存储系统,特别适合用作分布式缓存。它由 memcached 开发团队的核心成员创建,并得到 Zynga ...

    开发周刊003期

    - **Membase 1.7发布**:开源NoSQL系统Membase推出了1.7版本,标志着NoSQL技术在大数据处理和高性能需求场景下的持续进步。此版本的发布反映了NoSQL数据库在应对大规模数据存储和快速访问需求方面的强大能力。 - **...

    5款最好的免费Linux缓存系统.pdf

    2. Java Caching System (JCS):JCS是一个基于Java的缓存系统,提供了高性能的缓存服务。JCS可以将对象缓存在内存、硬盘或数据库中,并且可以在分布式环境中提供高性能的缓存服务。 JCS的优点在于它可以提供高性能...

    SQLite学习手册-中文全本.pdf

    SQLite与BerkeleyDB、MemBASE等NoSQL存储引擎相比,虽然都是嵌入式的,但SQLite更倾向于提供传统关系型数据库的支持和便利性。 SQLite的主要特征包括管理上的简便性,几乎可以做到无需管理。它的操作非常方便,生成...

    8种Nosql数据库系统对比

    除此之外,其他NoSQL数据库,如CouchDB、Riak、Membase和Neo4j,各有其独特的特性和适用场景。CouchDB使用Erlang编写,强调数据一致性,适合需要版本控制和双向复制的应用。Riak是基于分布式键值存储的数据库,适合...

    couchbase-server-community_6.0.0-windows_amd64.msi

    couchbase是一个非关系型数据库,它实际上是由couchdb+membase组成,所以它既能像couchdb那样存储json文档,也能像membase那样高速存储键值对。主要有以下几个特点: 速度快 由于是放在内存中的数据库,所有的读写...

    传智 韩忠康 mysql 课程笔记1(吐血整理).docx

    而在NoSQL领域,例如MongoDB和MemBase,数据存储方式更灵活,如键值对,适合大数据和分布式环境。 数据库(DB)是存储数据的仓库,而表(table)是数据的具体容器,每张表包含多个行(record)和列(field)。行...

    SQLite教程(一):SQLite数据库介绍

     SQLite是目前最流行的开源嵌入式数据库,和很多其他嵌入式存储引擎相比(NoSQL),如BerkeleyDB、MemBASE等,SQLite可以很好的支持关系型数据库所具备的一些基本特征,如标准SQL语法、事务、数据表和索引等。...

    基于.NET平台常用的框架整理

    Membase作为重量级的NoSQL数据库成员,提供了与MongoDB不同的分布式文件存储解决方案。 自动任务调度框架***和Topshelf提供了快速简便的作业调度和Windows服务创建功能,便于开发者管理后台任务和系统服务。 依赖...

    couchbase的使用手册

    它起源于Membase Server 1.7,并在1.8版本中加入了一些修复和改进。Couchbase的设计目的是为了能够提供高性能、可扩展性和易于管理的数据存储,它特别适合用于构建大型的Web应用。 ### Couchbase Server简介 ...

    [转]CouchBase (一)安装篇

    2. **集群支持与数据持久化:** 支持多服务器集群部署,数据可以在集群内进行切片和复制,提高系统的可靠性和数据的安全性。同时支持数据的持久化存储,确保即使在服务器故障的情况下也能恢复数据。 3. **文档数据库...

    couchbase安装

    2. 继承Membase的特性,支持数据集群和持久化存储,确保即使在服务器故障时也能保持数据完整性。 3. 引入了CouchDB的文档模型,通过View功能对数据进行复杂查询和操作,弥补了Memcache在数据处理能力上的不足,但...

    SQLite学习手册_中文全本1

    SQLite是一种广泛应用的开源嵌入式数据库系统,相比其他NoSQL存储引擎,如BerkeleyDB和MemBASE,它提供了标准的关系型数据库特性,如SQL语法、事务处理、数据表和索引。尽管SQLite具有这些特性,但其设计目标是轻量...

Global site tag (gtag.js) - Google Analytics