{ "cells": [ { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "ename": "InfluxDBClientError", "evalue": "401: {\"code\":\"unauthorized\",\"message\":\"Unauthorized\"}", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mInfluxDBClientError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[6], line 6\u001b[0m\n\u001b[1;32m 3\u001b[0m client \u001b[39m=\u001b[39m InfluxDBClient(\u001b[39m'\u001b[39m\u001b[39m192.168.31.29\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m8086\u001b[39m, \u001b[39m'\u001b[39m\u001b[39madmin\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mqx123456\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[1;32m 5\u001b[0m \u001b[39m# 显示已存在的数据库\u001b[39;00m\n\u001b[0;32m----> 6\u001b[0m \u001b[39mprint\u001b[39m(client\u001b[39m.\u001b[39;49mget_list_database())\n", "File \u001b[0;32m/opt/module/anaconda2022-10/envs/py3916/lib/python3.9/site-packages/influxdb/client.py:704\u001b[0m, in \u001b[0;36mInfluxDBClient.get_list_database\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 690\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mget_list_database\u001b[39m(\u001b[39mself\u001b[39m):\n\u001b[1;32m 691\u001b[0m \u001b[39m\"\"\"Get the list of databases in InfluxDB.\u001b[39;00m\n\u001b[1;32m 692\u001b[0m \n\u001b[1;32m 693\u001b[0m \u001b[39m :returns: all databases in InfluxDB\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 702\u001b[0m \u001b[39m [{u'name': u'db1'}, {u'name': u'db2'}, {u'name': u'db3'}]\u001b[39;00m\n\u001b[1;32m 703\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 704\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mlist\u001b[39m(\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mquery(\u001b[39m\"\u001b[39;49m\u001b[39mSHOW DATABASES\u001b[39;49m\u001b[39m\"\u001b[39;49m)\u001b[39m.\u001b[39mget_points())\n", "File \u001b[0;32m/opt/module/anaconda2022-10/envs/py3916/lib/python3.9/site-packages/influxdb/client.py:521\u001b[0m, in \u001b[0;36mInfluxDBClient.query\u001b[0;34m(self, query, params, bind_params, epoch, expected_response_code, database, raise_errors, chunked, chunk_size, method)\u001b[0m\n\u001b[1;32m 518\u001b[0m \u001b[39mif\u001b[39;00m query\u001b[39m.\u001b[39mlower()\u001b[39m.\u001b[39mstartswith(\u001b[39m\"\u001b[39m\u001b[39mselect \u001b[39m\u001b[39m\"\u001b[39m) \u001b[39mand\u001b[39;00m \u001b[39m\"\u001b[39m\u001b[39m into \u001b[39m\u001b[39m\"\u001b[39m \u001b[39min\u001b[39;00m query\u001b[39m.\u001b[39mlower():\n\u001b[1;32m 519\u001b[0m method \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mPOST\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m--> 521\u001b[0m response \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mrequest(\n\u001b[1;32m 522\u001b[0m url\u001b[39m=\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39mquery\u001b[39;49m\u001b[39m\"\u001b[39;49m,\n\u001b[1;32m 523\u001b[0m method\u001b[39m=\u001b[39;49mmethod,\n\u001b[1;32m 524\u001b[0m params\u001b[39m=\u001b[39;49mparams,\n\u001b[1;32m 525\u001b[0m data\u001b[39m=\u001b[39;49m\u001b[39mNone\u001b[39;49;00m,\n\u001b[1;32m 526\u001b[0m stream\u001b[39m=\u001b[39;49mchunked,\n\u001b[1;32m 527\u001b[0m expected_response_code\u001b[39m=\u001b[39;49mexpected_response_code\n\u001b[1;32m 528\u001b[0m )\n\u001b[1;32m 530\u001b[0m data \u001b[39m=\u001b[39m response\u001b[39m.\u001b[39m_msgpack\n\u001b[1;32m 531\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m data:\n", "File \u001b[0;32m/opt/module/anaconda2022-10/envs/py3916/lib/python3.9/site-packages/influxdb/client.py:378\u001b[0m, in \u001b[0;36mInfluxDBClient.request\u001b[0;34m(self, url, method, params, data, stream, expected_response_code, headers)\u001b[0m\n\u001b[1;32m 376\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m 377\u001b[0m err_msg \u001b[39m=\u001b[39m reformat_error(response)\n\u001b[0;32m--> 378\u001b[0m \u001b[39mraise\u001b[39;00m InfluxDBClientError(err_msg, response\u001b[39m.\u001b[39mstatus_code)\n", "\u001b[0;31mInfluxDBClientError\u001b[0m: 401: {\"code\":\"unauthorized\",\"message\":\"Unauthorized\"}" ] } ], "source": [ "from influxdb import InfluxDBClient\n", "\n", "client = InfluxDBClient('192.168.31.29', 8086, 'admin', 'qx123456')\n", "\n", "# 显示已存在的数据库\n", "print(client.get_list_database())\n", "# # 创建数据库\n", "# client.create_database('py_db1')\n", "# print(client.get_list_database())\n", "# # 删除数据库\n", "# client.drop_database('py_db1')\n", "# print(client.get_list_database())\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "bucket=\"data\"\n", "\n", "write_api = client.write_api(write_options=SYNCHRONOUS)\n", " \n", "for value in range(5):\n", " point = (\n", " Point(\"measurement1\")\n", " .tag(\"tagname1\", \"tagvalue1\")\n", " .field(\"field1\", value)\n", " )\n", " write_api.write(bucket=bucket, org=\"zhili\", record=point)\n", " time.sleep(1) # separate points by 1 second" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "FluxRecord() table: 0, {'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 7, 9, 14, 40, 16, 231809, tzinfo=tzutc()), '_stop': datetime.datetime(2023, 7, 9, 14, 50, 16, 231809, tzinfo=tzutc()), '_time': datetime.datetime(2023, 7, 9, 14, 47, 23, 498266, tzinfo=tzutc()), '_value': 0, '_field': 'field1', '_measurement': 'measurement1', 'tagname1': 'tagvalue1'}\n", "FluxRecord() table: 0, {'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 7, 9, 14, 40, 16, 231809, tzinfo=tzutc()), '_stop': datetime.datetime(2023, 7, 9, 14, 50, 16, 231809, tzinfo=tzutc()), '_time': datetime.datetime(2023, 7, 9, 14, 47, 24, 510663, tzinfo=tzutc()), '_value': 1, '_field': 'field1', '_measurement': 'measurement1', 'tagname1': 'tagvalue1'}\n", "FluxRecord() table: 0, {'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 7, 9, 14, 40, 16, 231809, tzinfo=tzutc()), '_stop': datetime.datetime(2023, 7, 9, 14, 50, 16, 231809, tzinfo=tzutc()), '_time': datetime.datetime(2023, 7, 9, 14, 47, 25, 515181, tzinfo=tzutc()), '_value': 2, '_field': 'field1', '_measurement': 'measurement1', 'tagname1': 'tagvalue1'}\n", "FluxRecord() table: 0, {'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 7, 9, 14, 40, 16, 231809, tzinfo=tzutc()), '_stop': datetime.datetime(2023, 7, 9, 14, 50, 16, 231809, tzinfo=tzutc()), '_time': datetime.datetime(2023, 7, 9, 14, 47, 26, 520705, tzinfo=tzutc()), '_value': 3, '_field': 'field1', '_measurement': 'measurement1', 'tagname1': 'tagvalue1'}\n", "FluxRecord() table: 0, {'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 7, 9, 14, 40, 16, 231809, tzinfo=tzutc()), '_stop': datetime.datetime(2023, 7, 9, 14, 50, 16, 231809, tzinfo=tzutc()), '_time': datetime.datetime(2023, 7, 9, 14, 47, 27, 526577, tzinfo=tzutc()), '_value': 4, '_field': 'field1', '_measurement': 'measurement1', 'tagname1': 'tagvalue1'}\n" ] } ], "source": [ "query_api = client.query_api()\n", "\n", "query = \"\"\"from(bucket: \"data\")\n", " |> range(start: -10m)\n", " |> filter(fn: (r) => r._measurement == \"measurement1\")\"\"\"\n", "tables = query_api.query(query, org=\"zhili\")\n", "\n", "for table in tables:\n", " for record in table.records:\n", " print(record)" ] } ], "metadata": { "kernelspec": { "display_name": "py3916", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "8f4b0a32e8aea4331366ae2a004af1cd179962452cce6e31b3bd8ca311f93913" } } }, "nbformat": 4, "nbformat_minor": 2 }