新建一个maven项目
1 导入依赖
<dependency>
<groupId>com.taobao.tair</groupId>
<artifactId>tair-client</artifactId>
<version>2.3.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.59</version>
</dependency>
2 编写测试代码
package com.example.demo;
import com.alibaba.fastjson.JSON;
import com.taobao.tair.DataEntry;
import com.taobao.tair.Result;
import com.taobao.tair.ResultCode;
import com.taobao.tair.impl.DefaultTairManager;
import java.util.ArrayList;
import java.util.List;
public class TestTair {
public static void main(String[] args) {
List<String> configList = new ArrayList<String>();
configList.add("192.168.222.154:5198");//ConfigServer的ip和端口
DefaultTairManager tairManager = new DefaultTairManager();
tairManager.setConfigServerList(configList);
tairManager.setTimeout(5000);
tairManager.setCharset("utf-8");
tairManager.setGroupName("group_test");//和配置中的组名称保持一致
tairManager.init();
//put数据
ResultCode putResult = tairManager.put(1,"name","tairtest");
System.out.println("putResult:"+putResult);
System.out.println("putResult:"+ JSON.toJSONString(putResult));
//get数据
Result<DataEntry> getResult = tairManager.get(1, "name");
System.out.println("getResult:"+getResult);
System.out.println("getResult:"+JSON.toJSONString(putResult));
DataEntry dataEntry = getResult.getValue();
System.out.println(JSON.toJSONString(dataEntry));
//删除数据
ResultCode delResult = tairManager.delete(1,"name");
System.out.println("delResult:"+JSON.toJSONString(delResult));
//获取数据
Result<DataEntry> getResult2 = tairManager.get(1,"name");
System.out.println("getResult:"+getResult2);
}
}