简体   繁体   English

在MongoSink - alpakka mongo连接器响应后,向kafka消费者致敬

[英]Commit to kafka consumer after response from MongoSink - alpakka mongo connector

I am using alpakka connector to consume packets from Kafka and insert them into Mongo db. 我正在使用alpakka连接器来使用来自Kafka的数据包并将它们插入到Mongo数据库中。 I was trying to commit the offset after getting response from Mongo db but couldn't find anything regarding the same. 我在收到Mongo db的响应之后试图提交偏移但是找不到任何相同的内容。 How can I make sure that offset will be committed only after the packet successfully inserted to the Mongodb? 如何确保只有在数据包成功插入Mongodb后才会提交偏移量?

I am using Consumer.CommittableSource as a source and MongoSink as a sink and running the stream using RunnableGraph. 我使用Consumer.CommittableSource作为源,使用MongoSink作为接收器并使用RunnableGraph运行流。 Please refer the code for more clarification. 请参阅代码以获得更多说明。

Source: 资源:

    public Source<ConsumerMessage.CommittableMessage<String, String>, Consumer.Control> source() {
        return Consumer.committableSource(consumerSettings, subscription);
    }

Flow: 流:

   public Flow<ConsumerMessage.CommittableMessage, String, NotUsed> transformation() {
        return Flow.of(ConsumerMessage.CommittableMessage.class).map(i -> i.record().value().toString());
    }

Sink: 下沉:

    public Sink<String, CompletionStage<Done>> sink() {
        return MongoSink.insertOne(mongoCollection);
    }

Graph: 图形:

        RunnableGraph graph = RunnableGraph.fromGraph(GraphDSL.create(sink(), (builder, s) -> {
            builder.from(builder.add(source()).out()).via(builder.add(transformation())).to(s);
            return ClosedShape.getInstance();
        }));
        graph.run(ActorMaterializer.create(actorSystem));

EDIT : 编辑

Using PassThroughFlow, insertion to Mongo is working and it is not giving any exception or error but still not able to commit the packet. 使用PassThroughFlow,插入Mongo正在工作,它没有给出任何异常或错误,但仍然无法提交数据包。 transformationCommit() function has never been called. 从未调用过transformationCommit()函数。

Updated Flow : 更新流程:

    public Flow<String, String, NotUsed> transformationMongo() {
        LOGGER.info("Insert into Mongo");
        return  MongoFlow.insertOne(connection.getDbConnection());
    }
public Flow<ConsumerMessage.CommittableMessage, ConsumerMessage.CommittableOffset, NotUsed> transformationCommit() {
        return Flow.of(ConsumerMessage.CommittableMessage.class).map(i -> i.committableOffset());
    }

Sink: 下沉:

    public Sink<ConsumerMessage.CommittableOffset, CompletionStage<Done>> sinkCommit() {
        CommitterSettings committerSettings = CommitterSettings.create(actorSystem);
        return Committer.sink(committerSettings);
    }

PassThroughFlow: PassThroughFlow:

public class PassThroughFlow {
    public Graph<FlowShape<ConsumerMessage.CommittableMessage, ConsumerMessage.CommittableMessage>, NotUsed> apply(Flow<ConsumerMessage.CommittableMessage, String, NotUsed> flow) {
        return Flow.fromGraph(GraphDSL.create(builder -> {
            UniformFanOutShape broadcast = builder.add(Broadcast.create(2));
            FanInShape2 zip = builder.add(ZipWith.create((left, right) -> Keep.right()));
            builder.from(broadcast.out(0)).via(builder.add(flow)).toInlet(zip.in0());
            builder.from(broadcast.out(1)).toInlet(zip.in1());
            return FlowShape.apply(broadcast.in(), zip.out());
        }));
    }
}

Graph: 图形:


RunnableGraph graph = RunnableGraph.fromGraph(GraphDSL.create(sinkCommit(), (builder, s) -> {
            builder.from(builder.add(source()).out()).via(builder.add(passThroughFlow.apply(transformation().via(transformationMongo())))).via(builder.add(transformationCommit())).to(sales);
            return ClosedShape.getInstance();
        }));
        graph.run(ActorMaterializer.create(actorSystem));

//Insertion to mongo is working but still the packet not committed. transformationCommit() function has never been called.

而不是MongoSink ,使用MongoFlow ,它允许您继续流,然后您将能够提交偏移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM