Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#25 better naming #26

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package za.co.absa.kafkacase.examples.reader
import io.circe.Decoder
import java.util.Properties

object UsingsResourceHandling {
object ReaderUsingsResourceHandling {
def apply[T: Decoder](readerProps: Properties, topicName: String): Unit = {
println("Scala 3 feature")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package za.co.absa.kafkacase.examples.writer
import io.circe.Encoder
import java.util.Properties

object UsingsResourceHandling {
object WriterUsingsResourceHandling {
def apply[T: Encoder](writerProps: Properties, topicName: String, sampleMessageToWrite: T): Unit = {
println("Scala 3 feature")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import za.co.absa.kafkacase.reader.ReaderImpl
import java.util.Properties
import scala.util.Using

object UsingsResourceHandling {
object ReaderUsingsResourceHandling {
def apply[T: Decoder](readerProps: Properties, topicName: String): Unit = {
Using(new ReaderImpl[T](readerProps, topicName, neverEnding = false)) { reader =>
for (item <- reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import za.co.absa.kafkacase.writer.WriterImpl
import java.util.Properties
import scala.util.Using

object UsingsResourceHandling {
object WriterUsingsResourceHandling {
def apply[T: Encoder](writerProps: Properties, topicName: String, sampleMessageToWrite: T): Unit = {
Using(new WriterImpl[T](writerProps, topicName)) { writer =>
writer.Write("sampleMessageKey1", sampleMessageToWrite)
writer.Write("sampleMessageKey2", sampleMessageToWrite)
writer.write("sampleMessageKey1", sampleMessageToWrite)
writer.write("sampleMessageKey2", sampleMessageToWrite)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package za.co.absa.kafkacase.examples

import org.apache.kafka.clients.consumer.ConsumerConfig
import org.apache.kafka.clients.producer.ProducerConfig
import za.co.absa.kafkacase.examples.reader.{ReaderCustomResourceHandling, ReaderManualResourceHandling, ReaderUsingsResourceHandling}
import za.co.absa.kafkacase.examples.writer.{WriterCustomResourceHandling, WriterManualResourceHandling, WriterUsingsResourceHandling}
import za.co.absa.kafkacase.models.topics.EdlaChange

import java.util.{Properties, UUID}
Expand Down Expand Up @@ -57,11 +59,11 @@ object KafkaCase {


def main(args: Array[String]): Unit = {
writer.ManualResourceHandling(writerProps, topicName, sampleMessageToWrite)
writer.CustomResourceHandling(writerProps, topicName, sampleMessageToWrite)
writer.UsingsResourceHandling(writerProps, topicName, sampleMessageToWrite)
reader.ManualResourceHandling[EdlaChange](readerProps, topicName)
reader.CustomResourceHandling[EdlaChange](readerProps, topicName)
reader.UsingsResourceHandling[EdlaChange](readerProps, topicName)
WriterManualResourceHandling(writerProps, topicName, sampleMessageToWrite)
WriterCustomResourceHandling(writerProps, topicName, sampleMessageToWrite)
WriterUsingsResourceHandling(writerProps, topicName, sampleMessageToWrite)
ReaderManualResourceHandling[EdlaChange](readerProps, topicName)
ReaderCustomResourceHandling[EdlaChange](readerProps, topicName)
ReaderUsingsResourceHandling[EdlaChange](readerProps, topicName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import za.co.absa.kafkacase.reader.ReaderImpl

import java.util.Properties

object CustomResourceHandling {
object ReaderCustomResourceHandling {
def apply[T: Decoder](readerProps: Properties, topicName: String): Unit = {
withResource(new ReaderImpl[T](readerProps, topicName, neverEnding = false))(reader => {
for (item <- reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import za.co.absa.kafkacase.reader.ReaderImpl

import java.util.Properties

object ManualResourceHandling {
object ReaderManualResourceHandling {
def apply[T: Decoder](readerProps: Properties, topicName: String): Unit = {
val reader = new ReaderImpl[T](readerProps, topicName, neverEnding = false)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import za.co.absa.kafkacase.writer.WriterImpl

import java.util.Properties

object CustomResourceHandling {
object WriterCustomResourceHandling {
def apply[T: Encoder](writerProps: Properties, topicName: String, sampleMessageToWrite: T): Unit = {
withResource(new WriterImpl[T](writerProps, topicName))(writer => {
writer.Write("sampleMessageKey1", sampleMessageToWrite)
writer.Write("sampleMessageKey2", sampleMessageToWrite)
writer.write("sampleMessageKey1", sampleMessageToWrite)
writer.write("sampleMessageKey2", sampleMessageToWrite)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import za.co.absa.kafkacase.writer.WriterImpl

import java.util.Properties

object ManualResourceHandling {
object WriterManualResourceHandling {
def apply[T: Encoder](writerProps: Properties, topicName: String, sampleMessageToWrite: T): Unit = {
val writer = new WriterImpl[T](writerProps, topicName)
try {
writer.Write("sampleMessageKey1", sampleMessageToWrite)
writer.Write("sampleMessageKey2", sampleMessageToWrite)
writer.write("sampleMessageKey1", sampleMessageToWrite)
writer.write("sampleMessageKey2", sampleMessageToWrite)
} finally {
writer.close()
}
Expand Down
10 changes: 5 additions & 5 deletions writer/src/main/scala/za/co/absa/kafkacase/writer/Writer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package za.co.absa.kafkacase.writer

trait Writer[TType] extends AutoCloseable {
def Write(key: String, value: TType): Unit
def Flush(): Unit
def write(key: String, value: TType): Unit
def flush(): Unit

def WriteSync(key: String, value: TType): Unit = {
Write(key, value)
Flush()
def writeSync(key: String, value: TType): Unit = {
write(key, value)
flush()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import java.util.Properties
class WriterImpl[TType: Encoder](props: Properties, topic: String) extends Writer[TType] {
private val producer = new KafkaProducer[String, String](props)

def Write(key: String, value: TType): Unit = {
def write(key: String, value: TType): Unit = {
log.info(f"Writing: $key => $value")
producer.send(new ProducerRecord[String, String](topic, key, value.asJson.noSpaces))
}

def Flush(): Unit = producer.flush()
def flush(): Unit = producer.flush()

override def close(): Unit = producer.close()
}
Expand Down