TDengine 和 InfluxDB 都是时序数据库(Time Series Database)。前者是新兴的黑马,后者是老牌的龙头,这两个哪个更易用,上手更快呢?
下面从最基础的数据写入和查询方面比较一下,由于只对 Python 比较熟悉,就从 Python 出发,通过两者提供的示例代码进行一下对比。
1. InfluxDB
官网文档地址:https://docs.influxdata.com/influxdb/v2.2/api-guide/client-libraries/python/
1.1. 安装连接器
pip install influxdb-client
1.2. 复制示例代码
InfluxDB 提供的代码里面没有提供创建 org 和 bucket 的操作, 相关操作可以参考 InfluxDB 客户端基础操作
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
bucket = "db01"
org = "test"
token = "xxxxxxxxxxxxxxx"
url="http://192.168.0.111:8086"
client = influxdb_client.InfluxDBClient(
url=url,
token=token,
org=org
)
write_api = client.write_api(write_options=SYNCHRONOUS)
p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
write_api.write(bucket=bucket, org=org, record=p)
query_api = client.query_api()
query = ' from(bucket:"db01")\
|> range(start: -10m)\
|> filter(fn:(r) => r._measurement == "my_measurement")\
|> filter(fn: (r) => r.location == "Prague")\
|> filter(fn:(r) => r._field == "temperature" )'
result = query_api.query(org=org, query=query)
results = []
for table in result:
for record in table.records:
results.append((record.get_field(), record.get_value()))
print(results)
PS:直接复制官方的代码运行时报错:
File "influx.py", line 23
query = ` from(bucket:"my-bucket")\
^
SyntaxError: invalid syntax
需要将代码中的反引号修改为单引号才能成功。
1.3. 运行代码
root@cl1:~/influx
[('temperature', 25.3)]
2. TDengine
TDengine 的安装可参考 TDengine学习笔记-集群安装 官网文档:https://docs.taosdata.com/2.4/reference/connector/python
2.1. 安装连接器
pip3 install taospy
2.2. 复制示例代码
import taos
conn: taos.TaosConnection = taos.connect(host="192.168.0.11",
user="root",
password="taosdata",
port=6030,
config="/etc/taos",
timezone="Asia/Shanghai")
server_version = conn.server_info
print("server_version", server_version)
client_version = conn.client_info
print("client_version", client_version)
conn.execute("DROP DATABASE IF EXISTS test")
conn.execute("CREATE DATABASE test")
conn.select_db("test")
conn.execute("CREATE STABLE weather(ts TIMESTAMP, temperature FLOAT) TAGS (location INT)")
affected_row: int = conn.execute("INSERT INTO t1 USING weather TAGS(1) VALUES (now, 23.5) (now+1m, 23.5) (now+2m 24.4)")
print("affected_row", affected_row)
result: taos.TaosResult = conn.query("SELECT * from weather")
fields: taos.field.TaosFields = result.fields
for field in fields:
print(field)
data = result.fetch_all()
print(data)
PS:和 InfluxDB 一样,直接运行代码也会报错
connect failed, reason: Invalid database name.
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.9) or chardet (3.0.4) doesn't match a supported version!
warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
Traceback (most recent call last):
File "tdengine.py", line 3, in <module>
conn: taos.TaosConnection = taos.connect(host="192.168.0.11",
File "/usr/local/lib/python3.8/dist-packages/taos/__init__.py", line 63, in connect
return TaosConnection(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/taos/connection.py", line 24, in __init__
self._conn = self._chandle.connect(self._host, self._user, self._password, self._database, self._port)
File "/usr/local/lib/python3.8/dist-packages/taos/cinterface.py", line 993, in connect
return taos_connect(host, user, password, db, port)
File "/usr/local/lib/python3.8/dist-packages/taos/cinterface.py", line 213, in taos_connect
raise ConnectionError("connect to TDengine failed")
问题在于连接串里面指定了数据库 test,但是此时数据库还没有创建,因此就会报错。 将 database = “test” 注释即可运行。
2.3. 运行代码
root@cl1:~/tdengine
server_version 2.4.0.26
client_version 2.4.0.24
affected_row 3
{name: ts, type: 9, bytes: 8}
{name: temperature, type: 6, bytes: 4}
{name: location, type: 4, bytes: 4}
[(datetime.datetime(2022, 6, 15, 10, 45, 50, 787000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), 23.5, 1), (datetime.datetime(2022, 6, 15, 10, 46, 50, 787000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), 23.5, 1), (datetime.datetime(2022, 6, 15, 10, 47, 50, 787000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), 24.399999618530273, 1)]
3. 结论
首先,两个产品都提供了示例代码,能够实现简单的数据写入和查询。
其次,直接复制代码都出现了问题,区别在于 InfluxDB 的问题是简单的语法问题,可能是由于某些插件版本问题造成的,而 TDengine 就是逻辑上的问题了,如果对产品不熟悉,可能要花点时间才能定位到。
再次,个人感觉 InfluxDB 的查询语法还是不如 TDengine 的 SQL 看起来简单。
最后,如果只是从数据写入和查询的简单功能来看,还无法判断两者哪个更易上手些。
|