本文整理汇总了Scala中kafka.admin.AdminUtils类的典型用法代码示例。如果您正苦于以下问题:Scala AdminUtils类的具体用法?Scala AdminUtils怎么用?Scala AdminUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AdminUtils类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: KafkaConfiguratorFeatureSpec
//设置package包名称以及导入依赖的类
package com.sky.kafka.configurator
import common.KafkaIntSpec
import kafka.admin.AdminUtils
import org.scalatest.concurrent.Eventually
import scala.util.Success
class KafkaConfiguratorFeatureSpec extends KafkaIntSpec with Eventually {
"KafkaConfigurator" should "create new topics in Kafka from a file" in {
val args = Array(
"-f", getClass.getResource("/topic-configuration.yml").getPath,
"--zookeeper", s"localhost:${kafkaServer.zookeeperPort.toString}"
)
val topics = List("topic1", "topic2")
topics.map(AdminUtils.topicExists(zkUtils, _) shouldBe false)
Main.run(args) shouldBe Success(())
eventually {
withClue("Topic exists: ") {
topics.map(AdminUtils.topicExists(zkUtils, _) shouldBe true)
}
}
}
}
示例2: KafkaUtilities
//设置package包名称以及导入依赖的类
package com.fortysevendeg.log.utils
import java.util.Properties
import kafka.admin.AdminUtils
import kafka.producer.{KeyedMessage, Producer, ProducerConfig}
import kafka.utils.ZkUtils
import org.I0Itec.zkclient.ZkConnection
import org.apache.kafka.clients.consumer.KafkaConsumer
object KafkaUtilities {
def createKafkaProducer(): Producer[String, String] = {
val props = new Properties()
props.put("metadata.broker.list", "localhost:9092")
props.put("serializer.class", "kafka.serializer.StringEncoder")
// props.put("partitioner.class", "com.fortysevendeg.biglog.SimplePartitioner")
// props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
// props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
props.put("producer.type", "async")
props.put("request.required.acks", "1")
val config = new ProducerConfig(props)
new Producer[String, String](config)
}
def createKafkaConsumer(): KafkaConsumer[String, String] = {
val props = new Properties()
props.put("bootstrap.servers", "localhost:9092")
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
new KafkaConsumer[String, String](props)
}
def createTopicIntoKafka(topic: String, numPartitions: Int, replicationFactor: Int): Unit = {
val zookeeperConnect = "localhost:2181"
val sessionTimeoutMs = 10 * 1000
val connectionTimeoutMs = 8 * 1000
val zkClient = ZkUtils.createZkClient(zookeeperConnect, sessionTimeoutMs, connectionTimeoutMs)
val zkUtils = new ZkUtils(zkClient, zkConnection = new ZkConnection(zookeeperConnect), isSecure = false)
AdminUtils.createTopic(zkUtils, topic, numPartitions, replicationFactor, new Properties)
zkClient.close()
}
def d(kafkaProducer: Producer[String, String], topic: String, message: String) = {
kafkaProducer.send(new KeyedMessage[String, String](topic, message))
}
}