【问题标题】:How to use table.where() to filter for subfields in PyFlink?如何使用 table.where() 过滤 PyFlink 中的子字段?
【发布时间】:2021-12-17 02:43:32
【问题描述】:

我正在使用 pyflink 和 Flink 11.2,我已经这样定义了我的表:

def _create_sink_table(st_env):
    # Create SINK table.
    st_env.execute_sql(f"""
        CREATE TABLE {"in"} (
            `a` STRING,
            `b` STRING,
            `c` STRING,
            `d` ROW(
                `e` STRING,
                `f` STRING
            )
        ) WITH (
            'connector' = 'kafka',
            'topic' = '{SINK_TOPIC_NAME}',
            'properties.bootstrap.servers' = '{BOOTSTRAP_SERVERS}',
            'format' = 'json',
            'json.fail-on-missing-field' = 'false',
            'json.ignore-parse-errors' = 'true'
        )
    """)

我想使用 table.where 来过滤子字段:

def execute(st_env: StreamTableEnvironment):
    table = st_env.from_path("in")
    table = table.select("a, b, c, d")
    table = table.where("d.e = 'fail'")

到目前为止,我所做的一切都导致了错误,这是其中之一:

➜  ./run.sh
Traceback (most recent call last):
  File "/tmp/test.py", line 143, in <module>
    main()
  File "/tmp/test.py", line 139, in main
    execute(st_env)
  File "/tmp/test.py", line 109, in execute
    table = table.where("d.e = 'fail'")
  File "/opt/flink/opt/python/pyflink.zip/pyflink/table/table.py", line 140, in where
  File "/opt/flink/opt/python/py4j-0.10.8.1-src.zip/py4j/java_gateway.py", line 1286, in __call__
  File "/opt/flink/opt/python/pyflink.zip/pyflink/util/exceptions.py", line 147, in deco
  File "/opt/flink/opt/python/py4j-0.10.8.1-src.zip/py4j/protocol.py", line 328, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o39.where.
: org.apache.flink.table.api.ValidationException: Undefined function: e
    at org.apache.flink.table.expressions.resolver.LookupCallResolver.lambda$visit$0(LookupCallResolver.java:49)
    at java.util.Optional.orElseThrow(Optional.java:290)
    at org.apache.flink.table.expressions.resolver.LookupCallResolver.visit(LookupCallResolver.java:49)
    at org.apache.flink.table.expressions.resolver.LookupCallResolver.visit(LookupCallResolver.java:38)
    at org.apache.flink.table.expressions.ApiExpressionVisitor.visit(ApiExpressionVisitor.java:37)
    at org.apache.flink.table.expressions.LookupCallExpression.accept(LookupCallExpression.java:65)
    at org.apache.flink.table.expressions.resolver.LookupCallResolver.lambda$resolveChildren$1(LookupCallResolver.java:64)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1384)

它尝试在 d 上执行函数 e,而不是检索子字段。

【问题讨论】:

  • 什么错误?展示您尝试过的内容,并完整引用所有错误。
  • 我添加了一条带有错误消息的评论,因为 SO 不允许我引用,所以无法引用完整的错误堆栈跟踪。

标签: pyflink


【解决方案1】:

我认为table.where("d.get(0) = 'fail'") 可能有用

【讨论】:

    猜你喜欢
    • 2018-09-28
    • 2017-03-08
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多