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.2.tar.gz (10.5 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.2-cp38-cp38-win_amd64.whl (13.9 kB view details)

Uploaded CPython 3.8Windows x86-64

line_protocol_parser-1.0.2-cp38-cp38-win32.whl (13.0 kB view details)

Uploaded CPython 3.8Windows x86

line_protocol_parser-1.0.2-cp38-cp38-linux_armv7l.whl (31.6 kB view details)

Uploaded CPython 3.8

line_protocol_parser-1.0.2-cp37-cp37m-win_amd64.whl (13.8 kB view details)

Uploaded CPython 3.7mWindows x86-64

line_protocol_parser-1.0.2-cp37-cp37m-win32.whl (13.0 kB view details)

Uploaded CPython 3.7mWindows x86

line_protocol_parser-1.0.2-cp37-cp37m-linux_armv7l.whl (31.2 kB view details)

Uploaded CPython 3.7m

line_protocol_parser-1.0.2-cp36-cp36m-win_amd64.whl (18.4 kB view details)

Uploaded CPython 3.6mWindows x86-64

line_protocol_parser-1.0.2-cp36-cp36m-win32.whl (17.7 kB view details)

Uploaded CPython 3.6mWindows x86

line_protocol_parser-1.0.2-cp36-cp36m-linux_armv7l.whl (30.2 kB view details)

Uploaded CPython 3.6m

line_protocol_parser-1.0.2-cp35-cp35m-linux_armv7l.whl (29.4 kB view details)

Uploaded CPython 3.5m

File details

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

File metadata

  • Download URL: line-protocol-parser-1.0.2.tar.gz
  • Upload date:
  • Size: 10.5 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.2.tar.gz
Algorithm Hash digest
SHA256 33bf84534948c77e7c7b543323a71a7e61f508b8dca639844e26936414802be6
MD5 a0fb329e497f13f269d8f265028eabe4
BLAKE2b-256 738fa295e3e23a17457dacf934a3f73f16214b439af6bc2adee89c29aceb3cb8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: line_protocol_parser-1.0.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 13.9 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/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.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 38feed8c6d254db4734a5b50fc2ea8ec08b3b8627a400497f9f8114b6abcae72
MD5 ebb2ffc60fe27ba1cb9f104c4e19da7e
BLAKE2b-256 ab2fa7c2eef442fa82489c6e178193a82a104a50f590140075d545c32c2b21c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: line_protocol_parser-1.0.2-cp38-cp38-win32.whl
  • Upload date:
  • Size: 13.0 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/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.2-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 d1f341185b3e44108cc23bd9d3d0b1bd3a6325bc13cd69ffbb8c610b167bb4bb
MD5 e859d85e28351fe050f0948eccc5ea4e
BLAKE2b-256 2bc09a7cc5418648b90966ad7b27f157e11a09c08eee3985f49f86796acf4022

See more details on using hashes here.

File details

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

File metadata

  • Download URL: line_protocol_parser-1.0.2-cp38-cp38-linux_armv7l.whl
  • Upload date:
  • Size: 31.6 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.1

File hashes

Hashes for line_protocol_parser-1.0.2-cp38-cp38-linux_armv7l.whl
Algorithm Hash digest
SHA256 0f03a8a77f584e713c1684a9c0079b1a95c502ce17cc870f845f3d6965c1e275
MD5 8e9143b0be143c24f1c5cc7cd30166ec
BLAKE2b-256 5225a560266d546d9a8357ddf375ba401ce44d85bed0548078d8f14e9cd754f6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: line_protocol_parser-1.0.2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 13.8 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/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.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 1298878bb79c68db0671156d126df23c001e66e5d3ae848bd928fdc322da8724
MD5 4f54065720a90be8ead541aa6811ca4a
BLAKE2b-256 af608c5b301ee70c07c46a32ed326a4f15bba8b9227846e38c1ab579cbc770c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: line_protocol_parser-1.0.2-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 13.0 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/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.2-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 d54a92cf507cba2d964a1899b2bcc9b510188e14f03de0ca9aa6b7014f04cb0f
MD5 ac79c2e141a51b68b1beb637cb4123df
BLAKE2b-256 b5fd43f88ae2a30ed1486a1df32762da1589e05bfc848c18d4eb939c952159b8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: line_protocol_parser-1.0.2-cp37-cp37m-linux_armv7l.whl
  • Upload date:
  • Size: 31.2 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.1

File hashes

Hashes for line_protocol_parser-1.0.2-cp37-cp37m-linux_armv7l.whl
Algorithm Hash digest
SHA256 6967a9fa0de421f84391119ae286d736cff4a16d58d8e24e78d472e37f404838
MD5 b79c5f31906256432e5c6802e1480f45
BLAKE2b-256 84d2cdf53e5104d48a26cd2de4aa957252abe6eefdb6e263391b49039542be09

See more details on using hashes here.

File details

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

File metadata

  • Download URL: line_protocol_parser-1.0.2-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 18.4 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/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.2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 b96201dda6ac47844f4d1d9b3fac950fce0b4332271c6bb9df0e4aca654f6378
MD5 58f50d10356aae917352fd9e4e442d8c
BLAKE2b-256 a13f3dd207662ae5831e59f4f61e4bfe9fd5dc7306de96085cf95df19c5b60df

See more details on using hashes here.

File details

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

File metadata

  • Download URL: line_protocol_parser-1.0.2-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 17.7 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/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.2-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 18e13df74e32e2c0a470a0fa19f18761adeb84885648bb7cd3cb44ce5d61ebea
MD5 17629c00dacae30b0fde22bab7015da5
BLAKE2b-256 41c77807a4469288a8ecf8758e3a3963eb026a84000c4336df25ab09ace3622d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: line_protocol_parser-1.0.2-cp36-cp36m-linux_armv7l.whl
  • Upload date:
  • Size: 30.2 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.1

File hashes

Hashes for line_protocol_parser-1.0.2-cp36-cp36m-linux_armv7l.whl
Algorithm Hash digest
SHA256 ee34803102eb8eb4c9ea589291b837876ea8962563e03e440c633ac327c109ca
MD5 46fed3ff08354b73f71ac95b9b6c784c
BLAKE2b-256 e997ec68607d69c46ff081f257ebbfec1f6b79697b6a24feeb43c9b95c221314

See more details on using hashes here.

File details

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

File metadata

  • Download URL: line_protocol_parser-1.0.2-cp35-cp35m-linux_armv7l.whl
  • Upload date:
  • Size: 29.4 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/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.8.1

File hashes

Hashes for line_protocol_parser-1.0.2-cp35-cp35m-linux_armv7l.whl
Algorithm Hash digest
SHA256 0440bd6ce7608051848cab7cd540c335a691007ef66e55fa91b0e6109dcc7112
MD5 371005ae5bb241e620aa5d94f93df8d9
BLAKE2b-256 999debb9bba0026d3ef4fd6a864fcaa44e076ce17bf2552d69a23dcb7de8858f

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