influxdata / influxdb-client-python

InfluxDB 2.0 python client
https://influxdb-client.readthedocs.io/en/stable/
MIT License
705 stars 185 forks source link

Provide LineProtocolParser #577

Closed vlcinsky closed 1 year ago

vlcinsky commented 1 year ago

Proposal: Provde a parser for parsing influxdb line protocol lines

Current behavior: Searching the library I have found only parser for parsing CSV.

Desired behavior:

from influxdb import InfluxDBLineProtocolParser
parser = InfluxDBLineProtocolParser()
line = "measurement,tag1=value1,tag2=value2 field1=42 1621422000000000000"
point = parser.parse_line(line)
print(point)

and it shall print (if formatted as JSON):

{
  "measurement": "measurement",
  "tags": {
    "tag1": "value1",
    "tag2": "value2"
  },
  "time": "2021-05-19T00:00:00Z",
  "fields": {
    "field1": 42
  }
}

Alternatives considered: There is https://pypi.org/project/line-protocol-parser/ but it seems not maintained any more and there is an issue with not following spec for the format for special characters.

Use case: Sometime I need to process line protocol without explicitly dealing with InfluxDB, e.g. when i transfer line protocol lines via other means such as mqtt broker.

powersj commented 1 year ago

This is outside the scope of this client library. The primary focus of this library is to send data into InfluxDB and as such get data into line protocol.

What you are after is essentially:

  1. Split the line on white space.
  2. Verify that the length is 2 or 3. If length of 3 (e.g. 1621422000000000000), set the timestamp. If length of 2, no timestamp. Error on other lengths.
  3. Take the second value (e.g. `field1=42) and loop through each of the fields
  4. Take the first value (e.g. measurement,tag1=value1,tag2=value2) take index 0 as the measurement name. If the length is greater than 1, then loop through each and set your tags.

Closing as not in plan.