official website
Quickly experience TDengine through Docker | TDengine Documentation | Taosi Data
docker installation
Pull the latest docker image
docker pull tdengine/tdengine:latest
then execute
docker run -d -p 6030:6030 -p 6041:6041 -p 6043-6049:6043-6049 -p 6043-6049:6043-6049/udp tdengine/tdengine
Check if the container is started docker ps
Enter the container docker exec -it <container name> bash
docker view
Create library, table operation
Install the client driver taosc
After the installation is complete, start cmd;
create database
The above statement will create a library named power, the data in this library will be kept for 365 days (more than 365 days will be automatically deleted), one data file every 10 days, and the size of each VNode’s write memory pool is 16 MB. Write WAL to the database but do not execute FSYNC.
switch database
Change the library operated in the current connection to power, otherwise, you need to use "library name. table name" to specify the name of the library before operating on a specific table.
Create a super table:
CREATE STABLE meters (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);
When creating a super table, you need to provide the table name (meters in the example), the table structure Schema, that is, the definition of data columns. The first column must be the timestamp (ts in the example), and the other columns are the collected physical quantities (current, voltage, phase in the example), and the data type can be integer, floating point, string, etc. In addition, you also need to provide the Schema of the label (location, groupId in the example), and the data type of the label can be integer, floating point, string, etc. Static attributes of collection points can often be used as labels, such as the geographic location of the collection point, device model, device group ID, administrator ID, and so on. The Schema of the label can be added, deleted, and modified afterwards
create table
TDengine needs to build a table independently for each data collection point. Like a standard relational database, a table has a table name, Schema, but in addition, it can also have one or more labels. When creating, you need to use the super table as a template and specify the specific value of the label. Taking the smart meter in Table 1 as an example, you can use the following SQL command to create a table:
CREATE TABLE d1001 USING meters TAGS ("California.SanFrancisco", 2);
Among them, d1001 is the table name, meters is the table name of the super table, followed by the specific label value of the label Location "California.SanFrancisco", and the specific label value of the label groupId is 2. Although the label value needs to be specified when creating the table, it can be modified afterwards. For detailed rules, please refer to the table management chapter of TDengine SQL .
TDengine suggests to use the globally unique ID of the data collection point as the table name (such as the device serial number). But for some scenarios, there is no unique ID, and multiple IDs can be combined into a unique ID. Unique IDs are not recommended as tag values.
Automatic table
In some special scenarios, the user is not sure whether the table of a certain data collection point exists when writing data. At this time, the automatic table creation syntax can be used to create a non-existing table when writing data. If the table already exists A new table will not be created and subsequent USING statements are ignored. for example:
INSERT INTO d1001 USING meters TAGS ("California.SanFrancisco", 2) VALUES (NOW, 10.2, 219, 0.32);
The above SQL statement inserts records (NOW, 10.2, 219, 0.32)
into table d1001. If the table d1001 has not been created yet, it will be automatically created using the super table meters as a template, and the label value will be marked at the same time "California.SanFrancisco", 2
.
Multi-column model vs single-column model
TDengine supports multi-column models. As long as the physical quantities are collected at one data collection point at the same time (time stamps are consistent), these quantities can be placed in a super table as different columns. But there is also an extreme design, a single-column model, where each collected physical quantity is created separately, so each type of physical quantity is separately created with a super table. For example, for current, voltage, and phase, three super tables are built.
TDengine recommends adopting the multi-column model as much as possible because of higher insertion efficiency and storage efficiency. However, for some scenarios, the type of collection volume of a collection point changes frequently. At this time, if a multi-column model is used, the structure definition of the super table needs to be frequently modified, making the application more complicated. At this time, it is more convenient to use a single-column model. Simple.
Operate on the data table
insert
INSERT INTO d1001 VALUES (NOW, 10.2, 219, 0.32);
Inquire
super table sql
view super table
show stables;
delete super table
drop stable power.power_device;
View super table structure
DESCRIBE power.power_device;
create super table
CREATE STABLE if not exists power.power_device (ts timestamp,remark BINARY(500),name nchar(100), gridVoltage BINARY(100), powerGeneration BINARY(100),powerGenerationTotal BINARY(100),temperature BINARY(100),errCode BINARY(500)) TAGS (location binary(64),grounpdId int);
Create subtables from supertables
CREATE TABLE [subtable name] USING [super table name] TAGS[super table tags field]
CREATE TABLE power.device_1 USING power.power_device TAGS("CQ.YB",4);
view table
show tables;
Query by time
SELECT * FROM power.device_0123456 WHERE ts >= '2023-05-15 15:23:05' AND ts <= '2023-05-16 15:23:05';