使用kafka主题创建表时,表需要主键

时间:2020-07-14 05:55:19

标签: apache-kafka ksqldb

我有一个mysql表,如下所示: enter image description here 我使用kafka连接器将此表添加到kafka主题:

ksql> CREATE SOURCE CONNECTOR SOURCE_MYSQL_01 WITH (
    'connector.class' = 'io.debezium.connector.mysql.MySqlConnector',
    'database.hostname' = 'mysql',
    'database.port' = '3306',
    'database.user' = 'debezium',
    'database.password' = 'dbz',
    'database.server.id' = '42',
    'database.server.name' = 'asgard',
    'table.whitelist' = 'demo.customers',
    'database.history.kafka.bootstrap.servers' = 'kafka:29092',
    'database.history.kafka.topic' = 'dbhistory.demo' ,
    'include.schema.changes' = 'false',
    'transforms'= 'unwrap,extractkey',
    'transforms.unwrap.type'= 'io.debezium.transforms.ExtractNewRecordState',
    'transforms.extractkey.type'= 'org.apache.kafka.connect.transforms.ExtractField$Key',
    'transforms.extractkey.field'= 'id',
    'key.converter'= 'org.apache.kafka.connect.storage.StringConverter',
    'value.converter'= 'io.confluent.connect.avro.AvroConverter',
    'value.converter.schema.registry.url'= 'http://schema-registry:8081'
    );

然后我要使用它来基于它创建一个表:

CREATE TABLE  CUSTOMERS WITH (KAFKA_TOPIC='asgard.demo.CUSTOMERS', VALUE_FORMAT='AVRO');

然后我收到此错误:

Tables require a PRIMARY KEY. Please define the PRIMARY KEY.
Use a partial schema to define the primary key and still load the value columns from the Schema Registry, for example:
        CREATE TABLE CUSTOMERS (ID INT PRIMARY KEY) WITH (...);

当我按照建议更改时:

CREATE TABLE CUSTOMERS (id int primary key) WITH (KAFKA_TOPIC='asgard.demo.CUSTOMERS', VALUE_FORMAT='AVRO');

我收到此错误:

Duplicate column names: `ID`

进行了一些搜索,但仍然停留在这里。这有什么问题,我该如何解决?谢谢

1 个答案:

答案 0 :(得分:1)

问题在于,您为PK列分配的名称与从架构注册表中加载的Avro架构中的列名称冲突。

您可以根据自己的喜好为键列命名,因为列名不会在任何地方持久存在,因此只需为其命名就不会冲突,例如customer_id

CREATE TABLE CUSTOMERS (customer_id int primary key) WITH (KAFKA_TOPIC='asgard.demo.CUSTOMERS', VALUE_FORMAT='AVRO');

还注意到您拥有:

'key.converter'='org.apache.kafka.connect.storage.StringConverter'

我相信它将把密钥转换为STRING。不确定这是故意的。 ksqlDB也将与INT PK一起使用...