`

tokyotyrant-java客户端

 
阅读更多

目录:

  1. 概述
  2. 演示

[一]、概述

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

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

如果是maven构建项目的,在pom.xml 的<dependencies>节点中增加如下依赖配置即可:

1 <dependency>
2             <groupId>tokyotyrant</groupId>
3             <artifactId>tokyotyrant</artifactId>
4             <version>0.11</version>
5         </dependency>
6         <dependency>
7             <groupId>org.jboss.netty</groupId>
8             <artifactId>netty</artifactId>
9             <version>3.1.5.GA</version>
10         </dependency>
11         <dependency>
12             <groupId>org.slf4j</groupId>
13             <artifactId>slf4j-api</artifactId>
14             <version>1.5.6</version>
15         </dependency>
16         <dependency>
17             <groupId>org.slf4j</groupId>
18             <artifactId>slf4j-log4j12</artifactId>
19             <version>1.5.6</version>
20             <scope>runtime</scope>
21             <optional>true</optional>
22         </dependency>

[二]、演示

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

演示代码:RDBExample.java

1 package com.micmiu.nosql.ttserver;
2  
3 import java.io.IOException;
4 import java.net.InetSocketAddress;
5  
6 import tokyotyrant.RDB;
7 import tokyotyrant.transcoder.DoubleTranscoder;
8 import tokyotyrant.transcoder.IntegerTranscoder;
9  
10 /**
11  *
12  * blog http://www.micmiu.com
13  *
14  * @author Michael
15  *
16  */
17 public class RDBExample {
18  
19     public static void main(String[] args) throws IOException {
20  
21         RDB db = new RDB();
22         try {
23             // connect to the server
24             // db.open(new NodeAddress("tcp://192.168.126.134:1978"));
25             db.open(new InetSocketAddress("192.168.126.134"1978));
26  
27             Object key;
28             Object value;
29             // store records
30             if (db.put("my_firstname""Sun")) {
31                 System.out.println("db put my_firstname successful.");
32             else {
33                 System.out.println("db put my_firstname error.");
34             }
35  
36             if (db.put("my_lastname""Michael")) {
37                 System.out.println("db put my_lastname successful.");
38             else {
39                 System.out.println("db put my_lastname error.");
40             }
41  
42             if (db.put("my_blogurl""www.micmiu.com")) {
43                 System.out.println("db put my_blogurl successful.");
44             else {
45                 System.out.println("db put my_blogurl error.");
46             }
47  
48             if (db.put("my_weibo""www.sina.com/ctosun")) {
49                 System.out.println("db put my_weibo successful.");
50             else {
51                 System.out.println("db put my_weibo error.");
52             }
53  
54             // retrieve records
55             value = db.get("my_blogurl");
56             System.out.println("test_blogurl =: " + value);
57  
58             value = db.get("test_noexit");
59             System.out.println("test_noexit =: " + value);
60  
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"));
66  
67             // Initialize the iterator
68             System.out.println("===== access all key ");
69             db.iterinit();
70             while ((key = db.iternext()) != null) {
71                 value = db.get(key);
72                 System.out.println(key + " =: " + value);
73             }
74             System.out.println("===== test int double ");
75             // add int
76             db.put("int_i"3new 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()));
81  
82             // add double
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()));
88  
89         catch (Exception e) {
90             e.printStackTrace();
91         finally {
92             // close the connection
93             db.close();
94         }
95     }
96 }

运行日志如下:

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;
2  
3 import tokyotyrant.MRDB;
4 import tokyotyrant.networking.NodeAddress;
5  
6 /**
7  *
8  * blog http://www.micmiu.com
9  *
10  * @author Michael
11  *
12  */
13 public class MRDBExample {
14  
15     /**
16      * @param args
17      * @throws Exception
18      */
19     public static void main(String[] args) throws Exception {
20  
21         MRDB db = null;
22         try {
23             db = new MRDB();
24             // connect to the server
25             db.open(NodeAddress.addresses("tcp://192.168.126.134:1978"));
26             Object value;
27             // store records
28             if (db.await(db.put("my_firstname""Sun"))) {
29                 System.out.println("MRDB put my_firstname successful.");
30             else {
31                 System.out.println("MRDB put my_firstname error.");
32             }
33  
34             if (db.await(db.put("my_lastname""Michael"))) {
35                 System.out.println("MRDB put my_lastname successful.");
36             else {
37                 System.out.println("MRDB put my_lastname error.");
38             }
39  
40             if (db.await(db.put("my_blogurl""www.micmiu.com"))) {
41                 System.out.println("MRDB put my_blogurl successful.");
42             else {
43                 System.out.println("MRDB put my_blogurl error.");
44             }
45  
46             if (db.await(db.put("my_weibo""www.sina.com/ctosun"))) {
47                 System.out.println("MRDB put my_weibo successful.");
48             else {
49                 System.out.println("MRDB put my_weibo error.");
50             }
51  
52             // retrieve records
53             value = db.await(db.get("my_blogurl"));
54             System.out.println("test_blogurl =: " + value);
55  
56             value = db.await(db.get("test_noexit"));
57             System.out.println("test_noexit =: " + value);
58  
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")));
64  
65             // add int
66             db.put("int_i"4);
67             // add double
68             db.put("dou_d"8.8D);
69  
70             // Initialize the iterator
71             System.out.println("===== access all key ");
72             Object[] keys = db
73                     .await(db.fwmkeys("", db.size().get().intValue()));
74             for (Object keyObj : keys) {
75                 System.out.println(keyObj + " =: " + db.await(db.get(keyObj)));
76             }
77  
78         catch (Exception e) {
79             e.printStackTrace();
80         finally {
81             // close the connection
82             db.close();
83         }
84     }
85 }

运行结果:

[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

————————

原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴http://www.micmiu.com/ ]

本文链接地址: http://www.micmiu.com/nosql/tokyotyrant-java-client/

分享到:
评论

相关推荐

    tokyotyrant-1.1.40.tar.gz

    然后,解压tokyotyrant-1.1.40.tar.gz文件,进入解压后的目录,运行配置脚本,指定TokyoCabinet的安装路径,编译并安装。安装完成后,启动TokyoTyrant服务器,通常通过`ttserver`命令。 4. **使用TokyoTyrant**:...

    tokyotyrant

    媲美memcached的缓存服务器软件包,tokyocabinet-1.4.45.tar.gz+tokyotyrant-1.1.40.tar.gz

    tokyotyrant/tokyocabinet/gpac

    标题 "tokyotyrant/tokyocabinet/gpac" 提及了三个关键组件:TokyoTyrant、TokyoCabinet 和 GPAC。这三者都是在IT领域中有着特定用途的工具,尤其是在数据存储和多媒体处理方面。下面将详细阐述这三个组件的知识点。...

    ttserver-lib包

    tokyocabinet-1.4.45.tar.gz tokyotyrant-1.1.41.tar.gz tokyotyrant-0.11.jar ch-tokyocabinet-java-1.24.0.jar

    发布一个基于TokyoTyrant的C#客户端开源项目

    首先,描述中提到的开源项目是针对.NET平台的一个C#客户端,旨在弥补.NET环境中TokyoTyrant客户端的不足。开发者在两个月前开始接触TokyoCabinet(简称TC)和TokyoTyrant(简称TT),并编写了相应的客户端代码,这个...

    Tokyocabinet-Tokyotyrant文档大合集

    Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符串。这里没有数据类型和数据表的概念。 当做为Hash表数据库使用时,...

    tokyocabinet安装配置总结(Ubuntu)

    tar xvf tokyotyrant-1.1.54.tar.gz # 进入源代码目录并编译安装 cd tokyotyrant-1.1.54 ./configure make sudo make install ``` 8. **Tokyotyrant配置**: 创建一个配置文件,例如`/etc/tokyotyrant....

    tokyoCabint及tokyoTyrant简介

    标题 "tokyoCabinet及tokyoTyrant简介" 指向了两个与数据库管理相关的开源工具,Tokyo Cabinet和Tokyo Tyrant。这两个工具由日本开发者开发,主要用于小型到中型的数据存储,尤其适合那些对数据读写速度有较高要求的...

    Java-concurrentMap-内存模型深入分析-HotCode

    在Java编程领域,`concurrentMap`是并发编程中至关重要的一部分,它提供了线程安全的映射操作。本文将深入探讨`concurrentMap`在Java内存模型(JMM,Java Memory Model)中的实现原理,以及如何通过HotCode优化并发...

    ttserver

    在 "tokyotyrant-1.1.24" 这个压缩包中,我们预期会找到 Tokyo Tyrant 的源代码、文档、示例程序和测试用例。Tokyo Tyrant 提供了丰富的API,使得开发人员可以轻松地在各种编程语言(如C、Python、Ruby等)中与之...

    TTserver1.1.4

    从给出的信息来看,它可能是Tokyo Tyrant的一个特定版本,因为压缩包中的文件名为"tokyotyrant-1.1.41"。Tokyo Tyrant是一个轻量级、高性能的键值存储系统,常用于数据缓存和快速查找应用。 Tokyo Tyrant主要知识点...

    tokyo tyrant文档

    工作线程组负责处理来自客户端的请求,每个线程都有自己的任务队列,这样可以并行处理不同的请求,提高响应速度。 ### 7. 性能对比测试 Tokyo Tyrant与MemcacheDB、Redis等其他流行数据库进行了性能对比测试。在...

    memcached全面剖析.pdf

    - TokyoTyrant是一款由mixi工程师开发的兼容memcached协议的键值存储系统,具有更高的性能和稳定性。 - TokyoTyrant可以在某些场景下作为memcached的替代品。 综上所述,memcached作为一种强大的缓存解决方案,在...

    Memcached分布式缓存

    常见的客户端有PHP、Python、Java等语言提供的库。 **1.5 使用Cache::Memcached** - **使用Cache::Memcached连接memcached**:Perl中的Cache::Memcached模块提供了与memcached服务器交互的方法。 - **保存数据**:...

    MemCached 全面剖析 memcached.pdf(中文)

    - **TokyoTyrant 案例**:TokyoTyrant 是一个兼容 MemCached 协议的高性能键值存储系统。 以上内容覆盖了 MemCached 的基本概念、安装使用、内存管理、删除机制、分布式算法以及实际应用场景等方面的知识点,为深入...

    mencache学习手册中文版.pdf

    - **TokyoTyrant案例**:介绍了一个兼容memcached的数据库系统TokyoTyrant,其提供了类似的功能和服务。 - **应用经验**:分享了实际应用memcached的经验教训,如通过daemontools启动服务、监控性能等。 通过以上...

Global site tag (gtag.js) - Google Analytics