【问题标题】:KSQLDB coalesce always returns null despite parameters尽管有参数,KSQLDB coalesce 总是返回 null
【发布时间】:2021-03-25 00:59:21
【问题描述】:

我有以下 ksql 查询:

SELECT 
  event->acceptedevent->id as id1, 
  event->refundedevent->id as id2, 
  coalesce(event->acceptedevent->id, event->refundedevent->id) as coalesce_col 
FROM events 
EMIT CHANGES;

根据文档,(https://docs.ksqldb.io/en/latest/developer-guide/ksqldb-reference/scalar-functions/#coalesce) COALESCE 返回第一个非空参数。

查询返回以下内容:

+-----------------------------------------------+-----------------------------------------------+-----------------------------------------------+
|ID1                                            |ID2                                            |COALESCE_COL                                   |
+-----------------------------------------------+-----------------------------------------------+-----------------------------------------------+
|1                                              |null                                           |null                                           |
|2                                              |null                                           |null                                           |
|3                                              |null                                           |null                                           |

我期待,因为 ID1 显然不是 null,作为调用的第一个参数,COALESCE 将返回与 ID1 相同的值,但它返回 null。我错过了什么?

我正在使用 confluentinc/cp-ksqldb-server:6.1.1 并使用 avro 作为值 serde。

EventMessage.avsc:

{
  "type": "record",
  "name": "EventMessage",
  "namespace": "com.example.poc.processor2.avro",
  "fields": [
    {
      "name": "event",
      "type": [
        "com.example.poc.processor2.avro.AcceptedEvent",
        "com.example.poc.processor2.avro.RefundedEvent"
      ]
    }
  ]
}

【问题讨论】:

  • 你运行的是什么版本的 ksqlDB?
  • @RobinMoffatt 我正在使用 confluentinc/cp-ksqldb-server:6.1.1

标签: apache-kafka ksqldb


【解决方案1】:

可能是数据反序列化方式或 COALESCE 函数的错误。

  • 你运行的是什么 KSQL 版本
  • 您的数据在主题中是如何序列化的?

我尝试了 JSON 格式,它成功了。

ksql> describe events;

Name                 : EVENTS
 Field | Type                                                                       
------------------------------------------------------------------------------------
 EVENT | STRUCT<ACCEPTEDEVENT STRUCT<ID INTEGER>, REFUNDEDEVENT STRUCT<ID INTEGER>> 
------------------------------------------------------------------------------------

ksql> print 'events' from BEGINNING;
Key format: ¯\_(ツ)_/¯ - no data processed
Value format: JSON or KAFKA_STRING
rowtime: 2021/03/24 13:57:27.403 Z, key: <null>, value: {"event":{"acceptedevent":{"id":1}, "refundedevent":{}}}, partition: 


ksql> select event->acceptedevent->id, event->refundedevent->id, coalesce(event->acceptedevent->id, event->refundedevent->id) from events emit changes;
+----------------------------------------------------------+----------------------------------------------------------+----------------------------------------------------------+
|ID                                                        |ID_1                                                      |KSQL_COL_0                                                |
+----------------------------------------------------------+----------------------------------------------------------+----------------------------------------------------------+
|1                                                         |null                                                      |1                                                         |

【讨论】:

  • 我正在使用 confluentinc/cp-ksqldb-server:6.1.1 并使用 avro 作为值 serde。
猜你喜欢
  • 2013-10-31
  • 2022-10-21
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 2016-06-11
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多