【问题标题】:How to unpack dictionary values inside a beam.Map with Python Apache Beam如何使用 Python Apache Beam 解压缩 beam.Map 中的字典值
【发布时间】:2021-07-26 23:05:12
【问题描述】:

我在 beam.Map 中使用了这个函数:

def to_row(self, record):
    tags = {
        "foo": "bar"
    }

    x = {**tags}

    return beam.Row(
        date=record.get("date"),
        value=record.get("input_value").get("value"),
    )

当函数在管道中运行时,由于语句 x={**tags},它会中断并给出以下错误:

 TypeError: Attempted to determine schema for unsupported type 'Any'

如果我删除 x=... 语句,一切正常。

我知道 Apache Beam 是在 python 中输入的,但我真的不明白为什么我不能解压值。

有没有办法在没有这个问题的情况下正确解包?理想情况下,我想将 Dict[str,str] 传递给 beam.Row 没有任何问题。

【问题讨论】:

    标签: python apache-beam


    【解决方案1】:

    xtags 在您的代码中使用在哪里?

    如果您想将Dict[str, str] 传递给beam.Row,只需解压即可:beam.Row(**tags)

    在您的代码中,x 仍然是等于 tagsDict[str, str]

    【讨论】:

    • xtags 不在函数之外的任何地方使用。 x 只是为了这个问题而添加的。 beam.Row(**tags)) 也不起作用,它给出了同样的错误。换句话说,我的问题是我无法解压任何东西,而且我不明白为什么 Apache Beam 会阻止我这样做。
    • xtags 不应该影响管道执行,因为它们没有被使用。应引发类型错误here。所以这意味着你的 element_type 错过了这个line。您可以通过pcollection.element_type 检查 PCollection 的 element_type。您是否也可以尝试定义 lambda inline 或使用像 pc = input | beam.Map(lambda ...).with_output_types(NamedTupleSchema) 这样的 NamedTuple?
    • 啊,我明白了,你是对的,我的错误不在于拆包,而是因为我在之后使用了apache_beam.dataframe.convert.to_dataframe。为函数to_dataframe 提供代理解决了我的问题
    • @BigJerBD 你能详细说明是什么解决了你的问题吗?我面临着完全相同的事情 - 尝试将 dict 转换为一行(当前使用 beam.Row(**my_dict) ),然后将该 to_dataframe 转换为你得到的错误。你提供了代理是什么意思?
    猜你喜欢
    • 2020-03-18
    • 2021-11-15
    • 2022-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    相关资源
    最近更新 更多