将InfluxDB行协议字符串解析到Python字典中

line-protocol-parser的Python项目详细描述


将InfluxDBline protocol字符串解析到Python字典中。在

示例:

>>>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}

InfluxDB line协议是一种基于文本的格式,用于将点写入InfluxDB。 此项目可以读取此格式并将行字符串转换为Python二进制格式。

线路协议的格式如下:

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

并记录在这里:InfluxDB line protocol。在

{{tt1}函数{tt1}仅在tt1}上引发异常^$3。在

安装

来自PyPI:

^{pr2}$

或从源代码(确保先有python3 -m pip install wheel setuptools):

$ 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

或从生成的Debian包:

# 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

用例1:从文件中读取点

假设您有一个包含influxDB测量点的文本文件,“my_influxDB_点.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
...

然后,您可以将每一行加载到一个要打印的二进制文件中,如下所示:

>>>fromline_protocol_parserimportparse_line>>>withopen('my_influxDB_points.txt','r')asf_obj:...forlineinf_obj:...print(parse_line(line))

用例2:InfluxDB订阅

InfluxDB订阅记录在这里:InfluxDB Subscriptions。在

InfluxDB订阅是本地或远程终结点,写入InfluxDB的所有数据都将复制到该端点。能够接受UDP、HTTP或HTTPS连接的端点可以订阅InfluxDB并在写入时接收所有数据的副本。在

在本例中,我们将执行以下操作:

  1. 设置并运行InfluxDB容器。在
  2. 创建订阅。在
  3. 创建一个Python服务器并将其注册为端点。在
  4. 使用line_protocol_parser读取和打印传入数据。在

步骤1。运行以下命令来运行InfluxDB container并附加到inflix客户机。在

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

步骤2。创建订阅。在inflix client提示符中运行这些命令。在

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

因为我们使用了–network=“host”,所以可以从容器内部使用localhost。在

步骤3&4。接收InfluxDB数据的Python服务器。在

创建python文件服务器.py包含以下内容:

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()

启动服务器:

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

接下来,回到您的流入客户端并插入一个数据点:

> INSERT oven,room=kitchen temperature=225.0 1234567890

返回Python服务器并查看输出:

$ 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 -

纯C用法

如果您对Python包装器不感兴趣,您可能会发现pure-c文件很有用:

  • include/line_protocol_parser.h
  • src/line_protocol_parser.c

示例:

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;}

有关详细信息,请参阅源文件和头文件中的注释。在

来自测试用例的示例

测试用例是很好的示例来源。请看:tests/test_parse_line.py。在

变更日志

变更日志保存在debian目录中,请检查:changelog。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java为什么单元测试中的“验证”在不同实例的情况下不返回“不同参数”错误?   java如何从具有相同类名的div中选择元素(Selenium)   Java制作纸牌游戏,有关于类的问题吗   bluej在Java中使用变异器和访问器设置颜色和圆半径   java准确定位异常错误的最佳方法是什么?   使用多个监视器时的java设置对话框位置   java如何在不使用JavascriptExecutor的情况下在网页中向下/向上滚动   java在for循环中将大小设置为n   java为什么akka需要不可变的消息   java LdapInvalidDnException:意外标记:   java如果字符串的第一个字母不是一个,如何大写?   使用htmlunit的java html内容提取   java从CDI托管bean获取方法的注释   java有办法在运行时检查post请求参数的大小吗   java我可以将实体注入托管Bean并直接持久化吗?