typeError使用Python的AioInflux用户定义的模式,将数据写入涌入时,没有值
因此,我有一个我定义的架构,并与aioinflux
库一起使用,以将Python的数据写入infuxdb
:
from datetime import datetime
from aioinflux import lineprotocol, TIMEDT, TAG, FLOAT, MEASUREMENT, INT
from dataclasses import dataclass
from typing import Optional
@lineprotocol(
schema=dict(
timestamp=TIMEDT,
measurement=MEASUREMENT,
object_id=INT,
dt=TAG,
weight=FLOAT,
width=FLOAT,
size=FLOAT,
risk=TAG,
confidence=FLOAT,
)
)
@dataclass
class CbdrDebugPoint:
timestamp: datetime
object_id: int
dt: str
weight: float
size: Optional[float]
width: Optional[float]
risk: Optional[str]
confidence: Optional[float]
measurement: str = "my_table_name"
但是,如果我的任何一个我的可选>可选
字段获取值等于none
涌入的写作失败,
TypeError("Invalid variable type: value should be str, int or float, got None")
但可以肯定可以写入null
值为涌入,所以任何想法为什么会出现此错误?
So I have a schema that I defined and use with aioinflux
library to write data from Python to InfluxDB
:
from datetime import datetime
from aioinflux import lineprotocol, TIMEDT, TAG, FLOAT, MEASUREMENT, INT
from dataclasses import dataclass
from typing import Optional
@lineprotocol(
schema=dict(
timestamp=TIMEDT,
measurement=MEASUREMENT,
object_id=INT,
dt=TAG,
weight=FLOAT,
width=FLOAT,
size=FLOAT,
risk=TAG,
confidence=FLOAT,
)
)
@dataclass
class CbdrDebugPoint:
timestamp: datetime
object_id: int
dt: str
weight: float
size: Optional[float]
width: Optional[float]
risk: Optional[str]
confidence: Optional[float]
measurement: str = "my_table_name"
but if any of my Optional
fields get value equal to None
the writing to influx fails with the following error:
TypeError("Invalid variable type: value should be str, int or float, got None")
but for sure it is possible to write null
values to influx, so any idea why does this error appear?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,在挖掘出 aioinflux package ,似乎是这种情况的行为:
none
在字典中,它只是将它们从写入涌入的文字中,以便您获得所需的行为- 如果您将使用定义的模式与
dataclass
使用相同的行为,则它实际上试图将其实际写入inter
,并且失败了是我的是将用户定义的模式保留为文档的缩短
aioinflux
的字典期望如下所示:然后在写入
infux
之前,我产生了此词典。这是一个黑客,但是鉴于库不支持用户定义的架构编写 none ,这是一个可接受的解决方案。So after digging in the aioinflux package, it seems that is has this werid behavior:
None
in the dictionary and it simply omits them from writing them to influx so you get the desired behavior-If you use the use defined schema with the
dataclass
it does not have the same behavior, isntead it tries to actually write None toInflux
and it failsSo a way around it was for me to keep the user defined schema as short of documentation where the
dataclass
easily describes the model and it can be easily maintained but then I just added ato_dict
method to make the dictionary thataioinflux
expects as shown below:and then before writing to
Influx
I produce this dictionary. It is a hack but given that the library does not support writingNone
for user defined schema it is an acceptable solution.