安装influxdb-client模块:
首先需要配置类似镜像的东西,否则模块安装不了:
(base) nkimaintainer@nku09-PowerEdge-R740:~$ conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/ (base) nkimaintainer@nku09-PowerEdge-R740:~$ conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/free/ (base) nkimaintainer@nku09-PowerEdge-R740:~$ conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ (base) nkimaintainer@nku09-PowerEdge-R740:~$ conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ (base) nkimaintainer@nku09-PowerEdge-R740:~$ conda config --set show_channel_urls yes (base) nkimaintainer@nku09-PowerEdge-R740:~$
执行上述命令后即可安装:
conda install influxdb-client
换下以下命令进行安装才能成功。
创建Python项目:
拷贝代码:
python代码如下:
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
# Initialize the Client
from datetime import datetime
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
# You can generate an API token from the "API Tokens Tab" in the UI
token = "9DQmVnWbmIRTKmrb-tmLGya9eU7oV1Srd1scykEocmuKndu_IAxaLQU3tBdn-j33U7_x5h4KMJ-EruNCRYCrDQ=="
org = "ghy"
bucket = "test"
with InfluxDBClient(url="http://124.222.138.109:8086", token=token, org=org) as client:
# Write Data
write_api = client.write_api(write_options=SYNCHRONOUS)
data = "mem_python,host=host1 used_percent=23.43234543"
write_api.write(bucket, org, data)
# Execute a Flux query
# query = 'from(bucket: "test") |> range(start: -1h)'
# tables = client.query_api().query(query, org=org)
# for table in tables:
# for record in table.records:
# print(record)
client.close()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
运行成功:
个人不推荐python操作influxdb。