一个数据采集点一张表:对每个数据采集点单独建表(比如有一千万个智能电表,就需创建一千万张表,上述表格中的d1001, d1002, d1003, d1004都需单独建表)
---- 创建库(这个库的数据将保留 365 天(超过 365 天将被自动删除),每 10 天一个数据文件,内存块数为 6,允许更新数据)
CREATE DATABASE power KEEP 365 DAYS 10 BLOCKS 6 UPDATE 1;
---- 创建超级表
CREATE STABLE meters (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);
---- 以超级表为模板创建数据表
create table test using meter tags('California.SanFrancisco', 2);
---- 单独创建表
create table t (ts timestamp, speed int);
---- 一次插入多条数据
INSERT INTO d1001 VALUES (1538548684000, 10.2, 220, 0.23) (1538548696650, 10.3, 218, 0.25);
---- 一次写入多表
INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31) (1538548695000, 12.6, 218, 0.33) d1002 VALUES (1538548696800, 12.3, 221, 0.31);
---- 插入记录时自动创建表
INSERT INTO d21001 USING meters TAGS ('California.SanFrancisco', 2) VALUES ('2021-07-13 14:06:32.272', 10.2, 219, 0.32);
insert into device01 using device_category tags('shanghai',11) values ('2022-08-04 05:20:00','集控器','as0001',1, 35.1)
-- 关联超级表的时候只需要最开始的初始化表的时候using stable,后续写入数据不需要带using:
insert into device01 values (now(),'ZigBee窗帘','as0006',1, 35.1);
---- 分页查询
select * from device01 limit 10 offset 2;
超级表可以直接查询数据:
select * from test.meters limit 10;
超级表
采集点的静态属性往往可以作为标签,比如采集点的地理位置、设备型号、设备组 ID、管理员 ID 等等。标签的 schema 可以事后增加、删除、修改。具体定义以及细节请见 TAOS SQL 的超级表管理 章节
更新数据:
TDengine 在数据落盘时会打开新的预写日志文件,在落盘后则会删除老的预写日志文件,避免日志文件无限制的增长
select avg(degree) from t1 interval(5);
select avg(degree) from t_master where location='shanghai' interval(5);
注:超级表不可导入
sudo cp /mnt/e/file/tdengine/taosTools-2.0.0-Linux-x64.tar.gz /usr/local/td
sudo tar -zxvf taosTools-2.0.0-Linux-x64.tar.gz
sudo ./install-taostools.sh
mkdir -p /tmp/dump/db0
mkdir -p /tmp/dump/stb1
mkdir -p /tmp/dump/tb1
tasodump -h td2.server.com -u root -ptaosdata -o /tmp/dump/db0 -T 8 rain
tasodump -h td2.server.com -u root -ptaosdata -o /tmp/dump/stb1 -T 8 rain master_test
tasodump -h td2.server.com -u root -ptaosdata -o /tmp/dump/tb1 -T 8 rain test1
tasodump -h td2.server.com -u root -ptaosdata -i /tmp/dump/db0 -T 8
tasodump -h td2.server.com -u root -ptaosdata -i /tmp/dump/stb1 -T 8
tasodump -h td2.server.com -u root -ptaosdata -i /tmp/dump/tb1 -T 8
select * from test1 >> data.csv
insert into test1 file 'usr/local/td/file/data.csv'
taos> source "/usr/local/taos/data.sql";
sql文件类似:
create database if not exists rain;
USE rain;
create stable if not exists power(ts timestamp, voltage int, current float, temperature float) tags(sn int, city nchar(64), groupid int);
create table if not exists device1 using power tags(1, "太原", 1);
create table if not exists device2 using power tags(2, "西安", 2);
insert into device1 values("2022-09-04 21:03:38.734", 1, 1.0, 1.0);
insert into device2 values("2022-09-04 21:03:40.734", 2, 2.0, 2.0);
cd DataX
mvn -U clean package assembly:assembly -Dmaven.test.skip=true
cp target/datax.tar.gz your_install_dir
cd your_install_dir
tar -zxvf dataX.tar.gz
{
"job": {
"content": [{
"reader": {
"name": "mysqlreader",
"parameter": {
"username": "root",
"password": "root",
"column": ["id","name"],
"splitPk": "id",
"connection": [{
"table": ["t_employee"],
"jdbcUrl": ["jdbc:mysql://192.168.15.152:3306/rain"]
}]
}
},
"writer": {
"name": "tdenginewriter",
"parameter": {
"host": "192.168.31.56",
"port": 6030,
"dbname": "rain",
"user": "root",
"password": "taosdata",
"batchSize": 1000
}
}
}],
"setting": {
"speed": {
"channel": 1
}
}
}
}
python bin/datax.py job/opentsdb2tdengine.json
下载安装包:TDengine-server-3.0.0.1-Linux-x64.tar.gz (54.4 M)
解压:
sudo tar -zxvf TDengine-server-3.0.0.1-Linux-x64.tar.gz
sudo ./install.sh
配置taos.cfg文件
启动
sudo taosd (Ubuntu安装)
rmtaos
rmtaostools
sudo dpkg -r tdengine
sudo rm -rf /var/log/taos/
sudo rm -rf /var/lib/taos/
sudo rm -rf /etc/taos/taos.cfg