|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -package za.co.absa.kafkacase.examples |
| 17 | +package za.co.absa.KafkaCase.Examples |
18 | 18 |
|
19 | 19 | import org.apache.kafka.clients.consumer.ConsumerConfig
|
20 | 20 | import org.apache.kafka.clients.producer.ProducerConfig
|
21 | 21 | import za.co.absa.kafkacase.models.topics.EdlaChange
|
| 22 | +import za.co.absa.kafkacase.models.utils.ResourceHandler.withResource |
22 | 23 | import za.co.absa.kafkacase.reader.ReaderImpl
|
23 | 24 | import za.co.absa.kafkacase.writer.WriterImpl
|
24 | 25 |
|
25 | 26 | import java.util.{Properties, UUID}
|
| 27 | +// scala3 only |
| 28 | +// import scala.util.Using |
26 | 29 |
|
27 | 30 | object KafkaCase {
|
28 |
| - private def writer_use_case(): Unit = { |
29 |
| - // 0 -> HAVE SOMETHING TO WRITE |
30 |
| - val messageToWrite = EdlaChange( |
31 |
| - app_id_snow = "N/A", |
32 |
| - data_definition_id = "TestingThis", |
33 |
| - environment = "DEV", |
34 |
| - format = "FooBar", |
35 |
| - guid = "DebugId", |
36 |
| - location = "ether", |
37 |
| - operation = EdlaChange.Operation.Create(), |
38 |
| - schema_link = "http://not.here", |
39 |
| - source_app = "ThisCode", |
40 |
| - timestamp_event = 12345 |
41 |
| - ) |
42 |
| - |
43 |
| - // 1 -> DEFINE PROPS - kafka to treat all as string |
44 |
| - val writerProps = new Properties() |
45 |
| - writerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "ZADALNRAPP00009.corp.dsarena.com:9092") |
46 |
| - writerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer") |
47 |
| - writerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer") |
48 |
| - |
49 |
| - // 2 -> MAKE WRITER |
50 |
| - val writer = new WriterImpl[EdlaChange](writerProps, "KillMePleaseTopic") |
| 31 | + // This goes from your application logic |
| 32 | + private val sampleMessageToWrite = EdlaChange( |
| 33 | + app_id_snow = "N/A", |
| 34 | + data_definition_id = "TestingThis", |
| 35 | + environment = "DEV", |
| 36 | + format = "FooBar", |
| 37 | + guid = "DebugId", |
| 38 | + location = "ether", |
| 39 | + operation = EdlaChange.Operation.Create(), |
| 40 | + schema_link = "http://not.here", |
| 41 | + source_app = "ThisCode", |
| 42 | + timestamp_event = 12345 |
| 43 | + ) |
| 44 | + |
| 45 | + // This goes from your config / domain knowledge |
| 46 | + private val topicName = "KillMePleaseTopic" |
| 47 | + |
| 48 | + // This goes from your writer configs |
| 49 | + private val writerProps = new Properties() |
| 50 | + writerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "ZADALNRAPP00009.corp.dsarena.com:9092") |
| 51 | + writerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer") |
| 52 | + writerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer") |
| 53 | + |
| 54 | + // This goes from your reader configs |
| 55 | + private val readerProps = new Properties() |
| 56 | + readerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "ZADALNRAPP00009.corp.dsarena.com:9092") |
| 57 | + readerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer") |
| 58 | + readerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer") |
| 59 | + readerProps.put(ConsumerConfig.CLIENT_ID_CONFIG, s"DebugConsumer_${UUID.randomUUID()}") |
| 60 | + readerProps.put(ConsumerConfig.GROUP_ID_CONFIG, s"DebugGroup_${UUID.randomUUID()}") |
| 61 | + readerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest") |
| 62 | + |
| 63 | + private def writer_use_case_scala2(): Unit = { |
| 64 | + val writer = new WriterImpl[EdlaChange](writerProps, topicName) |
51 | 65 | try {
|
52 |
| - // 3 -> WRITE |
53 |
| - writer.Write("sampleMessageKey", messageToWrite) |
| 66 | + writer.Write("sampleMessageKey1", sampleMessageToWrite) |
| 67 | + writer.Write("sampleMessageKey2", sampleMessageToWrite) |
54 | 68 | } finally {
|
55 |
| - // Releasing resources should be handled by using block in newer versions of scala |
56 | 69 | writer.close()
|
57 | 70 | }
|
58 | 71 | }
|
59 | 72 |
|
60 |
| - private def reader_use_case(): Unit = { |
61 |
| - // 1 -> DEFINE PROPS - kafka to treat all as string |
62 |
| - val readerProps = new Properties() |
63 |
| - readerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "ZADALNRAPP00009.corp.dsarena.com:9092") |
64 |
| - readerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer") |
65 |
| - readerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer") |
66 |
| - readerProps.put(ConsumerConfig.CLIENT_ID_CONFIG, s"DebugConsumer_${UUID.randomUUID()}") |
67 |
| - readerProps.put(ConsumerConfig.GROUP_ID_CONFIG, s"DebugGroup_${UUID.randomUUID()}") |
68 |
| - readerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest") |
69 |
| - |
70 |
| - // 2 -> MAKE READER (should be in using block for newer versions of scala) |
71 |
| - val reader = new ReaderImpl[EdlaChange](readerProps, "KillMePleaseTopic") |
| 73 | + private def writer_use_case_scala2_custom_resource_handler(): Unit = { |
| 74 | + withResource(new WriterImpl[EdlaChange](writerProps, topicName))(writer => { |
| 75 | + writer.Write("sampleMessageKey1", sampleMessageToWrite) |
| 76 | + writer.Write("sampleMessageKey2", sampleMessageToWrite) |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | +// scala3 only |
| 81 | +// private def writer_use_case_scala3(): Unit = { |
| 82 | +// Using(new WriterImpl[EdlaChange](writerProps, topicName)) { writer => |
| 83 | +// writer.Write("sampleMessageKey1", sampleMessageToWrite) |
| 84 | +// writer.Write("sampleMessageKey2", sampleMessageToWrite) |
| 85 | +// } |
| 86 | +// } |
| 87 | + |
| 88 | + private def reader_use_case_scala2(): Unit = { |
| 89 | + val reader = new ReaderImpl[EdlaChange](readerProps, topicName, neverEnding = false) |
72 | 90 | try {
|
73 | 91 | for (item <- reader)
|
74 | 92 | println(item)
|
75 | 93 | } finally {
|
76 |
| - // Releasing resources should be handled by using block in newer versions of scala |
77 | 94 | reader.close()
|
78 | 95 | }
|
79 | 96 | }
|
80 | 97 |
|
| 98 | + private def reader_use_case_scala2_custom_resource_handler(): Unit = { |
| 99 | + withResource(new ReaderImpl[EdlaChange](readerProps, topicName, neverEnding = false))(reader => { |
| 100 | + for (item <- reader) |
| 101 | + println(item) |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | +// scala3 only |
| 106 | +// private def reader_use_case_scala3(): Unit = { |
| 107 | +// Using(new ReaderImpl[EdlaChange](readerProps, topicName, neverEnding = false)) { reader => |
| 108 | +// for (item <- reader) |
| 109 | +// println(item) |
| 110 | +// } |
| 111 | +// } |
| 112 | + |
81 | 113 | def main(args: Array[String]): Unit = {
|
82 |
| - writer_use_case() |
83 |
| - reader_use_case() |
| 114 | + writer_use_case_scala2() |
| 115 | + reader_use_case_scala2() |
| 116 | + |
| 117 | + writer_use_case_scala2_custom_resource_handler() |
| 118 | + reader_use_case_scala2_custom_resource_handler() |
| 119 | + |
| 120 | +// scala3 only |
| 121 | +// writer_use_case_scala3() |
| 122 | +// reader_use_case_scala3() |
84 | 123 | }
|
85 | 124 | }
|
0 commit comments