目录:
- 概述
- 演示
[一]、概述
java实现了对ttserver服务端的连接和访问。相关的源代码和jar包可以到其官网下载。
官网地址:http://code.google.com/p/tokyotyrant-java/
如果是maven构建项目的,在pom.xml 的<dependencies>节点中增加如下依赖配置即可:
2 |
< groupId >tokyotyrant</ groupId >
|
3 |
< artifactId >tokyotyrant</ artifactId >
|
4 |
< version >0.11</ version >
|
7 |
< groupId >org.jboss.netty</ groupId >
|
8 |
< artifactId >netty</ artifactId >
|
9 |
< version >3.1.5.GA</ version >
|
12 |
< groupId >org.slf4j</ groupId >
|
13 |
< artifactId >slf4j-api</ artifactId >
|
14 |
< version >1.5.6</ version >
|
17 |
< groupId >org.slf4j</ groupId >
|
18 |
< artifactId >slf4j-log4j12</ artifactId >
|
19 |
< version >1.5.6</ version >
|
20 |
< scope >runtime</ scope >
|
21 |
< optional >true</ optional >
|
[二]、演示
1.RDB :官方Tokyo Tyrant API的实现
演示代码:RDBExample.java
1 |
package com.micmiu.nosql.ttserver;
|
3 |
import java.io.IOException;
|
4 |
import java.net.InetSocketAddress;
|
6 |
import tokyotyrant.RDB;
|
7 |
import tokyotyrant.transcoder.DoubleTranscoder;
|
8 |
import tokyotyrant.transcoder.IntegerTranscoder;
|
17 |
public class RDBExample {
|
19 |
public static void main(String[] args) throws IOException {
|
25 |
db.open( new InetSocketAddress( "192.168.126.134" , 1978 ));
|
30 |
if (db.put( "my_firstname" , "Sun" )) {
|
31 |
System.out.println( "db put my_firstname successful." );
|
33 |
System.out.println( "db put my_firstname error." );
|
36 |
if (db.put( "my_lastname" , "Michael" )) {
|
37 |
System.out.println( "db put my_lastname successful." );
|
39 |
System.out.println( "db put my_lastname error." );
|
42 |
if (db.put( "my_blogurl" , "www.micmiu.com" )) {
|
43 |
System.out.println( "db put my_blogurl successful." );
|
45 |
System.out.println( "db put my_blogurl error." );
|
48 |
if (db.put( "my_weibo" , "www.sina.com/ctosun" )) {
|
49 |
System.out.println( "db put my_weibo successful." );
|
51 |
System.out.println( "db put my_weibo error." );
|
55 |
value = db.get( "my_blogurl" );
|
56 |
System.out.println( "test_blogurl =: " + value);
|
58 |
value = db.get( "test_noexit" );
|
59 |
System.out.println( "test_noexit =: " + value);
|
61 |
System.out.println( "===== test repeat put " );
|
62 |
db.put( "test_desc" , "hello world" );
|
63 |
System.out.println( "test_desc =: " + db.get( "test_desc" ));
|
64 |
db.put( "test_desc" , "repeat put value is hello Michael" );
|
65 |
System.out.println( "test_desc =: " + db.get( "test_desc" ));
|
68 |
System.out.println( "===== access all key " );
|
70 |
while ((key = db.iternext()) != null ) {
|
72 |
System.out.println(key + " =: " + value);
|
74 |
System.out.println( "===== test int double " );
|
76 |
db.put( "int_i" , 3 , new IntegerTranscoder());
|
77 |
int i = db.addint( "int_i" , 4 );
|
78 |
System.out.println( " i =: " + i);
|
79 |
System.out.println( "int_i =: "
|
80 |
+ db.get( "int_i" , new IntegerTranscoder()));
|
83 |
db.put( "dou_d" , 3 .0D, new DoubleTranscoder());
|
84 |
double d = db.adddouble( "dou_d" , 4 .0D);
|
85 |
System.out.println( " d =: " + d);
|
86 |
System.out.println( "dou_d =: "
|
87 |
+ db.get( "dou_d" , new DoubleTranscoder()));
|
89 |
} catch (Exception e) {
|
运行日志如下:
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
1 |
package com.micmiu.nosql.ttserver;
|
3 |
import tokyotyrant.MRDB;
|
4 |
import tokyotyrant.networking.NodeAddress;
|
13 |
public class MRDBExample {
|
19 |
public static void main(String[] args) throws Exception {
|
28 |
if (db.await(db.put( "my_firstname" , "Sun" ))) {
|
29 |
System.out.println( "MRDB put my_firstname successful." );
|
31 |
System.out.println( "MRDB put my_firstname error." );
|
34 |
if (db.await(db.put( "my_lastname" , "Michael" ))) {
|
35 |
System.out.println( "MRDB put my_lastname successful." );
|
37 |
System.out.println( "MRDB put my_lastname error." );
|
40 |
if (db.await(db.put( "my_blogurl" , "www.micmiu.com" ))) {
|
41 |
System.out.println( "MRDB put my_blogurl successful." );
|
43 |
System.out.println( "MRDB put my_blogurl error." );
|
46 |
if (db.await(db.put( "my_weibo" , "www.sina.com/ctosun" ))) {
|
47 |
System.out.println( "MRDB put my_weibo successful." );
|
49 |
System.out.println( "MRDB put my_weibo error." );
|
53 |
value = db.await(db.get( "my_blogurl" ));
|
54 |
System.out.println( "test_blogurl =: " + value);
|
56 |
value = db.await(db.get( "test_noexit" ));
|
57 |
System.out.println( "test_noexit =: " + value);
|
59 |
System.out.println( "===== test repeat put " );
|
60 |
db.put( "test_desc" , "hello world" );
|
61 |
System.out.println( "test_desc =: " + db.await(db.get( "test_desc" )));
|
62 |
db.put( "test_desc" , "repeat put value is hello Michael" );
|
63 |
System.out.println( "test_desc =: " + db.await(db.get( "test_desc" )));
|
68 |
db.put( "dou_d" , 8 .8D);
|
71 |
System.out.println( "===== access all key " );
|
73 |
.await(db.fwmkeys( "" , db.size().get().intValue()));
|
74 |
for (Object keyObj : keys) {
|
75 |
System.out.println(keyObj + " =: " + db.await(db.get(keyObj)));
|
78 |
} catch (Exception e) {
|
运行结果:
[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
————————
分享到:
相关推荐
tokyotyrant-1.1.40.tar.gz centos 7 x64系统应用 配合 tokyocabinet-1.4.45.tar
对数据库的使用方式进行了修改,使得开源软件能够支持分布式的访问。
tokyocabinet-1.4.45.tar.gz tokyotyrant-1.1.41.tar.gz tokyotyrant-0.11.jar ch-tokyocabinet-java-1.24.0.jar
媲美memcached的缓存服务器软件包,tokyocabinet-1.4.45.tar.gz+tokyotyrant-1.1.40.tar.gz
目前在网上关于TokyoCabinet(以下简称TC)和TokyoTyrant(以下简称TT)的资料已相对丰富了,但在.NET平台上的客户端软件却相对匮乏,因为做Discuz!NT企业版的关系,两个月前开始接触TC和TT,开始写相关的客户端代码...
Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符串。这里没有数据类型和数据表的概念。 当做为Hash表数据库使用时,...
NULL 博文链接:https://alvin871111.iteye.com/blog/680677
一个小型快速数据库的安装使用及原理介绍。
tokyotyrant(东京暴君) Redis (Redis) 还有一些非持久化存储,主要用于测试目的: locmem 内存缓存 store和parameters因一个后端而异。 有关更多详细信息,请参阅每个后端实现中包含的文档。 您可以以类似于...
《nosql数据库入门 中文版》详细地介绍了nosql数据库(非关系型数据库)的种类、用途以及使用方法,并对memcached、tokyotyrant、redis、mongodb这4种代表性的nosql数据库的特征、适用范围、实现代码进行了深入探讨,...
日本开发的nosql数据库,据说吞吐量很牛逼,最近开始研究这玩意的内部实现