Crate influxlp_tools

Source
Expand description

InfluxDB V2 Line Protocol Tools is a parsing and building library for InfluxDB v2’s line protocol. It provides easy-to-use functionality with built-in validation, support for a builder pattern and dynamic population, and options for modifying existing line protocols

§Example

Below is an example of building a line protocol string and parsing one

§Building a line protocol string

At minimum the measurement name and a field is required to build a valid line protocol string

let line_protocol = LineProtocol::new("measurement")
    .add_field("field", "value")
    .build()
    .unwrap();

You can overwrite the measurement name by calling the measurement method

let mut line_protocol = LineProtocol::new("measurement")
    .add_field("field", "value")
    .build()
    .unwrap();

line_protocol = line_protocol.measurement("new_measurement");

Multiple fields can be add by calling the add_field method multiple times

let line_protocol = LineProtocol::new("measurement")
    .add_field("field1", "value")
    .add_field("field2", "value")
    .build()
    .unwrap();

Optionally tags can be added. More tags can be added as with fields

let line_protocol = LineProtocol::new("measurement")
    .add_tag("tag1", "value")
    .add_tag("tag2", "value")
    .add_field("field", "value")
    .build()
    .unwrap();

A timestamp can be added with the with_timestamp method. By default the timestamp is defined in nanosecond precision. If you are using any other precision, e.g., seconds, it needs be defined when querying influx

let line_protocol = LineProtocol::new("measurement")
    .add_field("field", "value")
    .with_timestamp(1729270461612452700i64)
    .build()
    .unwrap();

A field, tag, and timestamp can be deleted if needed. This is done by calling the respective delete function

let mut line_protocol = LineProtocol::new("measurement")
    .add_tag("tag", "value")
    .add_field("field", "value");

line_protocol.delete_tag("tag")

Note: that deleting all fields will cause the building to fail as atleast one field is required

§Parsing a line protocol string

To parse a line protocol string the parse_line method can be used

let line =
    "measurement,tag2=value,tag=value field=\"hello\",field2=\"world\" 1729270461612452700";
let line_protocol = LineProtocol::parse_line(line).unwrap();

To parse multiple lines seperated by a newline the parse_lines method can be used instead

let lines = vec![
    "measurement,tag=value field=\"value\"",
    "measurement field=\"{\\\"test\\\": \\\"hello\\\"}\"",
    "measurement,tag2=value,tag=value field=\"value\",field2=\"{\\\"test\\\": \
     \\\"hello\\\"}\" 1729270461612452700",
]
.join("\n");

let result = LineProtocol::parse_lines(&lines);

Note: The parsed line can be modified and rebuilt if needed

Modules§

builder
A line is built by using the builder methods
element
Elements are whats makes up the individual parts of a line protocol string
error
parser
The parser methods consist of three main methods used for parsing line(s)
traits

Structs§

LineProtocol