![](/img/trans.png)
[英]Create KSQL table with ROWKEY same as Kafka topic message key
[英]ksql - CREATE TABLE results in table with null values even though kafka topic is populated
使用 ksqlDB,我創建了一個帶有自定義查詢的 JDBC 連接器。 然后,從生成的 kafka 主題中,我創建了一個表。 但是,從表中選擇只返回 PRIMARY KEY 的數據,而返回 null 的所有其他值。 我正在連接的 postgres 數據庫的銷售表不斷更新新數據,我正在嘗試使用 ksql 對 stream 進行更新。
ksql> CREATE SOURCE CONNECTOR con WITH (
'connector.class' ='io.confluent.connect.jdbc.JdbcSourceConnector',
'connection.url' = '....',
'topic.prefix' = 'sales',
...
'key' = 'id',
'query' = 'SELECT id, time, price FROM sales');
Message
Created connector CON
ksql> print sales limit 1;
Key format: HOPPING(KAFKA_STRING) or TUMBLING(KAFKA_STRING) or KAFKA_STRING
Value format: JSON or KAFKA_STRING
rowtime: 2020/11/30 09:07:55.109 Z, key: [123], value: {"schema":{"type":"struct","fields":[{"type":"string","optional":alse,"field":"id"},{"type":"int64","optional":true,"field":"time"},{"type":"float","optional":true,"field":"price"}],"optional":false},"payload":{"id":"123","time":1,"price":10.0}}
Topic printing ceased
ksql> CREATE TABLE sales_table (id VARCHAR PRIMARY KEY, time INT, price DOUBLE) WITH (kafka_topic='sales', partitions=1, value_format='JSON');
Message
Table created
ksql> SELECT * FROM sales_table EMIT CHANGES LIMIT 1;
+-----+-----+-----+
|ID |TIME |PRICE|
+-----+-----+-----+
|123 |null |null |
Limit Reached
Query terminated
如您所見,kafka 主題在時間和價格字段中具有適當值的條目。 但是,當基於該主題創建表時,從表中選擇會產生 null 時間和價格字段。 只有 id(即 PRIMARY KEY 列)被正確打印。
知道為什么會這樣嗎?
You're using the org.apache.kafka.connect.json.JsonConverter
converter in your connector with schemas.enable=true
, so your schema is not (id VARCHAR PRIMARY KEY, time INT, price DOUBLE)
, and thus you get NULL價值觀。
Better is to use io.confluent.connect.avro.AvroConverter
(or Protobuf, or JSON Schema) in your source connector, because then you don't even have to type in the schema for your CREATE STREAM
, you just have
CREATE TABLE sales_table WITH (kafka_topic='sales', value_format='AVRO');
您可以這樣指定替代轉換器:
CREATE SOURCE CONNECTOR SOURCE_01 WITH (
…
'key.converter'= 'org.apache.kafka.connect.storage.StringConverter',
'value.converter'= 'io.confluent.connect.avro.AvroConverter',
'value.converter.schema.registry.url'= 'http://schema-registry:8081'
);
但是,如果您必須使用 JSON,請在源連接器中禁用架構:
CREATE SOURCE CONNECTOR SOURCE_01 WITH (
…
'value.converter.schemas.enable'= 'false'
);
參考: https://www.confluent.io/blog/kafka-connect-deep-dive-converters-serialization-explained
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.