首页 > 编程语言 >tokyotyrant-java客户端

tokyotyrant-java客户端

时间:2023-07-24 20:01:13浏览次数:50  
标签:java db System tokyotyrant put println out my 客户端

目录:


概述

演示

[一]、概述


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/ ]



标签:java,db,System,tokyotyrant,put,println,out,my,客户端
From: https://blog.51cto.com/u_16203469/6838533

相关文章

  • 为什么有一些什么方法都没有的接口会存在?比如java.lang.Cloneable
    /***Aclassimplementsthe<code>Cloneable</code>interfaceto*indicatetothe{@linkjava.lang.Object#clone()}methodthatit*islegalforthatmethodtomakea*field-for-fieldcopyofinstancesofthatclass.*<p>*Invo......
  • Java类加载
    Java类加载类加载过程加载:JVM将class文件加载到内存中,并生成class对象连接(验证、准备、解析):JVM校验class对象的规范性等初始化:JVM执行class对象中的静态代码块的语句和对静态变量的赋值操作使用:创建实例化对象、调用方法等卸载:类加载机制类加载源码protectedClass<?>lo......
  • java 中保留一位小数有以下几种方法
    使用String.format()方法,它可以按照指定的格式化字符串来输出数值,比如“%.1f”表示保留一位小数。例如:doublenum=3.14159;Stringresult=String.format("%.1f",num);//result="3.1"复制使用DecimalFormat类,它可以按照指定的模式来格式化数值,比如“#.#......
  • java并发编程(二)---上下文切换
    一、什么是上下文切换?  即使是单核处理器也支持多线程执行代码,CPU通过给每个线程分配CPU时间片来实现这个机制。时间片是CPU分配给各个线程的时间,线程会在分配的时间片内执行任务,因为时间片非常短,所以CPU通过不停地切换线程执行,让我们感觉多个线程是同时执行的,时间片一般是几......
  • Java Commons-Collections链分析
    CC1调用链代码执行的关键点InvokerTransformer.java transform(Objectinput) Classcls=input.getClass();Methodmethod=cls.getMethod(iMethodName,iParamTypes);returnmethod.invoke(input,iArgs);主要靠transform方法用反射的方式执行命令input......
  • JavaScript数据结构和算法简述——数组
    为什么先讲数组数据结构可以简单的被分为线性结构和非线性结构。线性结构大致包括:数组(连续存储);链表(离散存储);栈(线性结构常见应用,由链表或数组增删和改进功能实现);队列(线性结构常见应用,由链表或数组增删和改进功能实现);非线性结构大致包括:树;图;其中,数组是应用最广泛的数据存储结构。它被......
  • JavaScript 需要清楚的10件事
    文/谢传贵  在学习JavaScript的过程中,最需要搞清楚的10件事是什么?关于这个问题有人在Quora上给出了的答案。其中提到了一些很有代表性的知识点(坑),但描述比较杂乱。下面我将在他的基础上进行重新编排和解释。希望对你学习JavaScript有些帮助(为避免文章跑题,以下内容先不考虑ES5......
  • 基于Java的日程管理系统开发
    完整资料进入【数字空间】查看——baidu搜索"writebug"摘要日程管理在日常生活中是十分普通的一件事情,人们无论在生活中还是工作中都会有大大小小、各种各样的事情安排,如果仅仅靠纸张或者自己记录这些事情,往往会遗忘。针对这样的痛点,本文提供了日程管理系统开发的一整套流程,从需求......
  • javascript基本数据类型与值类型引用类型说明
    DEMO:http://sources.ikeepstudying.com/jsdata/ 摘要:本文主要讲了javascript中的基本数据类型,以及值类型和引用类型的区别与使用一、基本数据类型在javascript中申明变量使用的关键字都是var,这点与其他的编程语言不尽相同,但是javascript亦含有五种基本的数据类型(也可以说是简......
  • 记录java中zookeeper客户端ZkClient一个小坑
    项目中使用org.I0Itec.zkclient.ZkClient库作为zookeeper的连接工具,一直很稳定。不过有个奇怪的问题,从ZooInspetor中连接Zookeeper,看NodeData,会发现数据像图中这样,字符串前面有一段奇怪的字符:一直不清楚"t%"这些像乱码一样的字符是什么东西。因为项目运行的比较稳定,也没报错,就......