1.搭建环境

首先根据官方给出的内容,先做下图红框内的两个操作
0.png
为了更接近于真实的环境,这里我们来整合一下SpringBoot、MyBatisPlus与TDengine

TDengine给我们提供了TAOS-JDBCDriver用来整合,我们经过官网查看得到了一些对应关系
1.png
然后我们查看一下我们安装的是哪个版本
2.png
我们这里安装的是2.1.0版本,所以我们的JDBCDriver版本可以选择2.0.22~2.0.30之间的任意一个

首先我们用IDEA创建一个SpringBoot项目,然后导入所需的依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!--MyBatisPlus-->
  7. <dependency>
  8. <groupId>com.baomidou</groupId>
  9. <artifactId>mybatis-plus-boot-starter</artifactId>
  10. <version>3.1.2</version>
  11. </dependency>
  12. <!--Druid连接池-->
  13. <dependency>
  14. <groupId>com.alibaba</groupId>
  15. <artifactId>druid-spring-boot-starter</artifactId>
  16. <version>1.1.17</version>
  17. </dependency>
  18. <!--TAOSJDBCDriver-->
  19. <dependency>
  20. <groupId>com.taosdata.jdbc</groupId>
  21. <artifactId>taos-jdbcdriver</artifactId>
  22. <version>2.0.30</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-devtools</artifactId>
  27. <scope>runtime</scope>
  28. <optional>true</optional>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.projectlombok</groupId>
  32. <artifactId>lombok</artifactId>
  33. <optional>true</optional>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-test</artifactId>
  38. <scope>test</scope>
  39. </dependency>
  40. <dependency>
  41. <groupId>junit</groupId>
  42. <artifactId>junit</artifactId>
  43. <version>4.13.1</version>
  44. <scope>test</scope>
  45. </dependency>
  46. </dependencies>

我们这里模拟一个温度传感器的场景来进行演示,我们创建我们需要的entity与mapper

entity:

  1. @Data
  2. public class Temperature {
  3. private Timestamp ts;
  4. private float temperature;
  5. private String location;
  6. private int index;
  7. }

mapper:

  1. @Mapper
  2. public interface TemperatureMapper extends BaseMapper<Temperature> {
  3. @Update("CREATE STABLE if not exists temperature (ts timestamp, temperature float) TAGS (location nchar(64), index int)")
  4. int createSuperTable();
  5. @Update("create table #{tbName} using temperature tags( #{location}, #{index})")
  6. int createTable(@Param("tbName") String tbName,@Param("location") String location,@Param("index") int index);
  7. @Update("drop table if exists temperature")
  8. void dropSuperTable();
  9. @Insert("insert into tb${index}(ts, temperature) values(#{ts}, #{temperature})")
  10. int insertOne(Temperature one);
  11. }

Test:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest
  3. public class TemperatureMapperTest{
  4. @Autowired
  5. private TemperatureMapper mapper;
  6. private static Random random = new Random();
  7. private static String[] locations = {"北京", "上海", "深圳", "广州", "杭州"};
  8. @Before
  9. public void before() {
  10. mapper.dropSuperTable();
  11. mapper.createSuperTable();
  12. for (int i = 0; i < 10; i++) {
  13. mapper.createTable("tb" + i, locations[random.nextInt(locations.length)], i);
  14. }
  15. int affectRows = 0;
  16. long ts = System.currentTimeMillis();
  17. for (int i = 0; i < 10; i++) {
  18. for (int j = 0; j < 5; j++) {
  19. Temperature one = new Temperature();
  20. one.setTs(new Timestamp(new Date().getTime()));
  21. one.setTemperature(new Random().nextInt(40));
  22. one.setLocation("望京");
  23. one.setIndex(i);
  24. affectRows += mapper.insertOne(one);
  25. }
  26. }
  27. Assert.assertEquals(50, affectRows);
  28. }
  29. @After
  30. public void after() {
  31. mapper.dropSuperTable();
  32. }
  33. @Test
  34. public void testSelectList() {
  35. List<Temperature> temperatureList = mapper.selectList(null);
  36. temperatureList.forEach(System.out::println);
  37. }
  38. @Test
  39. public void testInsert() {
  40. Temperature one = new Temperature();
  41. one.setTs(new Timestamp(new Date().getTime()));
  42. one.setTemperature(new Random().nextInt(40));
  43. one.setLocation("望京");
  44. int affectRows = mapper.insertOne(one);
  45. Assert.assertEquals(1, affectRows);
  46. }
  47. @Test
  48. public void testSelectByMap() {
  49. Map<String, Object> map = new HashMap<>();
  50. map.put("location", "北京");
  51. List<Temperature> temperatures = mapper.selectByMap(map);
  52. System.out.println(temperatures);
  53. }
  54. @Test
  55. public void testSelectPage() {
  56. IPage page = new Page(1, 2);
  57. IPage<Temperature> temperatureIPage = mapper.selectPage(page, null);
  58. System.out.println("total : " + temperatureIPage.getTotal());
  59. System.out.println("pages : " + temperatureIPage.getPages());
  60. for (Temperature temperature : temperatureIPage.getRecords()) {
  61. System.out.println(temperature);
  62. }
  63. }
  64. }

2.遇到的问题

(一)no taos in java.library.path
问题1.png
因为我是在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
问题2.png
解决方法:除了开启6030的TCP端口外,还需要开放6030的UDP端口,因为我的TDengine是用docker安装,所以要修改启动命令,开放端口出来

  1. docker run -d --name tdengine -p 6030-6041:6030-6041 -p 6030:6030/udp tdengine/tdengine:latest

因为链接使用的是FQDN链接,我也配置了hostname
问题3.png
但是还是链接失败,应该是Docker网络配置的原因,因为我对Docker也不是很精通,这里暂未解决,所以我们直接在服务器上装一个接着测,具体安装看第一篇,然后开放端口即可

  1. firewall-cmd --add-port=6030-6041/tcp --permanent
  2. firewall-cmd --add-port=6030/udp --permanent
  3. firewall-cmd --reload

Reference

因为代码和教程我也是参考其他人的,这里贴作者的就好