TDengine3.0 使用笔记(包括安装,踩坑等)

服务端安装

拉取镜像

docker pull tdengine/tdengine:latest

创建相关文件夹

mkdir -p /mydocker/tdengine/log /mydocker/tdengine/data

运行

docker run --restart=always --name tdengine -p 6030:6030 -p 6041:6041 -p 6043-6049:6043-6049 -p 6043-6049:6043-6049/udp -v /mydocker/tdengine/log:/var/log/taos -v /mydocker/tdengine/data:/var/lib/taos -d tdengine/tdengine

客户端安装

客户端和服务端版本必须一致,否则会出现时间不同步的问题(Client and server’s time is not synchronized)

客户端安装好后,firstEp和fqdn选择一个修改,测试都可以是ip或者域名,域名的话,记得配置本地hosts

firstEp                   gkrh_tdengine:6030

# The end point of the second dnode to be connected to if the firstEp is not available when `taosd` or `taos` is started
# secondEp

# The FQDN of the host where `taosd` will be started. It can be IP address
#fqdn                      gkrh_tdengine

host配置

192.168.2.25 gkrh_tdengine

整合springboot

  • 配置
    用rest api方式连接
spring:
  datasource:
    tdengine-server:
      jdbc-url: jdbc:TAOS-RS://host:6041/iot?user=root&password=taosdata&timezone=Asia/Shanghai
      username: root
      password: taosdata
@Configuration
@MapperScan(basePackages = {"com.rh.energy.platformbackend.mapper.tdengine"}, sqlSessionTemplateRef  = "tdengineSqlSessionTemplate")
public class TDengineServerConfig {

    @Resource
    private MybatisPlusInterceptor mybatisPlusInterceptor;


    @Bean(name = "tdengineDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.tdengine-server")
    public DataSource tdengineDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("tdengineMybatisConfiguration")
    @ConfigurationProperties(prefix = "mybatis-plus.configuration")
    public MybatisConfiguration getCfg() {
        return new MybatisConfiguration();
    }

    @Bean(name = "tdengineSqlSessionFactory")
    public SqlSessionFactory tdengineSqlSessionFactory(@Qualifier("tdengineDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setPlugins(mybatisPlusInterceptor);
        bean.setConfiguration(getCfg());
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/tdengine/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "tdengineTransactionManager")
    public DataSourceTransactionManager tdengineTransactionManager(@Qualifier("tdengineDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "tdengineSqlSessionTemplate")
    public SqlSessionTemplate tdengineSqlSessionTemplate(@Qualifier("tdengineSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

  • 后续就可以用mybatis或者mybatis plus 的方式进行操作tdengine数据库了,下边给一个xml的配置,这方面就不再过多讲了,我相信dddd
<mapper namespace="com.rh.energy.platformbackend.mapper.tdengine.IotMeterMapper">
    <resultMap id="BaseResultMap" type="com.rh.energy.platformbackend.entity.tdengine.IotMeter">
        <id column="ts" jdbcType="VARCHAR" property="ts" />
        <result column="current" jdbcType="FLOAT" property="current" />
        <result column="voltage" jdbcType="FLOAT" property="voltage" />
        <result column="phase" jdbcType="INTEGER" property="phase" />
        <result column="location" jdbcType="VARCHAR" property="location" />
        <result column="device_id" jdbcType="VARCHAR" property="deviceId" />
    </resultMap>
    <sql id="Base_Column_List">
        ts, current, voltage,phase,location,device_id
    </sql>
    <select id="select" resultType="com.rh.energy.platformbackend.entity.tdengine.IotMeter">
        select
        <include refid="Base_Column_List" />
        from iot_meter
        where device_id=#{deviceId}
        and ts &lt;= #{endTime} and ts &gt;= #{startTime}
        order by ts desc
    </select>
    <select id="getLatestByDeviceId" resultType="com.rh.energy.platformbackend.entity.tdengine.IotMeter">
        select
        <include refid="Base_Column_List" />
        from iot_meter
        where device_id=#{deviceId}
        order by ts desc
        limit 1

    </select>
    <insert id="insert" parameterType="com.rh.energy.platformbackend.entity.tdengine.IotMeter" >
        insert into #{deviceId} using iot_meter tags(#{location},#{deviceId}) values (#{ts}, #{current}, #{voltage}, #{phase})
    </insert>
</mapper>

使用技巧

加上\G垂直显示

show create stable iot_meter \G;
TDengine3.0 使用笔记(包括安装,踩坑等)_第1张图片

遇到的问题

客户端连接服务器的时候报 Client and server’s time is not synchronized

该问题可以先用ntpdate cn.pool.ntp.org命令同步下时间,如果还报该错误,检查下客户端和服务端的版本,必须保持一致,
我当时是客户端是3.0.0.1,服务端是3.0.1.0就会报该错误

当启动报错,找不到错误信息时,直接进入/usr/local/taos/bin,用taosd启动,能看到详细的错误原因

你可能感兴趣的:(个人沉淀,docker,tdengine,linux)