新建一个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);}
}