当前位置:网站首页>如何利用数仓创建时序表
如何利用数仓创建时序表
2022-06-23 18:38:00 【InfoQ】
创建第一张时序表
CREATE TABLE [ IF NOT EXISTS ] table_name
({ column_name data_type [ kv_type ]
| LIKE source_table [like_option [...] ] }
}
[, ... ])
[ WITH ( {storage_parameter = value} [, ... ] ) ]
[ TABLESPACE tablespace_name ]
[ DISTRIBUTE BY HASH ( column_name [,...])]
[ TO { GROUP groupname | NODE ( nodename [, ... ] ) } ]
[ table_partitioning_clauses ]
[ PARTITION BY {
{RANGE (partition_key) ( partition_less_than_item [, ... ] )}
} [ { ENABLE | DISABLE } ROW MOVEMENT ] ];
CREATE TABLE IF NOT EXISTS GENERATOR(
dynamo text TSTag,
manufacturer text TSTag,
model text TSTag,
location text TSTag,
ID bigint TSTag,
voltage numeric TSField,
power bigint TSTag,
frequency numeric TSField,
angle numeric TSField,
time timestamptz TSTime) with (orientation=TIMESERIES, period='7 days', ttl='1 month') distribute by hash(model);- 对于不随时间的变化而变化,描述发电机的属性信息的列(发电机信息、生产厂商、型号、位置、ID)被设置为tag列,在建表时需要将对应的列后面指定为TSTag;
- 对于采样数据的维度(电压、功率、频率、电流相角)这些对应的采样数值随时间的变化而变,我们将这些维度设置为field列,建表语句数据类型后面指定为TSField;
- 最后一列我们指定为时间列time,存储field列数据对应的时间信息,建表时将指定为TSTime。
时序表列类型
自动设置分区边界
- period:设置自动创建分区的间隔时间,默认值为1 day, 取值范围:1 hour ~ 100 years。默认会为时序表创建自增分区任务。自增分区任务动态为我们创建分区,保证当前时刻有足够充裕的分区用于导入数据。
- ttl:设置自动淘汰分区的时间,取值范围:1 hour ~ 100 years。默认不创建淘汰分区任务,需要用户自己在建表手动指定,或者建表后通过ALTER TABLE语法设置。淘汰分区的策略是通过计算 nowtime - 分区boundary > ttl,满足该条件的分区将被drop掉。帮助用户定时清理过期的旧数据。
- period设置为“小时” , 分区起始边界值为下个小时整点,分区的间隔为period的值
- period设置为“天” , 分区起始边界值为第二天零点,分区的间隔为period的值
- period设置为“月” , 分区起始边界值为下个月零点,分区的间隔为period的值
- period设置为“年” , 分区起始边界值为明年零点,分区的间隔为period的值
tsdb=# CREATE TABLE IF NOT EXISTS GENERATOR(
tsdb(# dynamo text TSTag,
tsdb(# manufacturer text TSTag,
tsdb(# model text TSTag,
tsdb(# location text TSTag,
tsdb(# ID bigint TSTag,
tsdb(# voltage numeric TSField,
tsdb(# power bigint TSTag,
tsdb(# frequency numeric TSField,
tsdb(# angle numeric TSField,
tsdb(# time timestamptz TSTime) with (orientation=TIMESERIES, period='1 hour', ttl='1 month') distribute by hash(model);
CREATE TABLE
tsdb=# select now();
now
-------------------------------
2022-05-25 15:28:38.520757+08
(1 row)
tsdb=# select relname, boundaries from pg_partition where parentid=(select oid from pg_class where relname='generator') order by boundaries ;
relname | boundaries
----------------+----------------------------
default_part_1 | {"2022-05-25 16:00:00+08"}
default_part_2 | {"2022-05-25 17:00:00+08"}
p1653505200 | {"2022-05-26 03:00:00+08"}
p1653541200 | {"2022-05-26 13:00:00+08"}
p1653577200 | {"2022-05-26 23:00:00+08"}
......
tsdb=# select now();
now
-------------------------------
2022-05-31 20:36:09.700096+08
(1 row)
tsdb=# CREATE TABLE IF NOT EXISTS GENERATOR(
tsdb(# dynamo text TSTag,
tsdb(# manufacturer text TSTag,
tsdb(# model text TSTag,
tsdb(# location text TSTag,
tsdb(# ID bigint TSTag,
tsdb(# voltage numeric TSField,
tsdb(# power bigint TSTag,
tsdb(# frequency numeric TSField,
tsdb(# angle numeric TSField,
tsdb(# time timestamptz TSTime) with (orientation=TIMESERIES, period='1 day') distribute by hash(model)
tsdb-# partition by range(time)
tsdb-# (
tsdb(# PARTITION P1 VALUES LESS THAN('2022-05-30 16:32:45'),
tsdb(# PARTITION P2 VALUES LESS THAN('2022-05-31 16:56:12')
tsdb(# );
WARNING: partition boundary is less than current time.
CREATE TABLE
tsdb=# select relname, boundaries from pg_partition where parentid=(select oid from pg_class where relname='generator') order by boundaries ;
relname | boundaries
-------------+----------------------------
p1 | {"2022-05-30 16:32:45+08"}
p2 | {"2022-05-31 16:56:12+08"}
p1654073772 | {"2022-06-01 16:56:12+08"}
p1654160172 | {"2022-06-02 16:56:12+08"}
......边栏推荐
- 【One by One系列】IdentityServer4(三)使用用户名和密码
- Hotline salon issue 26 - cloud security session
- Development notes of wedding studio applet based on wechat applet
- 硬件开发笔记(六): 硬件开发基本流程,制作一个USB转RS232的模块(五):创建USB封装库并关联原理图元器件
- Graffiti intelligence passed the hearing: Tencent is an important shareholder planning to return to Hong Kong for listing
- 墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库
- Comparison and evaluation of digicert and globalsign single domain ov SSL certificates
- Application de JDBC dans les essais de performance
- Advanced network planning notes (IX)
- Advanced network accounting notes (VIII)
猜你喜欢

How can enterprises do business monitoring well?

Online text entity extraction capability helps applications analyze massive text data

Définition de la fonction et paramètres de la fonction

Taolue biology rushes to the scientific innovation board: the actual controllers with annual losses of more than 100 million are Zhang Dawei and his wife, who are American nationals

混沌工程,了解一下

LeetCode 每日一题——30. 串联所有单词的子串

Application de JDBC dans les essais de performance

官宣.NET 7 预览版5

JDBC 在性能测试中的应用

Nanxin semiconductor rushes to the scientific innovation board: its annual revenue is RMB 980 million. Sequoia Xiaomi oppo is the shareholder
随机推荐
Advanced network accounting notes (V)
在线文本实体抽取能力,助力应用解析海量文本数据
Definition and model of indicators (complex indicators)
从零开发小程序和公众号【第一期】
【云动向】华为云云商店品牌全新发布 4大亮点都在这儿
[one by one series] identityserver4 (VIII) uses entityframework core to persist data
8、AI医生案例
宝安区航城街道领导一行莅临联诚发参观调研
深入理解和把握数字经济的基本特征
手把手写深度学习(15):在Hugging Face上构建自己的语料库
Requirements and precautions for applying for multi domain SSL certificate
盘点四种WiFi加密标准:WEP、WPA、WPA2、WPA3
CV convolution neural network
Netseer: stream event telemetry notes for programmable data plane
IDEA控制台显示中文乱码
How long do you need to prepare for the PMP Exam?
20set introduction and API
如何使用物联网低代码平台进行流程管理?
Develop small programs and official account from zero [phase I]
Pisces: a programmable, protocol independent software switch (summary)