Interacting with InfluxDB v2 from Mathematica via the Wolfram Language

Dr Stuart Woolley
Jul 3 · 3 min read

Sometimes when you’re approaching the bleeding edge, and have sparse or rather concise documentation, all you need is a little push to get going…

Image for post
Image for post

Flexible Data Wrangling

I’ve been using Mathematica for several years now and sometimes either because of the sheer scope of the language or the huge number of built-in functions available it’s sometimes difficult to locate exactly what I need.

Often I’ll find that after a period of investigation I’ll find a function that does exactly what I want but the often terse documentation available doesn’t always give me enough information to get going.

Sometimes it’s the arguments, sometimes it’s the return values, but mostly it’s the lack of concrete examples of specific use cases. The documentation is comprehensive but because of the often huge number of permutations there simply isn’t room to include everything!

In this case I’d usually head over to Stack Overflow or the Stack Exchange for Mathematica itself and go searching, but often enough because of the relatively niche user base and/or the rarity of someone doing the same thing it’s usually the case that I’m left without much to go on — particularly if it’s something near the bleeding edge.

Lately I’ve been working with InfuxDB v2 with it’s built-in Flux language and have found it a dream to work with — add to this the flexibility and speed of Mathematica and the Wolfram Language for prototyping and this rapidly becomes a dream toolset for data wrangling.

I couldn’t find any examples or write-ups of anyone doing this so I put together a couple of useful Mathematica Wolfram Language functions for interfacing to InfluxDB v2 with its line protocol that can easily be adapted for your own use.

Writing to InfluxDB

Prepare some textual data in the line protocol format, and store this in a String, each line terminated with a \n as usual. For example,

"measurementdata,SID=194,SID=5755 w1=7900 1593523543000000000\measurementdata,SID=194,SID=5755 w1=7905 1593523555000000000\n"

Now pass this to the following example function, which calls URLExecute[] on a prepared HTTPRequest[] that includes the necessary information to interact with InfluxDB v2’s API.

postToInflux[textdata_]:=Module[
{result},
result = URLExecute @ HTTPRequest[
"YOUR-MACHINE-LOCATION:9999/api/v2/write",
<|
"Method" -> "POST",
"Headers" -> {"Authorization" -> "Token YOUR-ACCESS-TOKEN"},
"Query" -> {"org"->"YOUR-ORG","bucket"->"YOUR-BUCKET"},
"Body" -> textdata
|>
];
Return[result];
]

Make sure you insert your specific authentication token that can be found under Load Data -> Tokens at the top left on the default InfluxDB UI. Additionally, further details on authentication tokens can be found here.

Several thousand entries can often be batched successfully, and I usually use a driving function to call postToInflux[] that passes 4,000 or so depending on how many tags and values are present. This is at your discretion.

Reading from InfluxDB

Performing a query on InfluxDB is a similar operation, here’s an example that counts the number of measurements in a specific bucket.

countItems[]:=Module[
{result},
result = URLExecute @ HTTPRequest[
"YOUR-MACHINE-LOCATION:9999/api/v2/query",
<| "Method" -> "POST",
"Headers" -> {"Authorization" -> "Token YOUR-ACCESS-TOKEN",
"Accept" -> "application/csv",
"Content-type" -> "application/vnd.flux"},
"Query" -> {"org"->"YOUR-ORG","bucket"->"YOUR-BUCKET"},
"Body" -> "from(bucket: \"YOUR-BUCKET\") |> range(start: -30d) |> count(column: \"_value\")"
|>];
Return[result];
]

The Body of the function can contain any query and I’ll leave it to you to modify the function to fit your own circumstances. The Flux language itself is extremely powerful but this function should allow you to pass pretty much any query and allow you to process the results appropriately.

I hope you find them useful.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look

Create a free Medium account to get Best Stories in your inbox.

Dr Stuart Woolley

Written by

Likes Maths, loves , would prefer to be in academia. SpaceX, Twitter, and Overwatch fan. Cares too much about software, coffee, and the Oxford comma.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Dr Stuart Woolley

Written by

Likes Maths, loves , would prefer to be in academia. SpaceX, Twitter, and Overwatch fan. Cares too much about software, coffee, and the Oxford comma.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store