|  | 
| 17 | 17 | 
 | 
| 18 | 18 | package org.apache.spark.examples.streaming | 
| 19 | 19 | 
 | 
| 20 |  | -import java.util.Properties | 
|  | 20 | +import java.util.HashMap | 
| 21 | 21 | 
 | 
| 22 |  | -import kafka.producer._ | 
|  | 22 | +import org.apache.kafka.clients.producer.{ProducerConfig, KafkaProducer, ProducerRecord} | 
| 23 | 23 | 
 | 
| 24 | 24 | import org.apache.spark.streaming._ | 
| 25 | 25 | import org.apache.spark.streaming.kafka._ | 
| @@ -77,23 +77,25 @@ object KafkaWordCountProducer { | 
| 77 | 77 |     val Array(brokers, topic, messagesPerSec, wordsPerMessage) = args | 
| 78 | 78 | 
 | 
| 79 | 79 |     // Zookeeper connection properties | 
| 80 |  | -    val props = new Properties() | 
| 81 |  | -    props.put("metadata.broker.list", brokers) | 
| 82 |  | -    props.put("serializer.class", "kafka.serializer.StringEncoder") | 
|  | 80 | +    val props = new HashMap[String, Object]() | 
|  | 81 | +    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers) | 
|  | 82 | +    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, | 
|  | 83 | +      "org.apache.kafka.common.serialization.StringSerializer") | 
|  | 84 | +    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, | 
|  | 85 | +      "org.apache.kafka.common.serialization.StringSerializer") | 
| 83 | 86 | 
 | 
| 84 |  | -    val config = new ProducerConfig(props) | 
| 85 |  | -    val producer = new Producer[String, String](config) | 
|  | 87 | +    val producer = new KafkaProducer[String, String](props) | 
| 86 | 88 | 
 | 
| 87 | 89 |     // Send some messages | 
| 88 | 90 |     while(true) { | 
| 89 |  | -      val messages = (1 to messagesPerSec.toInt).map { messageNum => | 
|  | 91 | +      (1 to messagesPerSec.toInt).foreach { messageNum => | 
| 90 | 92 |         val str = (1 to wordsPerMessage.toInt).map(x => scala.util.Random.nextInt(10).toString) | 
| 91 | 93 |           .mkString(" ") | 
| 92 | 94 | 
 | 
| 93 |  | -        new KeyedMessage[String, String](topic, str) | 
| 94 |  | -      }.toArray | 
|  | 95 | +        val message = new ProducerRecord[String, String](topic, null, str) | 
|  | 96 | +        producer.send(message) | 
|  | 97 | +      } | 
| 95 | 98 | 
 | 
| 96 |  | -      producer.send(messages: _*) | 
| 97 | 99 |       Thread.sleep(100) | 
| 98 | 100 |     } | 
| 99 | 101 |   } | 
|  | 
0 commit comments