Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. ioBroker Allgemein
    4. Frage : Migrate MySQL nach Influxdb

    NEWS

    • Monatsrückblick - April 2025

    • Minor js-controller 7.0.7 Update in latest repo

    • Save The Date: ioBroker@Smart Living Forum Solingen, 14.06.

    Frage : Migrate MySQL nach Influxdb

    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      MezzoDO @JackGruber last edited by MezzoDO

      @JackGruber said in Frage : Migrate MySQL nach Influxdb:

      @MezzoDO ok, das ack feld ist bereits als boolen angelegt, kommt nun aber als int.
      Ihc könnte es anpassen, dass dies beim import convertiert wird.

      Das wäre grandios 🙂
      Mein Script, was ich einsetze:

      ### MySQL DB info ###
      #import MySQLdb
      import pymysql
      conn = pymysql.connect(host="localhost",  # your host, usually localhost
                           user="xxx",         # your username
                           passwd="xxx",  # your password
                           db="iobroker")        # name of the data base
       
       
      ### PostgreSQL DB info ###
      import psycopg2
      import psycopg2.extras
       
      #####
      # connection data for PostgreSQL
      #conn = psycopg2.connect("dbname=xxx user=xxx password=xxx host=xxx.xxx.xxx.xxx port =5432")
      #####
       
      # InfluxDB info #
      from influxdb import InfluxDBClient
      #
      #####connection data for InfluxDB#####
      influxClient = InfluxDBClient(host='localhost', port=8086, username='xxx', password='xxx', database='iobroker')
      #####
      #influxClient.delete_database(influx_db_name)
      #influxClient.create_database(influx_db_name)
       
      # dictates how columns will be mapped to key/fields in InfluxDB
      schema = {
          "time_column": "time", # the column that will be used as the time stamp in influx
          "columns_to_fields" : ["ack","q", "from","value"], # columns that will map to fields 
          # "columns_to_tags" : ["",...], # columns that will map to tags
          "table_name_to_measurement" : "name", # table name that will be mapped to measurement
          }
       
      '''
      Generates an collection of influxdb points from the given SQL records
      '''
      def generate_influx_points(records):
          influx_points = []
          for record in records:
              #tags = {}, 
              fields = {}
              #for tag_label in schema['columns_to_tags']:
              #   tags[tag_label] = record[tag_label]
              for field_label in schema['columns_to_fields']:
                  fields[field_label] = record[field_label]
              influx_points.append({
                  "measurement": record[schema['table_name_to_measurement']],
                  #"tags": tags,
                  "time": record[schema['time_column']],
                  "fields": fields
              })
          return influx_points
       
       
       
      # query relational DB for all records
      #curr = conn.cursor('cursor', cursor_factory=psycopg2.extras.RealDictCursor)
      curr = conn.cursor(cursor=pymysql.cursors.DictCursor) 
      # curr = conn.cursor(dictionary=True)
      #####
      # SQL query for PostgreSQL, syntax for MySQL differs
      # query provide desired columns as a view on the sql server
       
      # request data from SQL, adjust ...from <view name>
      curr.execute("Select * from InfluxData;")
      #####
      row_count = 0
      # process 1000 records at a time
      while True:
          print("Processing row #" + str(row_count + 1))
          selected_rows = curr.fetchmany(1000)
          influxClient.write_points(generate_influx_points(selected_rows))
          row_count += 1000
          if len(selected_rows) < 1000:
              break
      conn.close()
       
      
      JackGruber 1 Reply Last reply Reply Quote 0
      • JackGruber
        JackGruber @MezzoDO last edited by

        @MezzoDO

        Ersetze in deinem Script Zeile 47 mit folgenden zwei zeilen. Auf das einrücken achten!:

                    if field_label == "ack":
                        record[field_label] = bool(record[field_label])
        
        simatec 1 Reply Last reply Reply Quote 0
        • simatec
          simatec Developer Most Active @JackGruber last edited by

          @JackGruber
          Ich komme leider auch nicht weiter ...
          Habe deinen Script von Github installiert und bekomme folgende Ausgabe:

          pi@raspberrypi:~/iobroker_mysql_2_influxdb $ python3 migrate.py all
          Migrate 'all' datapoint(s) ...
          
          Total metrics in ts_number: 0
          Total metrics in ts_bool: 0
          Total metrics in ts_string: 0
          Migrated: 0
          
          

          Das ganze passiert innerhalb einer Sekunde .

          Die Influxdb habe ich über den iobroker Adapter angelegt.

          JackGruber 1 Reply Last reply Reply Quote 0
          • JackGruber
            JackGruber @simatec last edited by

            Hi @simatec, hab einen mini Fehler im Script gefunden. Durch einen BUG musste das ALL großgeschrieben werden.
            Hab das Script angepasst, einfach neu herunterladen oder das ALL großschreiben.

            simatec 1 Reply Last reply Reply Quote 0
            • simatec
              simatec Developer Most Active @JackGruber last edited by

              @JackGruber
              Danke für den Tipp ... Ich hatte es auch einzeln versucht und dann auch jedes Objekt einzeln importiert.
              War zwar etwas Aufwand aber ging super 😉

              1 Reply Last reply Reply Quote 0
              • B
                base last edited by

                Wie und wo kann ich das Script benutzen? In iobroker javascript oder Konsole? iobroker läuft bei mir im Docker Container....

                JackGruber 1 Reply Last reply Reply Quote 0
                • JackGruber
                  JackGruber @base last edited by

                  @base python auf deinem PC installieren, das script wie auf der git seite beschrieben installieren und benutzen.

                  B 1 Reply Last reply Reply Quote 0
                  • B
                    base @JackGruber last edited by

                    @JackGruber

                    ich bekomme in diese Zeile einen Syntax Error:

                                print(f"Processing row {processed_rows + 1:,} to {processed_rows + len(selected_rows):,} from LIMIT {start_row:,} / {start_row + query_max_rows:,} " + table + " - " + metric['name'] + " (" + str(metric_nr) + "/" + str(metric_count) + ")")
                    

                    er meckert die Anführungsstriche vor table an

                    simatec JackGruber 2 Replies Last reply Reply Quote 0
                    • simatec
                      simatec Developer Most Active @base last edited by

                      @base
                      So war es bei mir auch.
                      Hast du mal versucht, die Daten einzeln pro Objekt zu importieren? Das half bei mir.

                      B 1 Reply Last reply Reply Quote 0
                      • B
                        base @simatec last edited by base

                        @simatec habe dein Script von oben ausprobiert, dass holt aber nicht alle Objekte...
                        was muss ich da ändern?

                        ok, habs gefunden. Muss natürlich die SQL Abfrage anpassen. Jetzt klappts

                        1 Reply Last reply Reply Quote 0
                        • JackGruber
                          JackGruber @base last edited by

                          @base was für eine python version nutzt du?
                          Bei mir läuft das script ohne probleme durch.
                          76e399c7-9eab-47d9-86d5-cef5e7130421-grafik.png

                          B 1 Reply Last reply Reply Quote 0
                          • B
                            base @JackGruber last edited by

                            @JackGruber hab jetzt Version 3.7.9, hatte vorher 3.9, da ging gar nichts.

                            Den Befehl pip install -r requirements.txt habe ich in der Eingabeaufforderung im entsprechenden Ordner durchgeführt, war das so richtig?

                            JackGruber 1 Reply Last reply Reply Quote 0
                            • JackGruber
                              JackGruber @base last edited by

                              @base Ja, genau, dann sollte er alle nötigen module installieren.
                              Hm, muss ich mir mal mit python 3.9 anschauen ...

                              B 2 Replies Last reply Reply Quote 0
                              • B
                                base @JackGruber last edited by

                                @JackGruber wie gesagt, 3.9 hat gar nicht funktioniert bei mir und bei der 3.7.9 bekomme ich die Fehlermeldung

                                1 Reply Last reply Reply Quote 0
                                • B
                                  base @JackGruber last edited by

                                  @JackGruber Habe gerade festgestellt, dass die Fehlermeldung nur bei der 64bit Version kommt.
                                  Bei der 32èr gehts.
                                  Leider bekomme ich jetzt noch einen:

                                  MySQL connection error
                                  '192.168.1.100'

                                  JackGruber 1 Reply Last reply Reply Quote 0
                                  • JackGruber
                                    JackGruber @base last edited by

                                    @base hm komisch grad mit Python 3.9 64 bit getetet und geht 1a.
                                    Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)]

                                    Das ist die ganze Fehlerausgabe?

                                    B 1 Reply Last reply Reply Quote 0
                                    • B
                                      base @JackGruber last edited by

                                      @JackGruber ja

                                      RESTART: C:\Users\Base\Downloads\iobroker_mysql_2_influxdb-master\iobroker_mysql_2_influxdb-master\migrate.py
                                      MySQL connection error
                                      '192.168.1.100'

                                      JackGruber 1 Reply Last reply Reply Quote 0
                                      • JackGruber
                                        JackGruber @base last edited by

                                        @base teste doch mal in einer powershell folgendes:

                                        Test-NetConnection 192.168.1.100 -p 3307

                                        B 2 Replies Last reply Reply Quote 0
                                        • B
                                          base @JackGruber last edited by

                                          @JackGruber Lernen Sie das neue plattformübergreifende PowerShell kennen – https://aka.ms/pscore6 PS C:\Users\Base> Test-NetConnection 192.168.1.100 -p 3307 WARNUNG: TCP connect to (192.168.1.100 : 3307) failed
                                          ComputerName : 192.168.1.100
                                          RemoteAddress : 192.168.1.100
                                          RemotePort : 3307
                                          InterfaceAlias : WiFi
                                          SourceAddress : 192.168.1.72
                                          PingSucceeded : True
                                          PingReplyDetails (RTT) : 6 ms
                                          TcpTestSucceeded : False

                                          1 Reply Last reply Reply Quote 0
                                          • B
                                            base @JackGruber last edited by

                                            @JackGruber PS C:\Users\Base> Test-NetConnection 192.168.1.100 -p 3306 ComputerName : 192.168.1.100
                                            RemoteAddress : 192.168.1.100
                                            RemotePort : 3306
                                            InterfaceAlias : WiFi
                                            SourceAddress : 192.168.1.72
                                            TcpTestSucceeded : True

                                            PS C:\Users\Base>

                                            JackGruber 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate
                                            FAQ Cloud / IOT
                                            HowTo: Node.js-Update
                                            HowTo: Backup/Restore
                                            Downloads
                                            BLOG

                                            953
                                            Online

                                            31.6k
                                            Users

                                            79.4k
                                            Topics

                                            1.3m
                                            Posts

                                            31
                                            151
                                            18302
                                            Loading More Posts
                                            • Oldest to Newest
                                            • Newest to Oldest
                                            • Most Votes
                                            Reply
                                            • Reply as topic
                                            Log in to reply
                                            Community
                                            Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                                            The ioBroker Community 2014-2023
                                            logo