如何在SourceConnector中获取OffsetReader,而不是在SourceConnectorTask中?

时间:2017-07-16 23:11:36

标签: apache-kafka apache-kafka-connect

我需要在SourceConnector.taskConfigs()方法中读取偏移量,有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

这张票和PR

  

https://issues.apache.org/jira/browse/KAFKA-4794

现在,我设法使用反射(JOOR)来获取offsetReader:

private OffsetStorageReader getOffsetStorageReader() {

    try {
        Object innerContext = on(context).get("this$0");
        Object ctx = on(innerContext).get("ctx");

        Object herder = on(ctx).get("herder");
        String connectorName = on(ctx).get("connectorName");
        Object worker = on(herder).get("worker");
        Object internalKeyConverter = on(worker).get("internalKeyConverter");
        Object internalValueConverter = on(worker).get("internalValueConverter");
        Object offsetBackingStore = on(worker).get("offsetBackingStore");

        return (OffsetStorageReader)
            on(Class.forName("org.apache.kafka.connect.storage.OffsetStorageReaderImpl"))
                .create(offsetBackingStore, connectorName, internalKeyConverter, internalValueConverter)
                .get();

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}