Using KafkaActor Producer to push Messages
See original GitHub issueI can see some documentation on how to use the KafkaActor Consumer, but there is very little information on how to use the KafkaActor Producer. I want to push a message that is contained as a case class into a Topic. For this purpose (Just to see how it might work), I just copied one of the tests and tried to run it:
"KafkaProducerActor" should "write a given batch to Kafka 1" in {
case class MyDummy(s: String, i: Int)
val topic = randomString
val probe = TestProbe()
val producer = system.actorOf(KafkaProducerActor.props(producerConf))
val batch: Seq[ProducerRecord[String, MyDummy]] = Seq(
KafkaProducerRecord(topic, MyDummy("foo", 1)),
KafkaProducerRecord(topic, "key", MyDummy("value", 2)),
KafkaProducerRecord(topic, MyDummy("bar", 3)))
val message = ProducerRecords(batch, Some('response))
probe.send(producer, message)
probe.expectMsg('response)
val results = consumeFromTopic(topic, 3, 10000)
results.head shouldEqual ((None, "foo"))
results(1) shouldEqual ((Some("key"), "value"))
results(2) shouldEqual ((None, "bar"))
}
So as it can be seen that I want to push the MyDummy into the Kafka topic, but I get the following errors:
Error:(60, 34) No TypeTag available for MyDummy
val message = ProducerRecords(batch, Some('response))
Error:(60, 34) not enough arguments for method apply: (implicit evidence$13: reflect.runtime.universe.TypeTag[String], implicit evidence$14: reflect.runtime.universe.TypeTag[MyDummy])cakesolutions.kafka.akka.ProducerRecords[String,MyDummy] in object ProducerRecords.
Unspecified value parameter evidence$14.
val message = ProducerRecords(batch, Some('response))
I’m just not sure how to get past the compiler. Any help?
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Messages from and to Kafka • Akka Projection - Documentation
A typical source for Projections is messages from Kafka. Akka Projections supports integration with Kafka using Alpakka Kafka. The KafkaSourceProvider ...
Read more >Communicating with kafka using akka actors | dive-in-scala
we need a producer to place messages on our queue. we will use KafkaProducer which is a thin, lightweight wrapper for the Kafka...
Read more >Section 7: Projection publishing to Kafka :: Akka Guide
On this page you will learn how to: send messages to a Kafka topic from a Projection. consume messages from a Kafka topic ......
Read more >Documentation - Apache Kafka
Kafka comes with a command line client that will take input from a file or from standard input and send it out as...
Read more >akka/alpakka-kafka - Gitter
If you send the message to the actor and commit, you get at-most-once, ... I'm using Alpakka AMQP source and I only ack...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The TypeTaggedTrait is there to add the type tag derived from the extending class as a value. This allows preserving the type information in the runtime that would be otherwise lost due to type erasure. This is especially nice when combined with Akka actors where each message loses its type information. The problem that TypeTaggedTrait has is that it doesn’t deal very well with inheritance: the cast operation matches on exact type rather than being aware of the type hierarchy.
Here’s some additional reading on the subject, if you’re interested: https://www.cakesolutions.net/teamblogs/ways-to-pattern-match-generic-types-in-scala
It’s there so that the actors that receive those records (in this case the producer actor), can pattern match on only
ProducerRecords[MyKey, MyString]
and not on any otherProducerRecords
with different type parameters.The pattern match for the producer actor is effectively a function from
Any => Option[ProducerRecords[MyKey, MyString]
whereMyKey
andMyString
are any custom type parameters. If the input is of typeTypeTaggedTrait
, then we know that it has acast
method. We can use that cast method to attempt to cast the value toProducerRecords[MyKey, MyString]
, which will work if only if the value is of typeProducerRecords
and it has theMyKey
andMyString
type parameters. Otherwise it will returnNone
, i.e. it’s not a match. See TypeTaggedExtractor for the implementation details.