kafka connect转换RegExRouter退出,出现不可恢复的异常

时间:2018-11-15 23:35:52

标签: amazon-s3 apache-kafka apache-kafka-connect confluent minio

我制作了一个kafka管道,将sqlserver表复制到s3

在接收器期间,我正在尝试使用regexrouter函数转换主题名称并添加前缀:

    "transforms":"dropPrefix",      
    "transforms.dropPrefix.type":"org.apache.kafka.connect.transforms.RegexRouter",  
    "transforms.dropPrefix.regex":"SQLSERVER-TEST-(.*)",  
    "transforms.dropPrefix.replacement":"$1"

接收器失败,并显示以下消息:

org.apache.kafka.connect.errors.ConnectException: Exiting WorkerSinkTask due to unrecoverable exception.
at org.apache.kafka.connect.runtime.WorkerSinkTask.deliverMessages(WorkerSinkTask.java:586)
at org.apache.kafka.connect.runtime.WorkerSinkTask.poll(WorkerSinkTask.java:322)
at org.apache.kafka.connect.runtime.WorkerSinkTask.iteration(WorkerSinkTask.java:225)
at org.apache.kafka.connect.runtime.WorkerSinkTask.execute(WorkerSinkTask.java:193)
at org.apache.kafka.connect.runtime.WorkerTask.doRun(WorkerTask.java:175)
at org.apache.kafka.connect.runtime.WorkerTask.run(WorkerTask.java:219)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at io.confluent.connect.s3.S3SinkTask.put(S3SinkTask.java:188)
    at org.apache.kafka.connect.runtime.WorkerSinkTask.deliverMessages(WorkerSinkTask.java:564)
    ... 10 more

如果我删除了转换,则管道可以正常工作

问题可以通过以下docker-compose复制:

version: '2'
services:

  smtproblem-zookeeper:
    image: zookeeper
    container_name: smtproblem-zookeeper
    ports:
      - "2181:2181"

  smtproblem-kafka:
    image: confluentinc/cp-kafka:5.0.0
    container_name: smtproblem-kafka
    ports:
      - "9092:9092"
    links:
      - smtproblem-zookeeper
      - smtproblem-minio
    environment:
      KAFKA_ADVERTISED_HOST_NAME : localhost
      KAFKA_ZOOKEEPER_CONNECT: smtproblem-zookeeper:2181/kafka
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://smtproblem-kafka:9092
      KAFKA_CREATE_TOPICS: "_schemas:3:1:compact"
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

  smtproblem-schema_registry:
    image: confluentinc/cp-schema-registry:5.0.0
    container_name: smtproblem-schema-registry
    ports:
      - "8081:8081"
    links:
      - smtproblem-kafka
      - smtproblem-zookeeper
    environment:
      SCHEMA_REGISTRY_HOST_NAME: http://smtproblem-schema_registry:8081
      SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: PLAINTEXT://smtproblem-kafka:9092
      SCHEMA_REGISTRY_GROUP_ID: schema_group

  smtproblem-kafka-connect:
    image: confluentinc/cp-kafka-connect:5.0.0
    container_name: smtproblem-kafka-connect
    command: bash -c "wget -P /usr/share/java/kafka-connect-jdbc http://central.maven.org/maven2/com/microsoft/sqlserver/mssql-jdbc/6.4.0.jre8/mssql-jdbc-6.4.0.jre8.jar && /etc/confluent/docker/run"
    ports:
      - "8083:8083"
    links:
      - smtproblem-zookeeper
      - smtproblem-kafka
      - smtproblem-schema_registry
      - smtproblem-minio
    environment:
      CONNECT_BOOTSTRAP_SERVERS: smtproblem-kafka:9092
      CONNECT_REST_PORT: 8083
      CONNECT_GROUP_ID: "connect_group"
      CONNECT_OFFSET_FLUSH_INTERVAL_MS: 1000
      CONNECT_CONFIG_STORAGE_TOPIC: "connect_config"
      CONNECT_OFFSET_STORAGE_TOPIC: "connect_offsets"
      CONNECT_STATUS_STORAGE_TOPIC: "connect_status"

      CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR: 1
      CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR: 1
      CONNECT_STATUS_STORAGE_REPLICATION_FACTOR: 1

      CONNECT_KEY_CONVERTER: "io.confluent.connect.avro.AvroConverter"
      CONNECT_VALUE_CONVERTER: "io.confluent.connect.avro.AvroConverter"

      CONNECT_KEY_CONVERTER_SCHEMA_REGISTRY_URL: "http://smtproblem-schema_registry:8081"
      CONNECT_VALUE_CONVERTER_SCHEMA_REGISTRY_URL: "http://smtproblem-schema_registry:8081"

      CONNECT_INTERNAL_KEY_CONVERTER: "org.apache.kafka.connect.json.JsonConverter"
      CONNECT_INTERNAL_VALUE_CONVERTER: "org.apache.kafka.connect.json.JsonConverter"

      CONNECT_REST_ADVERTISED_HOST_NAME: "smtproblem-kafka_connect"

      CONNECT_LOG4J_ROOT_LOGLEVEL: INFO
      CONNECT_LOG4J_LOGGERS: org.reflections=ERROR
      CONNECT_PLUGIN_PATH: "/usr/share/java"

      AWS_ACCESS_KEY_ID: localKey
      AWS_SECRET_ACCESS_KEY: localSecret

  smtproblem-minio:
    image: minio/minio:edge
    container_name: smtproblem-minio
    ports:
      - "9000:9000"
    entrypoint: sh
    command: -c 'mkdir -p /data/datalake && minio server /data'
    environment:
      MINIO_ACCESS_KEY: localKey
      MINIO_SECRET_KEY: localSecret
    volumes:
      - "./minioData:/data"

  smtproblem-sqlserver:
    image: microsoft/mssql-server-linux:2017-GA
    container_name: smtproblem-sqlserver
    environment:
      ACCEPT_EULA: "Y"
      SA_PASSWORD: "Azertyu&"
    ports:
      - "1433:1433"

在sqlserver容器中创建数据库:

$ sudo docker exec -it smtproblem-sqlserver bash
# /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Azertyu&'

创建一个测试数据库:

create database TEST
GO
use TEST
GO
CREATE TABLE TABLE_TEST (id INT, name NVARCHAR(50), quantity INT, cbMarq INT NOT NULL IDENTITY(1,1), cbModification smalldatetime DEFAULT (getdate()))
GO
INSERT INTO TABLE_TEST VALUES (1, 'banana', 150, 1); INSERT INTO TABLE_TEST VALUES (2, 'orange', 154, 2);
GO

exit
exit

创建源连接器:

curl -X PUT http://localhost:8083/connectors/sqlserver-TEST-source-bulk/config -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{
"connector.class": "io.confluent.connect.jdbc.JdbcSourceConnector",
"connection.password": "Azertyu&",
"validate.non.null": "false",
"tasks.max": "3",
"table.whitelist": "TABLE_TEST",
"mode": "bulk",
"topic.prefix": "SQLSERVER-TEST-",
"connection.user": "SA",
"connection.url": "jdbc:sqlserver://smtproblem-sqlserver:1433;database=TEST"
}'

创建接收器连接器:

curl -X PUT http://localhost:8083/connectors/sqlserver-TEST-sink/config -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{
"topics": "SQLSERVER-TEST-TABLE_TEST",
"topics.dir": "TABLE_TEST",
"s3.part.size": 5242880,
"storage.class": "io.confluent.connect.s3.storage.S3Storage",
"tasks.max": 3,
"schema.compatibility": "NONE",
"s3.region": "us-east-1",
"schema.generator.class": "io.confluent.connect.storage.hive.schema.DefaultSchemaGenerator",
"connector.class": "io.confluent.connect.s3.S3SinkConnector",
"partitioner.class": "io.confluent.connect.storage.partitioner.DefaultPartitioner",
"format.class": "io.confluent.connect.s3.format.avro.AvroFormat",
"s3.bucket.name": "datalake",
"store.url": "http://smtproblem-minio:9000",
"flush.size": 1,
"transforms":"dropPrefix",      
"transforms.dropPrefix.type":"org.apache.kafka.connect.transforms.RegexRouter",  
"transforms.dropPrefix.regex":"SQLSERVER-TEST-(.*)",  
"transforms.dropPrefix.replacement":"$1"
}'

错误可以在Kafka connect UI中显示,或通过curl状态命令显示:

curl -X GET http://localhost:8083/connectors/sqlserver-TEST-sink/status

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

因此,如果我们进行调试,我们可以看到它正在尝试做什么...

enter image description here

有一个具有原始主题名称(SQLSERVER_TEST_TABLE_TEST-0)的HashMap,并且已经应用​​了转换(TABLE-TEST-0),因此,如果我们查找“新”主题名称,它将找不到S3 TopicPartition的作者。

因此,映射返回null,随后的.buffer(record)引发NPE。


在此之前,我有一个类似的用例-在单个S3路径中写入多个主题,最后我不得不编写一个自定义分区程序,例如class MyPartitioner extends DefaultPartitioner

如果使用类似这样的自定义代码构建JAR,请将其放在usr/share/java/kafka-connect-storage-common下,然后编辑partitioner.class的连接器配置,它应该可以正常工作。

我真的不确定这是否是一个“ bug”,因为备份调用堆栈,在声明topicPartitionWriters时无法获得对正则表达式转换的引用带有源主题名称。

如果有的话,存储连接器配置应允许单独的regex转换,该转换可以编辑encodedPartition(写入文件的路径)