Skip to main content

Parse InfluxDB line protocol string into Python dictionary

Project description

Parse InfluxDB line protocol strings into Python dictionaries.

Example:

>>> from line_protocol_parser import parse_line
>>> data = parse_line('myMeas,someTag=ABC field1=3.14,field2="Hello, World!" 123')
>>> print(data)
{'measurement': 'myMeas',
'fields': {'field1': 3.14, 'field2': 'Hello, World!'},
'tags': {'someTag': 'ABC'},
'time': 123}

The InfluxDB line protocol is a text based format for writing points to InfluxDB. This project can read this format and convert line strings to Python dicitonaries.

The line protocol has the following format:

<measurement>[,<tag_key>=<tag_value>[,<tag_key>=<tag_value>]] <field_key>=<field_value>[,<field_key>=<field_value>] [<timestamp>]

and is documented here: InfluxDB line protocol.

The line_protocol_parser module only contains the parse_line function and the LineFormatError exception which is raised on failure.

Installation

From PyPI:

$ python3 -m pip install line-protocol-parser

or from source (make sure you have python3 -m pip install wheel setuptools first):

$ git clone https://github.com/Penlect/line-protocol-parser.git
$ cd line-protocol-parser
$ python3 setup.py bdist_wheel
$ python3 -m pip install ./dist/line-protocol-parser-*.whl

or from generated Debian package:

# Install build dependencies
$ sudo apt install python3-all python3-all-dev python3-setuptools dh-python
$ git clone https://github.com/Penlect/line-protocol-parser.git
$ cd line-protocol-parser
$ make deb
$ sudo apt install ./python3-line-protocol-parser_*.deb

Use Case 1: Read points from a file

Suppose you have a text file with influxDB measurement points, “my_influxDB_points.txt”:

myMeasurement,someTag=A temperature=37.0 1570977942581909918
myMeasurement,someTag=A temperature=37.3 1570977942581910000
myMeasurement,someTag=A temperature=36.9 1570977942581912345
myMeasurement,someTag=A temperature=37.1 1570977942581923399
...

Then you can load each line into a dicitonary to be printed like this:

>>> from line_protocol_parser import parse_line
>>> with open('my_influxDB_points.txt', 'r') as f_obj:
...     for line in f_obj:
...         print(parse_line(line))

Use Case 2: InfluxDB subscriptions

InfluxDB subscriptions are documented here: InfluxDB Subscriptions.

InfluxDB subscriptions are local or remote endpoints to which all data written to InfluxDB is copied. Endpoint able to accept UDP, HTTP, or HTTPS connections can subscribe to InfluxDB and receive a copy of all data as it is written.

In this example we will do the following:

  1. Setup and run a InfluxDB container.

  2. Create a subscription.

  3. Create a Python server and register it as an endpoint.

  4. Use line_protocol_parser to read and print incoming data.

Step 1. Run the following commands to run a InfluxDB container and attach to the influx client.

$ docker run -d --network="host" --name inf influxdb
$ docker exec -it inf influx

Step 2. Create subscription. Run these commands in the influx client prompt.

> CREATE DATABASE mydb
> USE mydb
> CREATE SUBSCRIPTION "mysub" ON "mydb"."autogen" DESTINATIONS ALL 'http://localhost:9090'

Since we used –network=”host” we can use localhost from inside the container.

Step 3 & 4. Python server to receive InfluxDB data.

Create a python file server.py with the following content:

from pprint import pprint
from http.server import HTTPServer, BaseHTTPRequestHandler
from line_protocol_parser import parse_line

class PostHandler(BaseHTTPRequestHandler):

    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        post_data = self.rfile.read(content_length)
        pprint(parse_line(post_data))
        self.send_response(200)
        self.end_headers()

if __name__ == '__main__':
    server = HTTPServer(('localhost', 9090), PostHandler)
    print('Starting server, use <Ctrl-C> to stop')
    server.serve_forever()

Start the server:

$ python3 server.py
Starting server, use <Ctrl-C> to stop

Next, go back to your influx client and insert a data point:

> INSERT oven,room=kitchen temperature=225.0 1234567890

Head back to your Python server and watch the output:

$ python3 server.py
Starting server, use <Ctrl-C> to stop
{'fields': {'temperature': 225.0},
 'measurement': 'oven',
 'tags': {'room': 'kitchen'},
 'time': 1234567890}
172.17.0.2 - - [14/Oct/2019 21:02:57] "POST /write?consistency=&db=mydb&precision=ns&rp=autogen HTTP/1.1" 200 -

Pure C usage

If you are not interested in the Python wrapper you may find the pure-c files useful:

  • include/line_protocol_parser.h

  • src/line_protocol_parser.c

Example:

int main()
{
    const char *line = "measurement,tag=value field=\"Hello, world!\" 1570283407262541159";
    struct LP_Point *point;
    int status = 0;
    point = LP_parse_line(line, &status);
    if (point == NULL) {
        LP_DEBUG_PRINT("ERROR STATUS: %d\n", status);
    }
    // < Do something useful with point here >
    LP_free_point(point);
    return status;
}

Please see the comments in the source and header file for more information.

Examples from the Test Cases

The test cases are a good source of examples. Please see: tests/test_parse_line.py.

Changelog

The changelog is maintained in the debian directory, please check there: changelog.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

line-protocol-parser-1.0.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

line_protocol_parser-1.0.0-cp38-cp38-win_amd64.whl (14.0 kB view details)

Uploaded CPython 3.8Windows x86-64

line_protocol_parser-1.0.0-cp38-cp38-win32.whl (13.2 kB view details)

Uploaded CPython 3.8Windows x86

line_protocol_parser-1.0.0-cp38-cp38-linux_armv7l.whl (28.3 kB view details)

Uploaded CPython 3.8

line_protocol_parser-1.0.0-cp37-cp37m-win_amd64.whl (18.6 kB view details)

Uploaded CPython 3.7mWindows x86-64

line_protocol_parser-1.0.0-cp37-cp37m-win32.whl (13.1 kB view details)

Uploaded CPython 3.7mWindows x86

line_protocol_parser-1.0.0-cp37-cp37m-linux_armv7l.whl (28.3 kB view details)

Uploaded CPython 3.7m

line_protocol_parser-1.0.0-cp36-cp36m-win_amd64.whl (18.6 kB view details)

Uploaded CPython 3.6mWindows x86-64

line_protocol_parser-1.0.0-cp36-cp36m-win32.whl (17.9 kB view details)

Uploaded CPython 3.6mWindows x86

line_protocol_parser-1.0.0-cp36-cp36m-linux_armv7l.whl (27.3 kB view details)

Uploaded CPython 3.6m

line_protocol_parser-1.0.0-cp35-cp35m-linux_armv7l.whl (27.5 kB view details)

Uploaded CPython 3.5m

File details

Details for the file line-protocol-parser-1.0.0.tar.gz.

File metadata

  • Download URL: line-protocol-parser-1.0.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for line-protocol-parser-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ac7791803920a3fb799fb0d346385bbc50467bd17e9c45dfe5e0aa711e350e14
MD5 b0dc0dac7d54980c8e63e5f3ddf8738d
BLAKE2b-256 564cd5def2c9eb2a37898dfdcb57f2c472ea028eeb7f45e8e519fd6ef1843618

See more details on using hashes here.

File details

Details for the file line_protocol_parser-1.0.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: line_protocol_parser-1.0.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for line_protocol_parser-1.0.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 43b4f5b04b170cdca10ea96d807db2b760fb8e3e75c286a1c39986a45a4082ab
MD5 7fb8936073a7b4afb2270244572a8fe8
BLAKE2b-256 0d7bbc2bca65465e4fd694bafdc64b7feba71164f0480ad7373c8c1161c42ef4

See more details on using hashes here.

File details

Details for the file line_protocol_parser-1.0.0-cp38-cp38-win32.whl.

File metadata

  • Download URL: line_protocol_parser-1.0.0-cp38-cp38-win32.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: CPython 3.8, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for line_protocol_parser-1.0.0-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 319d6a7fe753fd1a4f0d312d2b61c439f35773a46de9afbe76b9c37394c3c5c6
MD5 4fabc8d7568af2fc38b0da930eb85000
BLAKE2b-256 4bb30ab6ad3816ae443d1ae2d6bd514f193cf55190a4cf8e7983adc6486658d7

See more details on using hashes here.

File details

Details for the file line_protocol_parser-1.0.0-cp38-cp38-linux_armv7l.whl.

File metadata

  • Download URL: line_protocol_parser-1.0.0-cp38-cp38-linux_armv7l.whl
  • Upload date:
  • Size: 28.3 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.8.0

File hashes

Hashes for line_protocol_parser-1.0.0-cp38-cp38-linux_armv7l.whl
Algorithm Hash digest
SHA256 73c382677c7518bf409ce2bed8ddbedf7ef8ea4f17be6235bd3ed2cd17146422
MD5 218c6e450137321e2ccb61fddee12029
BLAKE2b-256 053e3164cc12b7aca7def6dac9e4ec8014ba8ce53590f8f6e190de213e022fab

See more details on using hashes here.

File details

Details for the file line_protocol_parser-1.0.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: line_protocol_parser-1.0.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for line_protocol_parser-1.0.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 5d9f3a4448e49fe294aa22cf3565141f3aaed5be2039a339071944eb5873a780
MD5 ce529ab439a2bf3bd5c8c57023ba3afe
BLAKE2b-256 784231b3bfdc2c74d8c261481900f1fb2baa5c0c3cf2986355a792159b73eaee

See more details on using hashes here.

File details

Details for the file line_protocol_parser-1.0.0-cp37-cp37m-win32.whl.

File metadata

  • Download URL: line_protocol_parser-1.0.0-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for line_protocol_parser-1.0.0-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 c1b2721bfeebd1fd4be8f5cf1e9cfc7c4fca458bce57d4dd0fccb0333fcb7679
MD5 8bf2e382e70c54795fb7facfa269e56c
BLAKE2b-256 fec0bb944daff8c29c23d3f70cab835476262e1becabb7493ea48aae53686a53

See more details on using hashes here.

File details

Details for the file line_protocol_parser-1.0.0-cp37-cp37m-linux_armv7l.whl.

File metadata

  • Download URL: line_protocol_parser-1.0.0-cp37-cp37m-linux_armv7l.whl
  • Upload date:
  • Size: 28.3 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.8.0

File hashes

Hashes for line_protocol_parser-1.0.0-cp37-cp37m-linux_armv7l.whl
Algorithm Hash digest
SHA256 98827c02fc962032f6d883010ca647a7c0e8c7eac5830741a0aadad076890bf4
MD5 ac431c68ce5f552c3aaf697f9e8a3fae
BLAKE2b-256 0d9e7953c787c483ca3512ae564bf7b4684695913a28fdd626b72fc236ce71f0

See more details on using hashes here.

File details

Details for the file line_protocol_parser-1.0.0-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: line_protocol_parser-1.0.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for line_protocol_parser-1.0.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 12dff2d328258f1c5f08aaacc50d0e011888af1d2f28eabe060aabf23f9bc8be
MD5 d2259088d36181a92242746832efa12c
BLAKE2b-256 92fd5b1a94caab19d13e9eff5b2b688ca3b4b4a64af3661a7ce5cf9aaa78dca5

See more details on using hashes here.

File details

Details for the file line_protocol_parser-1.0.0-cp36-cp36m-win32.whl.

File metadata

  • Download URL: line_protocol_parser-1.0.0-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: CPython 3.6m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for line_protocol_parser-1.0.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 5c5dd2298b16fceab68a1a3cda6be700741e006ca57c770c3fbd0389a626ba2d
MD5 267951c91b762672360c1f1fa30a0444
BLAKE2b-256 7a16b894e796a0d902986f11561f5a3a92a4727834c7d467219305fc6f72f900

See more details on using hashes here.

File details

Details for the file line_protocol_parser-1.0.0-cp36-cp36m-linux_armv7l.whl.

File metadata

  • Download URL: line_protocol_parser-1.0.0-cp36-cp36m-linux_armv7l.whl
  • Upload date:
  • Size: 27.3 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.8.0

File hashes

Hashes for line_protocol_parser-1.0.0-cp36-cp36m-linux_armv7l.whl
Algorithm Hash digest
SHA256 2ed1b31b8c9ff25f3582ce14032dc556d536cb7be9ac4cd80cbf5dde27ea22a4
MD5 86f225e38439ebb99b55cccc01b3c681
BLAKE2b-256 47e4cdca70d2d31252d5fc8882a1fd775c75ccfece9e9682ce23be2203da382f

See more details on using hashes here.

File details

Details for the file line_protocol_parser-1.0.0-cp35-cp35m-linux_armv7l.whl.

File metadata

  • Download URL: line_protocol_parser-1.0.0-cp35-cp35m-linux_armv7l.whl
  • Upload date:
  • Size: 27.5 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for line_protocol_parser-1.0.0-cp35-cp35m-linux_armv7l.whl
Algorithm Hash digest
SHA256 a34ed33f81d079a8ba6e1a0ddeaae21d9792d76399315bc10e78812f690448a1
MD5 2b1bb28033d2b09d54796612dd579295
BLAKE2b-256 b765394a3150eae2e80cd8b22ab0225bf9a5f1b3de1eff07d7d8955ec8b1a722

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page