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

#24 Add once off Reader and Writer function #27

Merged
merged 3 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 @@ -18,8 +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.examples.reader.{ReaderCustomResourceHandling, ReaderManualResourceHandling, ReaderReadOnce, ReaderUsingsResourceHandling}
import za.co.absa.kafkacase.examples.writer.{WriterCustomResourceHandling, WriterManualResourceHandling, WriterUsingsResourceHandling, WriterWriteOnce}
import za.co.absa.kafkacase.models.topics.EdlaChange

import java.util.{Properties, UUID}
Expand Down Expand Up @@ -62,8 +62,10 @@ object KafkaCase {
WriterManualResourceHandling(writerProps, topicName, sampleMessageToWrite)
WriterCustomResourceHandling(writerProps, topicName, sampleMessageToWrite)
WriterUsingsResourceHandling(writerProps, topicName, sampleMessageToWrite)
WriterWriteOnce(writerProps, topicName, sampleMessageToWrite)
ReaderManualResourceHandling[EdlaChange](readerProps, topicName)
ReaderCustomResourceHandling[EdlaChange](readerProps, topicName)
ReaderUsingsResourceHandling[EdlaChange](readerProps, topicName)
ReaderReadOnce[EdlaChange](readerProps, topicName)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.kafkacase.examples.reader

import io.circe.Decoder
import za.co.absa.kafkacase.reader.Reader

import java.util.Properties

object ReaderReadOnce {
def apply[T: Decoder](readerProps: Properties, topicName: String): Unit =
Reader.readOnce[T](readerProps, topicName, println)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.kafkacase.examples.writer

import io.circe.Encoder
import za.co.absa.kafkacase.writer.Writer

import java.util.Properties

object WriterWriteOnce {
def apply[T: Encoder](writerProps: Properties, topicName: String, sampleMessageToWrite: T): Unit =
Writer.writeOnce(writerProps, topicName, "sampleKey", sampleMessageToWrite)
}
16 changes: 16 additions & 0 deletions reader/src/main/scala/za/co/absa/kafkacase/reader/Reader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,20 @@

package za.co.absa.kafkacase.reader

import io.circe.Decoder

import java.util.Properties

trait Reader[TType] extends Iterator[(String, Either[String, TType])] with AutoCloseable

object Reader {
def readOnce[T: Decoder](readerProps: Properties, topicName: String, work: ((String, Either[String, T])) => Unit): Unit = {
val reader = new ReaderImpl[T](readerProps, topicName, neverEnding = false)
try {
for (item <- reader)
work(item)
} finally {
reader.close()
}
}
}
15 changes: 15 additions & 0 deletions writer/src/main/scala/za/co/absa/kafkacase/writer/Writer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package za.co.absa.kafkacase.writer

import io.circe.Encoder

import java.util.Properties

trait Writer[TType] extends AutoCloseable {
def write(key: String, value: TType): Unit
def flush(): Unit
Expand All @@ -25,3 +29,14 @@ trait Writer[TType] extends AutoCloseable {
flush()
}
}

object Writer {
def writeOnce[T: Encoder](writerProps: Properties, topicName: String, messageKey: String, sampleMessageToWrite: T): Unit = {
val writer = new WriterImpl[T](writerProps, topicName)
try {
writer.write(messageKey, sampleMessageToWrite)
} finally {
writer.close()
}
}
}