{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5b285400-a3a1-410e-9e20-6df4f5e2429e",
   "metadata": {},
   "source": [
    "# Influxdb\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8101cd24-d68a-422c-a81a-6c7c81fa08c1",
   "metadata": {},
   "source": [
    "Influx DB ist eine Zeitserien Datenbank.\n",
    "Zum Schreiben und lesen von Daten in der Datenbank gibt es 2 verschiedene Schnitstellen. Eine SQL ähnliche Kommandosprache InfluxQl und eine funktionale Sprache Flux\n",
    "\n",
    "Der Influxdb containr ist über http://localhost:8086 erreichbar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c60534da",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ee8de7a5-722f-4b03-ab00-8aeea2f1a596",
   "metadata": {},
   "source": [
    "## Daten in InfluxDB eintragen\n",
    "Als erstes den Client initialisieren"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "de26a902-5d7d-4db6-a62d-9a6287c60df4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "from influxdb_client import InfluxDBClient, Point, WritePrecision\n",
    "from influxdb_client.client.write_api import SYNCHRONOUS\n",
    "\n",
    "# You can generate an API token from the \"API Tokens Tab\" in the UI\n",
    "token = \"wb4s191jc33JQ4a6wK3ZECwrrG3LuSyQd61akFa_q6ZCEsequUvFhL9Gre6FaZMA2ElCylKz26ByJ6RetkQaGQ==\"\n",
    "org = \"test-org\"\n",
    "bucket = \"dwd\"\n",
    "url = \"http://influxdb:8086\"\n",
    "\n",
    "with InfluxDBClient(url=\"http://influxdb:8086\", token=token, org=org) as client:\n",
    "    write_api = client.write_api(write_options=SYNCHRONOUS)\n",
    "    data = \"mem,host=host1 used_percent=23.43234543\"\n",
    "    write_api.write(bucket, org, data)\n",
    "client.close()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28ebb2bd-2efe-49b7-a4e9-2ad8dd48ce79",
   "metadata": {},
   "source": [
    "Daten schreiben"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b2a761f7-9541-4935-9ccd-e467214e9ea5",
   "metadata": {},
   "outputs": [],
   "source": [
    "client = InfluxDBClient(url=url, token=token, org=org)\n",
    "\n",
    "\n",
    "write_api = client.write_api(write_options=SYNCHRONOUS)\n",
    "query_api = client.query_api()\n",
    "\n",
    "a = datetime(2017, 11, 28, 23, 55, 59)\n",
    "\n",
    "p = Point(\"my_measurement\").tag(\"location\", \"Prague\").field(\"temperature\", 25.3).time(a,WritePrecision.S)\n",
    "\n",
    "write_api.write(bucket=bucket, record=p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00dffb4c-2f82-440c-9ed0-cbd441751031",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f513259d-3bfb-46df-9beb-d1c7dcb5a875",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
