目录:
- 概述
- 演示
[一]、概述
java实现了对ttserver服务端的连接和访问。相关的源代码和jar包可以到其官网下载。
官网地址:http://code.google.com/p/tokyotyrant-java/
如果是maven构建项目的,在pom.xml 的<dependencies>节点中增加如下依赖配置即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<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> |
[二]、演示
1.RDB :官方Tokyo Tyrant API的实现
演示代码:RDBExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
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(); } } } |
运行日志如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
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(); } } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[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/
0 条评论。