InfluxDB interface for elixir
If available in Hex, the package can be installed as:
-
Add influx_ex to your list of dependencies in
mix.exs
:def deps do [{:influx_ex, "~> 0.0.1"}] end
-
Ensure influx_ex is started before your application:
def application do [applications: [:influx_ex]] end
defmodule MyApp.InfluxConnection do
use InfluxEx.Connection, otp_app: :my_app
end
In config.exs
(or dev.exs
or test.exs
...)
config :my_app, MyApp.InfluxConnection,
base_url: "http://localhost:8086"
MyApp.InfluxConnection.create_db("my_db")
See InfluxEx.Connection
for details.
MyApp.InfluxConnection.drop_db("my_db")
See InfluxEx.Connection
for details.
:ok = MyApp.InfluxConnection.write(%{measurement: "cpu",
fields: %{load: 0.12},
tags: %{host: "web-staging"},
time: 12345678},
"my_db")
You can also write a list of points in a single query:
points = for i <- 1..2 do
%{measurement: "cpu",
fields: %{load: i + 0.12},
time: i + 12345670}
end
:ok = MyApp.InfluxConnection.write(points, "my_db")
See InfluxEx.Connection
for details.
MyApp.InfluxConnection.read("SELECT * FROM cpu", "my_db")
[ok: [
%{series: "cpu",
points: [
%{"host" => "web-staging", "load" => 0.12,
"time" => "2015-11-01T14:26:25.73642475Z"}
]}
]]
Because you can potentially run multiple queries in one request, you will get a list
back from InfluxConnection.read
. This list consists of a {:ok, results}
or
{:error, message}
tuple for each query.
results
in turn consists of a list of points per series.