目录:
概述
演示
[一]、概述
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", 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()));
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/ ]