|
| 1 | +/** |
| 2 | + * Copyright (C) 2018 Lightbend Inc. <https://www.lightbend.com> |
| 3 | + */ |
| 4 | + |
| 5 | +package com.lightbend.kafka.scala.streams |
| 6 | + |
| 7 | +import java.util.Properties |
| 8 | +import java.util.regex.Pattern |
| 9 | + |
| 10 | +import com.lightbend.kafka.scala.server.{KafkaLocalServer, MessageListener, MessageSender, RecordProcessorTrait} |
| 11 | +import minitest.TestSuite |
| 12 | +import org.apache.kafka.clients.consumer.ConsumerRecord |
| 13 | +import org.apache.kafka.common.serialization._ |
| 14 | +import org.apache.kafka.streams.{KafkaStreams, KeyValue, StreamsConfig} |
| 15 | +import ImplicitConversions._ |
| 16 | +import com.typesafe.scalalogging.LazyLogging |
| 17 | + |
| 18 | +object KafkaStreamsMergeTest extends TestSuite[KafkaLocalServer] with WordCountMergeTestData with LazyLogging { |
| 19 | + |
| 20 | + override def setup(): KafkaLocalServer = { |
| 21 | + val s = KafkaLocalServer(cleanOnStart = true, Some(localStateDir)) |
| 22 | + s.start() |
| 23 | + s |
| 24 | + } |
| 25 | + |
| 26 | + override def tearDown(server: KafkaLocalServer): Unit = { |
| 27 | + server.stop() |
| 28 | + } |
| 29 | + |
| 30 | + test("should count words") { server => |
| 31 | + |
| 32 | + server.createTopic(inputTopic1) |
| 33 | + server.createTopic(inputTopic2) |
| 34 | + server.createTopic(outputTopic) |
| 35 | + |
| 36 | + // |
| 37 | + // Step 1: Configure and start the processor topology. |
| 38 | + // |
| 39 | + import DefaultSerdes._ |
| 40 | + |
| 41 | + val streamsConfiguration = new Properties() |
| 42 | + streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, s"wordcount-${scala.util.Random.nextInt(100)}") |
| 43 | + streamsConfiguration.put(StreamsConfig.CLIENT_ID_CONFIG, "wordcountgroup") |
| 44 | + |
| 45 | + streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, brokers) |
| 46 | + streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, localStateDir) |
| 47 | + |
| 48 | + val builder = new StreamsBuilderS() |
| 49 | + |
| 50 | + val textLines1 = builder.stream[String, String](inputTopic1) |
| 51 | + val textLines2 = builder.stream[String, String](inputTopic2) |
| 52 | + |
| 53 | + val textLines = textLines1.merge(textLines2) |
| 54 | + |
| 55 | + val pattern = Pattern.compile("\\W+", Pattern.UNICODE_CHARACTER_CLASS) |
| 56 | + |
| 57 | + val wordCounts: KTableS[String, Long] = |
| 58 | + textLines.flatMapValues(v => pattern.split(v.toLowerCase)) |
| 59 | + .groupBy((k, v) => v) |
| 60 | + .count() |
| 61 | + |
| 62 | + wordCounts.toStream.to(outputTopic) |
| 63 | + |
| 64 | + val streams = new KafkaStreams(builder.build(), streamsConfiguration) |
| 65 | + streams.start() |
| 66 | + |
| 67 | + // |
| 68 | + // Step 2: Produce some input data to the input topics. |
| 69 | + // |
| 70 | + val sender = MessageSender[String, String](brokers, classOf[StringSerializer].getName, classOf[StringSerializer].getName) |
| 71 | + val mvals1 = sender.batchWriteValue(inputTopic1, inputValues) |
| 72 | + val mvals2 = sender.batchWriteValue(inputTopic2, inputValues) |
| 73 | + |
| 74 | + // |
| 75 | + // Step 3: Verify the application's output data. |
| 76 | + // |
| 77 | + val listener = MessageListener(brokers, outputTopic, "wordcountgroup", |
| 78 | + classOf[StringDeserializer].getName, |
| 79 | + classOf[LongDeserializer].getName, |
| 80 | + new RecordProcessor |
| 81 | + ) |
| 82 | + |
| 83 | + val l = listener.waitUntilMinKeyValueRecordsReceived(expectedWordCounts.size, 30000) |
| 84 | + |
| 85 | + assertEquals(l.sortBy(_.key), expectedWordCounts.sortBy(_.key)) |
| 86 | + |
| 87 | + streams.close() |
| 88 | + } |
| 89 | + |
| 90 | + class RecordProcessor extends RecordProcessorTrait[String, Long] { |
| 91 | + override def processRecord(record: ConsumerRecord[String, Long]): Unit = { |
| 92 | + // logger.info(s"Get Message $record") |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +trait WordCountMergeTestData { |
| 99 | + val inputTopic1 = s"inputTopic1.${scala.util.Random.nextInt(100)}" |
| 100 | + val inputTopic2 = s"inputTopic2.${scala.util.Random.nextInt(100)}" |
| 101 | + val outputTopic = s"outputTpic.${scala.util.Random.nextInt(100)}" |
| 102 | + val brokers = "localhost:9092" |
| 103 | + val localStateDir = "local_state_data" |
| 104 | + |
| 105 | + val inputValues = List( |
| 106 | + "Hello Kafka Streams", |
| 107 | + "All streams lead to Kafka", |
| 108 | + "Join Kafka Summit", |
| 109 | + "И теперь пошли русские слова" |
| 110 | + ) |
| 111 | + |
| 112 | + val expectedWordCounts: List[KeyValue[String, Long]] = List( |
| 113 | + new KeyValue("hello", 2L), |
| 114 | + new KeyValue("all", 2L), |
| 115 | + new KeyValue("streams", 4L), |
| 116 | + new KeyValue("lead", 2L), |
| 117 | + new KeyValue("to", 2L), |
| 118 | + new KeyValue("join", 2L), |
| 119 | + new KeyValue("kafka", 6L), |
| 120 | + new KeyValue("summit", 2L), |
| 121 | + new KeyValue("и", 2L), |
| 122 | + new KeyValue("теперь", 2L), |
| 123 | + new KeyValue("пошли", 2L), |
| 124 | + new KeyValue("русские", 2L), |
| 125 | + new KeyValue("слова", 2L) |
| 126 | + ) |
| 127 | +} |
| 128 | + |
| 129 | + |
0 commit comments