tokyotyrant-java客户端[转]

目录:

概述
演示
[一]、概述

java实现了对ttserver服务端的连接和访问。相关的源代码和jar包可以到其官网下载。

官网地址:http://code.google.com/p/tokyotyrant-java/

如果是maven构建项目的,在pom.xml 的节点中增加如下依赖配置即可:
[xml]
<dependency>
<groupId>tokyotyrant</groupId>
<artifactId>tokyotyrant</artifactId>
<version>0.11</version>
</dependency>
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.1.5.GA</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
[/xml]
[二]、演示

1.RDB :官方Tokyo Tyrant API的实现

演示代码:RDBExample.java

[java]
package com.micmiu.nosql.ttserver;

import java.io.IOException;
import java.net.InetSocketAddress;

import tokyotyrant.RDB;
import tokyotyrant.transcoder.DoubleTranscoder;
import tokyotyrant.transcoder.IntegerTranscoder;

/**
*
* blog http://www.micmiu.com
*
* @author Michael
*
*/
public class RDBExample {

public static void main(String[] args) throws IOException {

RDB db = new RDB();
try {
// connect to the server
// db.open(new NodeAddress("tcp://192.168.126.134:1978"));
db.open(new InetSocketAddress("192.168.126.134", 1978));

Object key;
Object value;
// store records
if (db.put("my_firstname", "Sun")) {
System.out.println("db put my_firstname successful.");
} else {
System.out.println("db put my_firstname error.");
}

if (db.put("my_lastname", "Michael")) {
System.out.println("db put my_lastname successful.");
} else {
System.out.println("db put my_lastname error.");
}

if (db.put("my_blogurl", "www.micmiu.com")) {
System.out.println("db put my_blogurl successful.");
} else {
System.out.println("db put my_blogurl error.");
}

if (db.put("my_weibo", "www.sina.com/ctosun")) {
System.out.println("db put my_weibo successful.");
} else {
System.out.println("db put my_weibo error.");
}

// retrieve records
value = db.get("my_blogurl");
System.out.println("test_blogurl =: " + value);

value = db.get("test_noexit");
System.out.println("test_noexit =: " + value);

System.out.println("===== test repeat put ");
db.put("test_desc", "hello world");
System.out.println("test_desc =: " + db.get("test_desc"));
db.put("test_desc", "repeat put value is hello Michael");
System.out.println("test_desc =: " + db.get("test_desc"));

// Initialize the iterator
System.out.println("===== access all key ");
db.iterinit();
while ((key = db.iternext()) != null) {
value = db.get(key);
System.out.println(key + " =: " + value);
}
System.out.println("===== test int double ");
// add int
db.put("int_i", 3, new IntegerTranscoder());
int i = db.addint("int_i", 4);
System.out.println(" i =: " + i);
System.out.println("int_i =: "
+ db.get("int_i", new IntegerTranscoder()));

// add double
db.put("dou_d", 3.0D, new DoubleTranscoder());
double d = db.adddouble("dou_d", 4.0D);
System.out.println(" d =: " + d);
System.out.println("dou_d =: "
+ db.get("dou_d", new DoubleTranscoder()));

} catch (Exception e) {
e.printStackTrace();
} finally {
// close the connection
db.close();
}
}
}
[/java]
运行日志如下:

db put my_firstname successful.
db put my_lastname successful.
db put my_blogurl successful.
db put my_weibo successful.
test_blogurl =: www.micmiu.com
test_noexit =: null
===== test repeat put
test_desc =: hello world
test_desc =: repeat put value is hello Michael
===== access all key
my_firstname =: Sun
my_lastname =: Michael
my_blogurl =: www.micmiu.com
my_weibo =: www.sina.com/ctosun
test_desc =: repeat put value is hello Michael
===== test int double
i =: 7
int_i =: 7
d =: 7.0
dou_d =: 7.0

2.MRDB :用于多数据源,可复制、可靠性高、响应快等特点

演示代码:MRDBExample.java

[java]
package com.micmiu.nosql.ttserver;

import tokyotyrant.MRDB;
import tokyotyrant.networking.NodeAddress;

/**
*
* blog http://www.micmiu.com
*
* @author Michael
*
*/
public class MRDBExample {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {

MRDB db = null;
try {
db = new MRDB();
// connect to the server
db.open(NodeAddress.addresses("tcp://192.168.126.134:1978"));
Object value;
// store records
if (db.await(db.put("my_firstname", "Sun"))) {
System.out.println("MRDB put my_firstname successful.");
} else {
System.out.println("MRDB put my_firstname error.");
}

if (db.await(db.put("my_lastname", "Michael"))) {
System.out.println("MRDB put my_lastname successful.");
} else {
System.out.println("MRDB put my_lastname error.");
}

if (db.await(db.put("my_blogurl", "www.micmiu.com"))) {
System.out.println("MRDB put my_blogurl successful.");
} else {
System.out.println("MRDB put my_blogurl error.");
}

if (db.await(db.put("my_weibo", "www.sina.com/ctosun"))) {
System.out.println("MRDB put my_weibo successful.");
} else {
System.out.println("MRDB put my_weibo error.");
}

// retrieve records
value = db.await(db.get("my_blogurl"));
System.out.println("test_blogurl =: " + value);

value = db.await(db.get("test_noexit"));
System.out.println("test_noexit =: " + value);

System.out.println("===== test repeat put ");
db.put("test_desc", "hello world");
System.out.println("test_desc =: " + db.await(db.get("test_desc")));
db.put("test_desc", "repeat put value is hello Michael");
System.out.println("test_desc =: " + db.await(db.get("test_desc")));

// add int
db.put("int_i", 4);
// add double
db.put("dou_d", 8.8D);

// Initialize the iterator
System.out.println("===== access all key ");
Object[] keys = db
.await(db.fwmkeys("", db.size().get().intValue()));
for (Object keyObj : keys) {
System.out.println(keyObj + " =: " + db.await(db.get(keyObj)));
}

} catch (Exception e) {
e.printStackTrace();
} finally {
// close the connection
db.close();
}
}
}
[/java]
运行结果:

[16:42:42] INFO [tokyotyrant.networking.nio.NioNode] – Connect tcp://192.168.126.134:1978
MRDB put my_firstname successful.
MRDB put my_lastname successful.
MRDB put my_blogurl successful.
MRDB put my_weibo successful.
test_blogurl =: www.micmiu.com
test_noexit =: null
===== test repeat put
test_desc =: hello world
test_desc =: repeat put value is hello Michael
===== access all key
my_firstname =: Sun
my_lastname =: Michael
my_blogurl =: www.micmiu.com
my_weibo =: www.sina.com/ctosun
int_i =: 4
test_desc =: repeat put value is hello Michael
dou_d =: 8.8
[16:42:42] INFO [tokyotyrant.networking.nio.NioNode] – Disconnect tcp://192.168.126.134:1978
[16:42:42] INFO [tokyotyrant.networking.nio.NioNetworking] – Stopped. So will not handle IO. 0 keys will be ignored
————————

转自:http://www.micmiu.com/nosql/tokyotyrant-java-client/

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注


*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>