2020-11-27

how to write dataframe into influxdb

i am new to working with influxdb, I have no problem establishing the connection to the db but I cant write to the database I receive errors like tags = point.get('tags') or {} AttributeError: 'str' object has no attribute 'get'

        data = json.loads(api_request.data.decode('utf-8'))

        # To process the data further with python data analytics library pandas
        # for easy processing i convert it into pandas dataframe
        df_apidata = pd.json_normalize(data, "data")
        # influxdata= jsonify(df_apidata.to_dict())
        # influxdata = json.dumps(df_apidata.to_json())
        # host = ' http://127.0.0.1'
        # port = 8086
        # user = 'root'
        # password = 'root'
        # dbname = 'kalidb'
        # dbuser = 'kalikimanzi'
        # dbuser_password = 'my_secret_word'
        client = InfluxDBClient(host='localhost', port=8086)
        print('list of databases')
        print(client.get_list_database())
        client.switch_database('kalidb')

        client.write_points(data)
        # This was not necessary but i included it to confirm the data
        # the head() function takes the number of rows you want to check
        print(df_apidata.head(10))


        # since i am interested with only marketprice
        # online i extract the marketprice column  from the dataframe by creating
        # another dataframe with the market price column, though i can also extract other
        # columns by specifying them
        df_marketprice = df_apidata[['marketprice', 'unit']]

        # The reason why i am changing this a dictionary is because it takes a longer process
        # to serialize a dataframe to json format. since using jsonify i can convert
        # the python dictionary to json format to be send over the api
        marketprice_dictionary = df_marketprice.to_dict("dict")

        return jsonify(marketprice_dictionary)

the errors are as follows


AttributeError: 'str' object has no attribute 'get'


from Recent Questions - Stack Overflow https://ift.tt/33nmFxJ
https://ift.tt/eA8V8J

No comments:

Post a Comment