Kafka jdbc connect sink:是否可以将pk.fields用于值和键中的字段?

时间:2020-05-14 20:27:36

标签: apache-kafka apache-kafka-connect confluent-platform

我遇到的问题是,当jdbc接收器连接器使用kafka消息时,写入db时的关键变量为null。

但是,当我直接通过kafka-avro-consumer进行消费时,因为使用了以下配置,所以可以看到键和值变量及其值:--property print.key = true。

询问:是否有必要确保jdbc连接器正在处理消息密钥变量值?

控制台kafka-avro配置

/opt/confluent-5.4.1/bin/kafka-avro-console-consumer \
    --bootstrap-server "localhost:9092" \
    --topic equipmentidentifier.persist \
    --property parse.key=true \
    --property key.separator=~ \
    --property print.key=true \
    --property schema.registry.url="http://localhost:8081" \
    --property key.schema=[$KEY_SCHEMA] \
    --property value.schema=[$IDENTIFIER_SCHEMA,$VALUE_SCHEMA]

错误:

org.apache.kafka.connect.errors.RetriableException: java.sql.SQLException: java.sql.BatchUpdateException: Batch entry 0 INSERT INTO "assignment_table" ("created_date","custome
r","id_type","id_value") VALUES('1970-01-01 03:25:44.567+00'::timestamp,123,'BILL_OF_LADING','BOL-123') was aborted: ERROR: null value in column "equipment_ide
ntifier_type" violates not-null constraint
  Detail: Failing row contains (null, null, null, null, 1970-01-01 03:25:44.567, 123, id, 56).  Call getNextException to see other errors in the batch.
org.postgresql.util.PSQLException: ERROR: null value in column "equipment_identifier_type" violates not-null constraint

接收器配置:

task.max=1
topic=assignment
connect.class=io.confluet.connect.jdbc.JdbcSinkConnector

connection.url=jdbc:postgresql://localhost:5432/db
connection.user=test
connection.password=test

table.name.format=assignment_table
auto.create=false
insert.mode=insert
pk.fields=customer,equip_Type,equip_Value,id_Type,id_Value,cpId

transforms=flatten

transforms.flattenKey.type=org.apache.kafka.connect.transforms.Flatten$Key
transforms.flattenKey.delimiter=_

transforms.flattenKey.type=org.apache.kafka.connect.transforms.Flatten$Value
transforms.flattenKey.delimiter=_

卡夫卡密钥:

{
  "assignmentKey": {
    "cpId": {
      "long": 1001
    },
    "equip": {
      "Identifier": {
        "type": "eq",
        "value": "eq_45"
      }
    },
    "vendorId": {
      "string": "vendor"
    }
  }
}

卡夫卡值:


{
  "assigmentValue": {
    "id": {
      "Identifier": {
        "type": "id",
        "value": "56"
      }
    },
    "timestamp": {
      "long": 1234456756
    },
    "customer": {
      "long": 123
    }
  }
}

1 个答案:

答案 0 :(得分:1)

您需要告诉连接器使用键中的字段,因为默认情况下不会。

pk.mode=record_key

不过您需要使用 值中的字段,而不是同时使用当前配置中的字段:

pk.fields=customer,equip_Type,equip_Value,id_Type,id_Value,cpId

如果设置pk.mode=record_key,则pk.fields将引用消息键中的字段。

参考:https://docs.confluent.io/current/connect/kafka-connect-jdbc/sink-connector/sink_config_options.html#sink-pk-config-options

另请参阅https://rmoff.dev/kafka-jdbc-video