Skip to content

emqx/influxdb-client-erl

Repository files navigation

Influxdb client erl

Client for influxdb

Exampler Code

Start application

application:ensure_all_started(influxdb).

Client Options: Write protocol http or udp

%% http
Option = [ {host, "127.0.0.1"}
         , {port, 8086}
         , {protocol, http}
         , {https_enabled, false}
         , {pool, influxdb_client_pool}
         , {pool_size, 8}
         , {pool_type, random}
         , {version, v1},
         , {path, undefined}
         , {username, <<"uname">>}
         , {password, <<"pwd">>}
         , {database, <<"mydb">>}
         , {precision, <<"ms">>}].
  • The client supports the v1 and v2 API versions of InfluxDB. Using v1 by default, the version automatically chooses the write endpoint path(for v1, it's /write).
  • The optional path option specifies the write endpoint path manually. The other TSDB supporting the InfluxDB writing protocol may have different write endpoint paths. You can configure it with this option.
  • For v1, when username and/or password are provided, the client uses the Authorization: Basic ... header by default rather than putting them in the URL query string. This also applies to ping_with_auth.
  • If a v1 database requires URL credentials instead, set {v1_auth_transport, query_string} to opt in to sending username and password as u and p query parameters. The default is {v1_auth_transport, header}.
  • Security note: sending credentials in the URL query string is less safe than using the Authorization header because URLs may be captured by access logs, proxies, caches, and error reporting systems. Prefer {v1_auth_transport, header} whenever possible, and enable TLS ({https_enabled, true}) when credentials are sent over the network.
%% udp
Option = [ {host, "127.0.0.1"}
         , {port, 8089}
         , {protocol, udp}
         , {pool, influxdb_client_pool}
         , {pool_size, 8}
         , {pool_type, random}].

Start client

{ok, Client} = influxdb:start_client(Option).

Make sure client alive

true = influxdb:is_alive(Client).

Test data

Points = [#{<<"fields">> => #{<<"temperature">> => 1},
            <<"measurement">> => <<"sample">>,
            <<"tags">> =>
                #{<<"from">> => <<"mqttx_4b963a8e">>,<<"host">> => <<"serverA">>,
                  <<"qos">> => 0,<<"region">> => <<"hangzhou">>},
            <<"timestamp">> => 1619775142098},
          #{<<"fields">> => #{<<"temperature">> => 2},
            <<"measurement">> => <<"sample">>,
            <<"tags">> =>
                #{<<"from">> => <<"mqttx_4b963a8e">>,<<"host">> => <<"serverB">>,
                  <<"qos">> => 0,<<"region">> => <<"ningbo">>},
            <<"timestamp">> => 1619775142098}].

Write to influxdb

influxdb:write(Client, Points).

If your pool_type is hash

Key = any_key_you_need.
influxdb:write(Client, Key, Points).

Stop Client

influxdb:stop_client(Client).

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages