InfluxDB - Send data to InfluxDB using Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everyone this is brad from devops journey and today we're going to have a look at how you can bring data into influx db using python this is one of the best ways to ingest data using influx db so if you're using influx db and you want to know how to bring in data using python stick around and if you're new to influx db and you want to check out how to set it up check out my video guide on setting up influx db with that said let's hop into the code so the first thing we're going to do is import influx db and then we're also going to import date time since influx db is a time series database having a timestamp is very important so let's go ahead and do our import statements so we're doing from influx db import influx db client and then we're going to import the date time module and if you don't have the influx db client just make sure to do a pip install in fluxdb after that we're going to set up our database so i'm going to paste in some more code here and you can see that we set up a client here and i just have it running on localhost port 8086 admin password and then a database name in my code i also have a docker file so you can use that to set up influx db but if you want to set it up not on docker then you can have a look at my other video where i just set up in fluxdb on the localhost it's pretty easy to do so let's pull up a python terminal and paste in this code and just make sure it runs okay so we got our python terminal here and we're setting up the client object here now what we can do is go client and then go get underscore list database and this is going to show the databases that exist in influx db and you can see the only database that exists is my database so if we want to we could create a new database which would be client dot create underscore database and then give the database a name and i'll go my new db and you gotta spell create database right so we'll do that and that looks like it worked and now if we go get list database again you can see that we have two databases we have mydb and my new db so if we want to select a certain database we use the switch database command so we'll go client dot switch underscore database and then give the name so we'll go my new db so we have the new database selected the next thing we want to do is set up a payload so i'm going to pull over my code editor here and the first thing i'm going to do is create a list so i'll just go json underscore payload and it's an empty list and the next thing i want to do is set up some data so i'll go data equals and this is going to be a dictionary so i'll put that like that and the first thing we want to do is set a measurement measurements are very similar to tables and we will call this one stocks and then we want to give it a tag the tag parameter is actually a dictionary as well so we'll do some curly brackets and then within there you can add as many tags as you want but i'll just add one we'll say ticker and then let's go tesla and then after that we want to set the time since influx db is a time series database it always needs a timestamp for every entry so we'll just set time and then we want to give it a timestamp so we'll do that with datetime.now and this is going to give our data a date timestamp after that we're going to set up some fields and this is going to be all the data within our measurement so i'll go fields and this is also a dictionary so we'll do our curly brackets and uh let's go open and then close i think this was the tesla price for today and the closing price here and visual studio code's a little mad here you can see it's underlying everything i'm missing a comma and that seems to fix it and this should be sort of the baseline of the payload that we want to send so you can see the most important parts here is first the measurement this is basically the table name if you're used to like sort of sql syntax the measurement is basically the measurement that you're taking tags are really good for organizing the data you can have more than one tag but here i just have one and it's the ticker and you can sort of see that the ticker makes the most sense if you're sort of organizing stock data after that we want to set a timestamp and i'm just setting the date time to now and then we're setting our data fields and we'll add a comma here and then after that we have our fields which are open and close so this is good for our payload so after that let's go json underscore payload and then we'll go dot append and we'll go data and you can see here that this is just a list of payloads and these payloads look like this so you can have more than one entry in this payload before you send it to influx db and that's an important thing to remember so if you're pulling this data from like yahoo finance or something like that you can pull it in sort of take each measurement put it into a payload like this and then just put them all into a list and then send it off to influx db so to send it off to influx db we're going to use clients dot write underscore points and then we'll send our payload so we'll go json underscore payload so we have that let's go ahead and paste it into our terminal and just make sure it works so let's first set up our empty list we got our empty list let's get our data payload and we got our data payload here i'm going to go ahead and print it out just so it looks nice so i'll go from rich import print and i'll go print data and you can see this is what our payload looks like let's go ahead and add it to json payloads so we'll go json underscore payload dot append data and now we're ready to send it to influx db so we'll go client dot write underscore points json payload and we can see that it returned true so we know that it wrote to influx db now if we want to select the data from influx db we do a select statement so let's save it to a variable named result and we'll go client dot query and it's similar to sql syntax here we'll go select star from stocks and you can see that returns our result here and let's print it out so it looks nice so you can see that our data got in there so it's very simple to do with python you could basically take any amount of data that you wanted and put it in a list and then send it over to influxdb and write that data i do it all the time for data that i'm working with and i want it in a database influx db is a very good option for that so anyways if you enjoyed this video go ahead and hit that like button and if you want to learn more about devops or it in general go ahead and subscribe to my channel thanks so much for watching and i hope to see you all in the next video [Music]
Info
Channel: DevOps Journey
Views: 6,694
Rating: 4.9581151 out of 5
Keywords:
Id: 49hKs_H5Xf0
Channel Id: undefined
Length: 8min 22sec (502 seconds)
Published: Mon Apr 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.