Linux MacBook Docker安装IoTDB及使用
方式一: session方式访问IotDB (推荐使用,自带连接池)
maven依赖iotdb-session
org.apache.iotdb
iotdb-session
0.11.2
springboot IotDB配置信息session方式
spring:
iotdb:
username: root
password: root
ip: 192.168.0.5
port: 6667
maxSize: 100
IotDB-session配置类
package com.beyond.data.config;
import org.apache.iotdb.session.pool.SessionPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Configuration
public class IotDBSessionConfig {
private static final Logger log = LoggerFactory.getLogger(IotDBSessionConfig.class);
@Value("${spring.iotdb.username:root}")
private String username;
@Value("${spring.iotdb.password:root}")
private String password;
@Value("${spring.iotdb.ip:127.0.0.1}")
private String ip;
@Value("${spring.iotdb.port:6667}")
private int port;
@Value("${spring.iotdb.maxSize:10}")
private int maxSize;
private static SessionPool sessionPool;
private SessionPool getSessionPool() {
if (sessionPool == null) {
sessionPool = new SessionPool(ip, port, username, password, maxSize);
}
return sessionPool;
}
public void insertRecord(String deviceId, long time, List measurements, List values) {
getSessionPool();
try {
log.info("iotdb数据入库:device_id:[{}], measurements:[{}], values:[{}]", deviceId, measurements, values);
sessionPool.insertRecord(deviceId, time, measurements, values);
} catch (Exception e) {
log.error("IotDBSession insertRecord失败: deviceId={}, time={}, measurements={}, values={}, error={}",
deviceId, time, measurements, values, e.getMessage());
}
}
}
调用session方式
@Autowired
private IotDBSessionConfig iotDBSessionConfig;
......
StringBuffer tableName = new StringBuffer();
tableName.append("root").append(".").append("test").append("deviceid");
long currentTime = System.currentTimeMillis();
List iotMeasurements = new ArrayList<>();
iotMeasurements.add("aaa");
iotMeasurements.add("bbb");
List iotValues = new ArrayList<>();
iotValues.add("123");
iotValues.add("abide");
iotDBSessionConfig.insertRecord(tableName.toString(), currentTime, iotMeasurements, iotValues);
方式二: jdbc方式访问IotDB (自己实现连接池)
maven依赖iotdb-jdbc
org.apache.iotdb
iotdb-jdbc
0.11.2
com.alibaba
druid-spring-boot-starter
spring boot IotDB配置信息jdbc
spring:
iotdb:
username: root
password: root
driver-name: org.apache.iotdb.jdbc.IoTDBDriver
url: jdbc:iotdb://192.168.0.5:6667/
initial-size: 5
min-idle: 10
max-active: 50
max-wait: 60000
remove-abandoned: true
remove-abandoned-timeout: 30
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
test-while-idle: false
test-on-borrow: false
test-on-return: false
IotDB-jdbc配置类
package com.beyond.data.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@Configuration
public class IotDBConfig {
private static final Logger log = LoggerFactory.getLogger(IotDBConfig.class);
@Value("${spring.iotdb.username}")
private String username;
@Value("${spring.iotdb.password}")
private String password;
@Value("${spring.iotdb.driver-name}")
private String driverName;
@Value("${spring.iotdb.url}")
private String url;
@Value("${spring.iotdb.initial-size:20}")
private int initialSize;
@Value("${spring.iotdb.min-idle:10}")
private int minIdle;
@Value("${spring.iotdb.max-active:500}")
private int maxActive;
@Value("${spring.iotdb.max-wait:60000}")
private int maxWait;
@Value("${spring.iotdb.remove-abandoned:true}")
private boolean removeAbandoned;
@Value("${spring.iotdb.remove-abandoned-timeout:30}")
private int removeAbandonedTimeout;
@Value("${spring.iotdb.time-between-eviction-runs-millis:60000}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.iotdb.min-evictable-idle-time-millis:300000}")
private int minEvictableIdleTimeMillis;
@Value("${spring.iotdb.test-while-idle:false}")
private boolean testWhileIdle;
@Value("${spring.iotdb.test-on-borrow:false}")
private boolean testOnBorrow;
@Value("${spring.iotdb.test-on-return:false}")
private boolean testOnReturn;
private static DruidDataSource iotDbDataSource;
//使用阿里的druid连接池
private Connection getConnection() {
if (iotDbDataSource == null) {
iotDbDataSource = new DruidDataSource();
//设置连接参数
iotDbDataSource.setUrl(url);
iotDbDataSource.setDriverClassName(driverName);
iotDbDataSource.setUsername(username);
iotDbDataSource.setPassword(password);
//配置初始化大小、最小、最大
iotDbDataSource.setInitialSize(initialSize);
iotDbDataSource.setMinIdle(minIdle);
iotDbDataSource.setMaxActive(maxActive);
//配置获取连接等待超时的时间
iotDbDataSource.setMaxWait(maxWait);
//连接泄漏监测
iotDbDataSource.setRemoveAbandoned(removeAbandoned);
iotDbDataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
//配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
iotDbDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
iotDbDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
//防止过期
iotDbDataSource.setTestWhileIdle(testWhileIdle);
iotDbDataSource.setTestOnBorrow(testOnBorrow);
iotDbDataSource.setTestOnReturn(testOnReturn);
}
Connection connection = null;
try {
connection = iotDbDataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
log.error("iotDB getConnection失败: error={}", e.getMessage());
}
return connection;
}
public void insert(String sql) {
log.info("iotDB insert sql={}", sql);
Connection connection = getConnection();
Statement statement = null;
try {
if(connection!=null){
statement = connection.createStatement();
long systemTime = System.currentTimeMillis();
statement.execute(sql);
log.info("执行IotDb的sql----[{}],时间:[{}]ms", sql, System.currentTimeMillis()-systemTime);
}
} catch (SQLException e) {
log.error("iotDB insert失败: error={}", e.getMessage());
} finally {
close(statement, connection);
}
}
public List
调用jdbc方式
@Autowired
private IotDBConfig iotDBConfig;
......
StringBuffer tableName = new StringBuffer();
tableName.append("root").append(".").append("test").append("deviceid");
long currentTime = System.currentTimeMillis();
List iotMeasurements = new ArrayList<>();
iotMeasurements.add("aaa");
iotMeasurements.add("bbb");
List iotValues = new ArrayList<>();
iotValues.add("123");
iotValues.add("abde");
StringBuffer sql = new StringBuffer();
sql.append(" insert into ").append(tableName.toString());
sql.append("(timestamp,");
sql.append(String.join( ",", iotMeasurements)).append(")");
sql.append(" values(").append(currentTime).append(",");
sql.append(String.join(",", iotValues)).append(")");
iotDBConfig.insert(sql.toString());
//查询
StringBuffer querySql = new StringBuffer();
querySql.append(" select ").append("aaa");
querySql.append(" from ").append(tableName.toString());
querySql.append(" where ").append("bbb").append(" = '");
querySql.append("abde").append("'");
querySql.append(" order by time desc limit 1 ");
log.info("sql----{}", querySql);
List> resultList = iotDBConfig.query(querySql.toString());