Package: line-protocol-parser
Parse InfluxDBline protocolstrings into Python dictionaries.Example:>>>fromline_protocol_parserimportparse_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.Theline_protocol_parsermodule only contains theparse_linefunction and theLineFormatErrorexception which is raised on failure.InstallationFrom PyPI:$python3-mpipinstallline-protocol-parseror from source (make sure you havepython3-mpip install wheel setuptoolsfirst):$gitclonehttps://github.com/Penlect/line-protocol-parser.git$cdline-protocol-parser$python3setup.pybdist_wheel$python3-mpipinstall./dist/line-protocol-parser-*.whlor from generated Debian package:# Install build dependencies$sudoaptinstallpython3-allpython3-all-devpython3-setuptoolsdh-python$gitclonehttps://github.com/Penlect/line-protocol-parser.git$cdline-protocol-parser$makedeb$sudoaptinstall./python3-line-protocol-parser_*.debUse Case 1: Read points from a fileSuppose 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:>>>fromline_protocol_parserimportparse_line>>>withopen('my_influxDB_points.txt','r')asf_obj:...forlineinf_obj:...print(parse_line(line))Use Case 2: InfluxDB subscriptionsInfluxDB 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:Setup and run a InfluxDB container.Create a subscription.Create a Python server and register it as an endpoint.Useline_protocol_parserto read and print incoming data.Step 1. Run the following commands to run aInfluxDB containerand attach to the influx client.$dockerrun-d--network="host"--nameinfinfluxdb$dockerexec-itinfinfluxStep 2. Create subscription. Run these commands in the influx client prompt.>CREATEDATABASEmydb>USEmydb>CREATESUBSCRIPTION"mysub"ON"mydb"."autogen"DESTINATIONSALL'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 fileserver.pywith the following content:frompprintimportpprintfromhttp.serverimportHTTPServer,BaseHTTPRequestHandlerfromline_protocol_parserimportparse_lineclassPostHandler(BaseHTTPRequestHandler):defdo_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:$python3server.pyStartingserver,use<Ctrl-C>tostopNext, go back to your influx client and insert a data point:>INSERToven,room=kitchentemperature=225.01234567890Head back to your Python server and watch the output:$python3server.pyStartingserver,use<Ctrl-C>tostop{'fields':{'temperature':225.0},'measurement':'oven','tags':{'room':'kitchen'},'time':1234567890}172.17.0.2--[14/Oct/201921:02:57]"POST /write?consistency=&db=mydb&precision=ns&rp=autogen HTTP/1.1"200-Pure C usageIf you are not interested in the Python wrapper you may find the pure-c files useful:include/line_protocol_parser.hsrc/line_protocol_parser.cExample:intmain(){constchar*line="measurement,tag=value field=\"Hello, world!\"1570283407262541159";structLP_Point*point;intstatus=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);returnstatus;}Please see the comments in the source and header file for more information.Examples from the Test CasesThe test cases are a good source of examples. Please see:tests/test_parse_line.py.ChangelogThe changelog is maintained in the debian directory, please check there:changelog.