13 InfluxDB-python API

本文介绍了如何在Python中通过conda安装influxdb-client模块,步骤包括配置额外的镜像源并演示了创建Python项目、编写写入数据及执行Flux查询的示例。不建议直接使用Python操作InfluxDB,但提供了必要的安装指导。
摘要由CSDN通过智能技术生成

 

安装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。 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荒先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值