簡體   English   中英

從中間主題將KStream與KTable結合會導致異常

[英]Joining KStream with KTable from intermediate topic results in exception

我正在嘗試使用KTable加入KStream。 沒有聯接,從中間主題“按ID分配book-attribute-by-id”中讀取內容就毫無問題。

KTable的示例消息:

{key: {id: 1}
 value: {id: 1, attribute_name: "weight"}}

KStream的示例消息:

{key: {id: 1},
 value: {id: 1, book_id: 1, attribute_id: 1, value: 200}}

希望輸出到“最終匯總”主題:

{key: {id: 1}, 
 value: {book_id: 1, attribute_name: "weight", value: 200}}
{key: {id: 1}, 
 value: {book_id: 1, attribute_name: "number_of_pages", value: 450}}

這是代碼

    KStream<DefaultId, BookAttribute> bookAttributeStream = builder.stream(bookAttributeTopic, Consumed.with(defaultIdSerde, bookAttributeSerde));
    KStream<DefaultId, BookValueInt> bookValueIntStream = builder.stream(bookValueIntTopic, Consumed.with(defaultIdSerde, bookValueIntSerde));

    bookAttributeStream
        .selectKey((k, v) -> k.getId())
        .to("book-attribute-by-id", Produced.with(Serdes.Integer(), bookAttributeSerde));

    KTable<Integer, BookAttribute> bookAttributeByIdTable = builder.table("book-attribute-by-id", Consumed.with(Serdes.Integer(), bookAttributeSerde));

    // when the snippet below is commented out, consuming "book-attribute-by-id" works. 
    bookValueIntStream
        .selectKey((k, v) -> v.getAttribute_id())
        .join(bookAttributeByIdTable, (intValue, attribute) -> {
                System.out.println("intValue: " + intValue);
                System.out.println("attribute: " + attribute);
                return new BookAttributeValue(intValue, attribute);
            });

加入KStream和KTable時發生異常:

線程“ xxx-StreamThread-1”中的異常org.apache.kafka.streams.errors.TopologyBuilderException:無效的拓撲構建:流線程[xxx-StreamThread-1]找不到主題:org的book-attribute-by-id。 apache.kafka.streams.processor.internals.StreamPartitionAssignor $ CopartitionedTopicsValidator.validate(StreamPartitionAssignor.java:792)

我假設您正在使用kafka-streams 1.0.0

問題是您必須為流創建輸入主題。

在您的情況下,主題是: book-attribute-by-id和作為變量值的主題: bookAttributeTopicbookValueIntTopic

對於聯接,Kafka Streams必須確保聯接主題中的分區數相等。 當嘗試獲取主題的元數據book-attribute-by-id時,拋出異常。

在運行您的應用程序之前,您必須手動創建book-attribute-by-id分配的book-attribute-by-id

在較新版本的kafka-streams中,在驗證分區數量之前先檢查主題的存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM