1.搭建环境
首先根据官方给出的内容,先做下图红框内的两个操作
为了更接近于真实的环境,这里我们来整合一下SpringBoot、MyBatisPlus与TDengine
TDengine给我们提供了TAOS-JDBCDriver用来整合,我们经过官网查看得到了一些对应关系
然后我们查看一下我们安装的是哪个版本
我们这里安装的是2.1.0版本,所以我们的JDBCDriver版本可以选择2.0.22~2.0.30之间的任意一个
首先我们用IDEA创建一个SpringBoot项目,然后导入所需的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--MyBatisPlus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<!--Druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<!--TAOSJDBCDriver-->
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>2.0.30</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
我们这里模拟一个温度传感器的场景来进行演示,我们创建我们需要的entity与mapper
entity:
@Data
public class Temperature {
private Timestamp ts;
private float temperature;
private String location;
private int index;
}
mapper:
@Mapper
public interface TemperatureMapper extends BaseMapper<Temperature> {
@Update("CREATE STABLE if not exists temperature (ts timestamp, temperature float) TAGS (location nchar(64), index int)")
int createSuperTable();
@Update("create table #{tbName} using temperature tags( #{location}, #{index})")
int createTable(@Param("tbName") String tbName,@Param("location") String location,@Param("index") int index);
@Update("drop table if exists temperature")
void dropSuperTable();
@Insert("insert into tb${index}(ts, temperature) values(#{ts}, #{temperature})")
int insertOne(Temperature one);
}
Test:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class TemperatureMapperTest{
@Autowired
private TemperatureMapper mapper;
private static Random random = new Random();
private static String[] locations = {"北京", "上海", "深圳", "广州", "杭州"};
@Before
public void before() {
mapper.dropSuperTable();
mapper.createSuperTable();
for (int i = 0; i < 10; i++) {
mapper.createTable("tb" + i, locations[random.nextInt(locations.length)], i);
}
int affectRows = 0;
long ts = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 5; j++) {
Temperature one = new Temperature();
one.setTs(new Timestamp(new Date().getTime()));
one.setTemperature(new Random().nextInt(40));
one.setLocation("望京");
one.setIndex(i);
affectRows += mapper.insertOne(one);
}
}
Assert.assertEquals(50, affectRows);
}
@After
public void after() {
mapper.dropSuperTable();
}
@Test
public void testSelectList() {
List<Temperature> temperatureList = mapper.selectList(null);
temperatureList.forEach(System.out::println);
}
@Test
public void testInsert() {
Temperature one = new Temperature();
one.setTs(new Timestamp(new Date().getTime()));
one.setTemperature(new Random().nextInt(40));
one.setLocation("望京");
int affectRows = mapper.insertOne(one);
Assert.assertEquals(1, affectRows);
}
@Test
public void testSelectByMap() {
Map<String, Object> map = new HashMap<>();
map.put("location", "北京");
List<Temperature> temperatures = mapper.selectByMap(map);
System.out.println(temperatures);
}
@Test
public void testSelectPage() {
IPage page = new Page(1, 2);
IPage<Temperature> temperatureIPage = mapper.selectPage(page, null);
System.out.println("total : " + temperatureIPage.getTotal());
System.out.println("pages : " + temperatureIPage.getPages());
for (Temperature temperature : temperatureIPage.getRecords()) {
System.out.println(temperature);
}
}
}
2.遇到的问题
(一)no taos in java.library.path
因为我是在windows操作系统下链接,所以需要在本地安装一个windows的TDengine连接器,下载地址:
https://www.taosdata.com/assets-download/TDengine-client-2.1.0.0-Windows-x64.exe (根据你们的版本进行修改版本号后再下载)
(二)JNI ERROR (2354): JNI connection is NULL
解决方法:除了开启6030的TCP端口外,还需要开放6030的UDP端口,因为我的TDengine是用docker安装,所以要修改启动命令,开放端口出来
docker run -d --name tdengine -p 6030-6041:6030-6041 -p 6030:6030/udp tdengine/tdengine:latest
因为链接使用的是FQDN链接,我也配置了hostname
但是还是链接失败,应该是Docker网络配置的原因,因为我对Docker也不是很精通,这里暂未解决,所以我们直接在服务器上装一个接着测,具体安装看第一篇,然后开放端口即可
firewall-cmd --add-port=6030-6041/tcp --permanent
firewall-cmd --add-port=6030/udp --permanent
firewall-cmd --reload
Reference
因为代码和教程我也是参考其他人的,这里贴作者的就好