时序数据库-8-[timescaleDB]在centos7中的安装部署
1 简介
许多应用程序(例如监视系统和数据收集系统)会累积数据以进行进一步分析。 这些分析通常着眼于数据或系统随时间变化的方式。 在这些情况下,数据表示为时间序列,每个数据点都带有时间戳。
TimescaleDB是一个开放源数据库,已针对存储时间序列数据进行了优化。 它实现为PostgreSQL的扩展,并结合了关系数据库的易用性和NoSQL数据库的速度。 结果,它允许您使用PostgreSQL在一个地方存储业务数据和时间序列数据。
2安装timescaleDB
yum Installation
This will install both TimescaleDB and PostgreSQL via yum.
Note: TimescaleDB requires PostgreSQL 11 or 12.
(1) download the correct PGDG from PostgreSQL for your operating system and architecture
# yum install -y https://download.postgresql.org/pub/repos/yum/12/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
(2)CentOS默认软件包存储库中没有TimescaleDB,因此在这一步中,您将从TimescaleDB的第三方存储库中安装它。
#vi /etc/yum.repos.d/timescaledb.repo
[timescale_timescaledb]
name=timescale_timescaledb
baseurl=https://packagecloud.io/timescale/timescaledb/el/7/\$basearch
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/timescale/timescaledb/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
#yum install -y timescaledb-postgresql-12
To get started you’ll need to restart PostgreSQL and add a postgres superuser (used in the rest of the docs). Please refer to your distribution for how to restart services.
(3)初始化pg数据库
#/usr/pgsql-12/bin/postgresql-12-setup initdb
(4)自启动和启动pg数据库
#systemctl enable postgresql-12
#systemctl enable postgresql-12
(5)修改postgres用户密码

3 配置
TimescaleDB模块可以与默认的PostgreSQL配置设置一起正常工作,但是为了提高性能并更好地利用处理器,内存和磁盘资源,TimescaleDB的开发人员建议配置一些单独的参数。 这可以使用timescaledb-tune工具自动完成,也可以通过手动编辑服务器的postgresql.conf文件来完成。
使用timescaledb-tune工具 ,它读取postgresql.conf文件并以交互方式建议进行更改。
(1)运行以下命令以启动配置向导
#timescaledb-tune --pg-config=/usr/pgsql-12/bin/pg_config
(2)要求您确认PostgreSQL配置文件的路径:
该实用程序会自动检测到配置文件的路径,因此请输入y确认
(3)在下一个提示符下键入y并按ENTER来启用TimescaleDB模块
(4)根据服务器的特性和PostgreSQL版本,将提示您调整设置。 按y开始调整过程:
(5)如果您的服务器有多个CPU,则会发现有关并行性设置的建议。 但是,如果您有一个CPU, timescaledb-tune会将您直接发送到WAL设置。
(6)有关预写日志(WAL)的建议
WAL保留数据完整性,但是默认设置会导致低效率的I / O,从而降低写入性能。
(7)其他参数
所有这些不同的参数都旨在提高性能。 例如,SSD可以处理许多并发请求,因此, effective_io_concurrency的最佳值可能是数百。
(8)注意
如果您要从头开始安装,还可以使用–quiet和–yes标志运行初始命令,这将自动应用所有建议并会对postgresql.conf配置文件进行更改:
#timescaledb-tune --pg-config=/usr/pgsql-12/bin/pg_config --quiet --yes
(9)为了使配置更改生效,必须重新启动PostgreSQL服务:
#systemctl restart postgresql-12
4 创建数据库和超表
优化TimescaleDB设置后,您就可以使用时间序列数据了。 TimescaleDB被实现为PostgreSQL扩展,因此使用时间序列数据进行的操作与关系数据操作并没有太大区别。 同时,该数据库允许您将来自由组合时间序列和关系表中的数据。
#su postgres
bash-4.2$ psql
(1)创建数据库
postgres=# create database tutorial;
(2)启用TimescaleDB扩展
postgres=# \c tutorial;首先连接到数据库
postgres-# create extension if not exists timescaledb cascade;

(3)创建超表
与时间序列数据进行交互的主要点是超表,它是保存数据的许多单独表的抽象,称为表块 。
要创建超表,请从常规SQL表开始,然后通过函数create_hypertable将其转换为超create_hypertable 。
制作一个表,该表将存储数据以跟踪一段时间内各个设备的温度和湿度:
tutorial=# create table conditions(
time TIMESTAMP WITH TIME ZONE NOT NULL,
device_id TEXT,
temperature NUMERIC,
humidity NUMERIC);
此命令将创建一个称为conditions的表,该表有四列。 第一列将存储时间戳,其中包括时区,不能为空。 接下来,您将使用时间列将表转换为按时间分区的超表:
tutorial=# SELECT create_hypertable('conditions','time');
5 写入和删除数据
(1)使用以下命令为理论设备weather-pro-000000插入一些样本temperature和humidity数据:
tutorial=# INSERT INTO conditions(time, device_id, temperature, humidity) VALUES (NOW(), 'weather-pro-000000', 84.1, 84.1);
(2)可以一次插入多行数据
tutorial=# INSERT INTO conditions values
(NOW(),'weather-pro-000002',71.0,51.0),
(NOW(),'weather-pro-000003',70.5,50.5),
(NOW(),'weather-pro-000004',70.0,50.2);
(3)查询数据

(4)删除数据
tutorial=# DELETE FROM conditions WHERE temperature > 80;
删除操作之后,建议使用VACUUM命令,该命令将回收已被删除的数据仍在使用的空间。
tutorial=# VACUUM conditions;
6 从文件读入数据
这些命令适用于小规模数据输入,但是由于时间序列数据通常会同时从多个设备生成庞大的数据集,因此了解如何一次插入数百或数千行也很重要。 如果您以结构化形式(例如csv格式)从外部源准备了数据,则可以快速完成此任务。
使用一个样本数据集,该数据集表示来自各个位置的温度和湿度数据。 它是由TimescaleDB开发人员创建的,可让您测试他们的数据库。
(1)下载数据
#curl https://timescaledata.blob.core.windows.net/datasets/weather_small.tar.gz -o weather_small.tar.gz
#tar -xzvf weather_small.tar.gz
#mv weather_small_conditions.csv /tmp
#cd /tmp/
#sudo -u postgres psql -d tutorial -c "\COPY conditions FROM weather_small_conditions.csv CSV"
显示COPY 1000000
7 查询数据
(1)登录数据库
#sudo -u postgres psql -d tutorial
(2)查看最新条目,按时间降序对数据数组进行排序
tutorial=# SELECT * FROM conditions ORDER BY time DESC LIMIT 5;

(3)查看来自weather-pro-000000设备的条目
tutorial=# SELECT * FROM conditions where device_id =‘weather-pro-000000’ ORDER BY time DESC LIMIT 5;

(4) 查找温度值的中值,可以将以下查询与percentile_cont函数一起使用
tutorial=# select percentile_cont(0.5) within group(order by temperature) from conditions WHERE device_id = ‘weather-pro-000000’;
输出40.5。
将看到weather-pro-00000传感器所在的整个观察期的中间温度。
(5)显示每个传感器的最新值,可以使用last函数
tutorial=# select device_id,last(temperature,time) from conditions group by device_id;

要获取第一个值,请使用第first函数。
(6)显示最近24小时内所选传感器的每小时平均,最低和最高温度。
tutorial=# select time_bucket(‘1 hour’,time) “hour”,trunc(avg(temperature),2) avg_temp,trunc(min(temperature),2) min_temp,trunc(max(temperature),2) max_temp from conditions where device_id=‘weather-pro-000000’ group by “hour” order by “hour” desc limit 24;

将看到温度在一天中的哪个时段升高或降低
更多推荐


所有评论(0)