influxdb3常用指令
·
- influxdb3 浏览器登录需要单独的后台,在influxdb官网可以下载。
- python按照influxdb3客户端:pip install influxdb3-python
注意事项
- influxdb3的time支持纳秒级别的时间戳,因此time、datetime的时间戳需要乘以1e9;
- field字段如果是float,插入数据时必须带有小数点后面部分,比如:.field(“open”, 22)会报错,必须是.field(“open”, 22.0);
启动服务端:
influxdb3 serve --node-id host01 --object-store file --data-dir .\influxdb3_data --http-bind 0.0.0.0:8181
首次启动需要创建token,否则无法使用数据库,脚本如下:
influxdb3 create token --admin
关闭服务
先通过ps -ef | grep influxdb命令找到 InfluxDB 3 的进程 ID,然后使用kill命令来终止进程。为了确保数据安全,建议使用SIGTERM信号(默认信号)来关闭进程:
kill -15 <influxdb进程ID>
创建数据库:
./influxdb3 create database --host http://localhost:8181 dean_trader --token apiv3_***
./influxdb3 show databases --host http://localhost:8181 --token apiv3_***
删除数据库:
./influxdb3 delete database demo_database01 --token apiv3_***
连接数据库
注意host必须带有“http://”
with InfluxDBClient3(
host=f"http://localhost:8181",
database=f"demo_database01",
token=f"apiv3_SZ5bj7AH1kqips0I7LIx00j6758SpoB_hCB_L63wsyh0FSv3Z4dLy0BXoFEJFDNSmamnRNf1oQU4JkpRqgvpew",
write_client_options=wco,
) as client:
插入数据
ts = int(time.time() * 1000000000)
print(ts)
point = Point("table_ohcl").tag("vtsymbol", "btcusdt").tag("exchange", "binance").time(ts).field("open", 22.0).field("high", 31.0).field("low", 20.0).field("close", 22.0)
# Write the point
client.write(point)
print(status)
查询数据
qry = f"""select * from table_ohcl where vtsymbol='btcusdt' and time > '2024-09-15T00:00:00Z'"""
reader = client.query(query=qry, language='sql')
1h=1小时,支持m(分钟)、d(天)、w(周)等
qry = f"""select * from table_ohcl where vtsymbol='btcusdt' and time > now() - INTERVAL '9' HOUR"""
print(qry)
reader = client.query(query=qry, language='sql')
新增一列
new_column = pa.array([True, False, True], type=pa.bool_())
new_table = table.append_column("is_student", new_column)
更多推荐




所有评论(0)