Membase,Memcached,Couchbase1.8 JAVA 简单调用 操作

昨天写了关于 C#版操作Membase的实例 http://blog.csdn.net/qq415734794/article/details/7865716

今天分享个JAVA的操作实例吧:

首先还是下载JAVA版的client包吧: http://packages.couchbase.com/clients/java/1.0.3/Couchbase-Java-Client-1.0.3.zip里面就是些JAR包

我本机WIN7 64位 ,使用的是myeclipse。

新建一个Maven项目

完成后,在下图红线框所示的位置添加Membase的引用jar包

接着我们在App.java文件中写如下的code:

package MavenApp;

import java.util.concurrent.TimeUnit;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;

/**
 * Hello world!
 * 
 */
public class App {
	public static void main(String[] args) {
		//定义一个client
		MemcachedClient client;
		try {
			//实例化client,为client指定数据库地址
			client = new MemcachedClient(AddrUtil.getAddresses("10.1.18.45:11211"));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return;
		}
		//获取名为item的键值对
		Object item = client.get("item");		
		if (item == null) {
			System.out.println("未找到item对象!");
			System.out.println("正在插入item对象!");
			client.set("item", 10, "Hello World! I am Billy.lee!");
			System.out.println("插入item对象成功,请再次运行获取此对象数据!");
		} else {
			//输出内容
			System.out.println((String) item);
		}
		client.waitForQueues(1, TimeUnit.MINUTES);
		System.exit(0);
	}
}

接着我们在AppTest.java文件中写如下code:

package MavenApp;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
    	App.main(null);
    }
}

在testApp()方法上右键,进行JUnit测试

第一次运行,如下图:

第二次运行,如下图:

至此,JAVA版的操作完成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值