typeError使用Python的AioInflux用户定义的模式,将数据写入涌入时,没有值

发布于 2025-01-29 15:29:28 字数 1018 浏览 4 评论 0原文

因此,我有一个我定义的架构,并与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木有鱼丸 2025-02-05 15:29:28

因此,在挖掘出 aioinflux package ,似乎是这种情况的行为:

  • 如果尝试使用字典中的字典格式编写数据,它确实接受none在字典中,它只是将它们从写入涌入的文字中,以便您获得所需的行为
    - 如果您将使用定义的模式与dataclass使用相同的行为,则它实际上试图将其实际写入inter ,并且失败

了是我的是将用户定义的模式保留为文档的缩短aioinflux的字典期望如下所示:

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"

    def to_dict(self):
        return {
            "measurement": "my_table_name",
            "fields": {
                "object_id": self.object_id,
                "weight": self.weight,
                "size": self.size,
                "width": self.width,
                "confidence": self.confidence,
            },
            "tags": {
                "dt": self.dt,
                "risk": self.risk,
            },
            "time": self.timestamp,
        }

然后在写入infux之前,我产生了此词典。这是一个黑客,但是鉴于库不支持用户定义的架构编写 none ,这是一个可接受的解决方案。

So after digging in the aioinflux package, it seems that is has this werid behavior:

  • If you try to write the data using the dictionary format it does accept 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 to Influx and it fails

So 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 a to_dict method to make the dictionary that aioinflux expects as shown below:

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"

    def to_dict(self):
        return {
            "measurement": "my_table_name",
            "fields": {
                "object_id": self.object_id,
                "weight": self.weight,
                "size": self.size,
                "width": self.width,
                "confidence": self.confidence,
            },
            "tags": {
                "dt": self.dt,
                "risk": self.risk,
            },
            "time": self.timestamp,
        }

and then before writing to Influx I produce this dictionary. It is a hack but given that the library does not support writing None for user defined schema it is an acceptable solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文