Java examples and documentations

Hello,
Its really depressing and very difficult to find documentation and examples for Alpakka kafka / streams in java. The documentation are available in scala only or am in missing somethings ? The stack overflow questions are also in scala
Do i need to learn a new language to work with a framework? I have no option but to use Play and Akka as it is being client requirement. I am loosing days which i would would not have with other framework or even .net.
I feel really helpless here. Can some one tell where to look for information ?

Thanks

Did you notice that the documentation has Java and Scala tabs for the code examples? For example Consumer • Alpakka Kafka Documentation

We can of course not take responsibility for stackoverflow questions, but Java and Scala have equal importance in APIs and documentation of Akka.

Depending on what you are developing you might find this tutorial a good starting point for learning Akka: Implementing Microservices with Akka :: Akka Platform Guide

1 Like

Many thanks for the reply. Yes I look in the documents and java code in that as well.
Sure I will go through the guide as well.
I am trying to convert the java for my requirement but end up getting errors: For eg, following code works:
CompletionStage control =
Consumer.plainSource(consumerConfig, Subscriptions.topics(topic))
.runForeach( consumerRecord → {
System.out.println(“got record =>” + consumerRecord.value());
}, actor);

but following code does not. I know i am doing something wrong, but do not know what.

CompletionStage controlCompletionStage =
db.loadOffset()
.thenApply(
fromOffset →
Consumer
.plainSource(settings,Subscriptions.assignmentWithOffset(new TopicPartition(“movie”, 1), fromOffset))
.runForeach( consumerRecord → {
System.out.println(“got record =>” + consumerRecord.value());
}, context().system())
);

What is the difference ? But again this works:

.via(
Slick.flow(
session,
getContext().getSystem().dispatcher(),
(kafkaMessage, connection) → {
log.info(“prepared statement {}” , statement);
return statement;
}
.log(“nr-of-updated-rows”)
.runWith(Sink.ignore(), getContext().getSystem())
));

But when i change to flowWithPassThrough it is expecting some other return type in lambda and cannot compile.

When I use source, where can i see the documentation to use via, runWith, run, map, mapAsync ? Yes, documentation has sample codes using this, but I don’t exactly understand to implement (I am from .net world).

Thanks

Actually, I think scala code in this case kinda self explaining (probably more than the java one).

If you have a Flow[A], and a function with A => B, the map what you are looking for. If you have a function with A => Future[B] (I have no idea what is the java eq of the async wrapper), the mapAsync what you are looking for (or even mapAsyncUnordered if the order is don’t care). Both will return a Flow[A, B, NotUsed]. If you have an x: Flow[A, B, NotUsed], you can use Flow[A].via(x) which will also return a Flow[A, B, NotUsed].

For “terminating” or closing a flow/source you can use to or toMat (to will returns the matvalue of the upstream, and toMat will wait for a function that combines the upstream and the sink matvalues). If you have a closed graph (like Source.single(1).to(Sink.ignore )) you can run it with run. Until you run it, it is like a description of the computation pipeline, when you run it, it starts to generate/consume elements, and also returns with the matvalues. Most of the times we start a stream from the source (like Source.single(1).map(_ + 1)) for a shorthand we can write runWith(sink) instead of toMat(sink)(Keep.right).run() this is bcs most times we are interested in the Sinks matValue.

If you use intelliJ, I would recommend to “click into” (aka ctrl/command + click) functions, and read the documentation over them. Most of the times they are pretty much explains what that thing supposed to do. Also, these tools are enable a type driven approach, if you have a function signiture, you can find the needed function that will fit.

For quick references;

(From the second link, and your second example, I think this is much better than using thenApply.)

I know that the whole streams lib is a bit too much for beginners, but you should read the whole docs at least two times, not bcs you need to learn/understand all of it, but just to get the idea where you can search things if you get blocked. It seems like a big investment, but without a solid foundation, you will probably shoot yourself (with reinventing things that are already written probably better, or writing things noted as really dangerous (like cycles)).

Thank you very much for the detailed reply and sure it helps a lot. Yes, i need to read and re-read the docs to get the foundation right. Couple of microservices is in pipeline using play and akka and so, need to do the investment in understanding things better.

I use VScode without any extensions / plugins.

Thanks once again.

Why would you do that? IntelliJ is 500eur/year/user for orgs, and it is a HUGE productivity increase. (For individuals its 150eur/year, best 150 eur I spent in a long time, and also you can try out the community edition for free.) If you want to stay in vscode, there is an official list of plugins play comes with sbt so testrunner, maven and other plugins are probably not needed, but metals probably is needed. Without autocomplete and context helps I’m sure this is really hard!

Thank you very much. I have installed IntellJ and sure it helps a lot, The editor gives good insight into the api and doc. Have completed the flowWithPassThrough implementation.

Very much appreciated.