简体   繁体   English

KSQL 可以使用模式中提供的默认值填充流吗?

[英]Can KSQL populate streams with default values provided in the schema?

I have a KSQL stream that I'd like to populate with the default values specified in the schema.我有一个 KSQL 流,我想用模式中指定的默认值填充它。 Other than manually specifying them again with coalesce statements, is there a way to do this?除了用 coalesce 语句再次手动指定它们之外,有没有办法做到这一点?

The previously answered question is similar to the issue I am facing, but it doesn't address the main point of using the default values already specified in the schema:先前回答的问题与我面临的问题类似,但它没有解决使用模式中已指定的默认值的要点:

Create KSQL stream with default values for a column? 使用列的默认值创建 KSQL 流?

I did the following (based on the documentation provided by Confluent: https://docs.confluent.io/platform/current/schema-registry/serdes-develop/serdes-avro.html#schema-references-in-avro ):我做了以下操作(基于 Confluent 提供的文档: https ://docs.confluent.io/platform/current/schema-registry/serdes-develop/serdes-avro.html#schema-references-in-avro ):

  1. Created a topic t1-a with a schema使用模式创建主题 t1-a
kafka-avro-console-producer --bootstrap-server localhost:9092 --property schema.registry.url=http://localhost:8081 --topic t1-a \
--property value.schema='{"type":"record","name":"myrecord","fields":[{"name":"name","type":"string","default":"no-name"}]}
  1. Set the compatibility to FULL on the subject (using the schema-registry REST API)将主题的兼容性设置为 FULL(使用模式注册表 REST API)
  2. Generated records to the topic using the CLI tool使用 CLI 工具为主题生成记录
{"name":"john"}
{"name":"doe"}
  1. Updated the schema using the Schema-registry使用模式注册表更新了模式
kafka-avro-console-producer --bootstrap-server localhost:9092 --property schema.registry.url=http://localhost:8081 --topic t1-a \
--property value.schema='{"type":"record","name":"myrecord","fields":[{"name":"name","type":"string", "default":"no-name"}, {"name":"age","type":"string", "default":"ageless-wonder"}]}
  1. Generated records to the topic using the CLI tool:使用 CLI 工具生成主题记录:
{"name":"jack", "age":"100"}
{"name":"jill", "age":"101"}
  1. started ksql cli and created a stream启动 ksql cli 并创建一个流
CREATE STREAM t1_a WITH (KAFKA_TOPIC='t1-a',VALUE_FORMAT='AVRO');
  1. Queried Records:查询记录:
SELECT * FROM t1_a;

Now I get the records, but the value in Age for John and Doe are listed as null (instead of the default value "ageless-wonder" specified in the schema):现在我得到了记录,但是 John 和 Doe 的 Age 中的值被列为 null(而不是模式中指定的默认值“ageless-wonder”):

NAME    AGE
john    null
doe     null
jack    100
jill    101

I understand that I can coalesce the values to default in the stream definition, but is there a way to have that field populate based on the schema already provided?我知道我可以将流定义中的值合并为默认值,但是有没有办法根据已经提供的模式填充该字段?

Ksql uses the schema ID that was originally sent with the data, rather than the latest, to deserialize the record and build each row. Ksql 使用最初与数据一起发送的模式 ID,而不是最新的,来反序列化记录并构建每一行。 You'd need to define your first schema with a default age, then send without.您需要使用默认年龄定义您的第一个模式,然后在没有默认年龄的情况下发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM