kafka.utils.ZkUtils Scala Examples
The following examples show how to use kafka.utils.ZkUtils.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: KafkaClient.scala From incubator-retired-gearpump with Apache License 2.0 | 6 votes |
package org.apache.gearpump.streaming.kafka.lib.util import kafka.admin.AdminUtils import kafka.cluster.Broker import kafka.common.TopicAndPartition import kafka.consumer.SimpleConsumer import kafka.utils.{ZKStringSerializer, ZkUtils} import org.I0Itec.zkclient.ZkClient import org.apache.gearpump.streaming.kafka.lib.source.consumer.KafkaConsumer import org.apache.gearpump.streaming.kafka.util.KafkaConfig import org.apache.gearpump.util.LogUtil import org.apache.kafka.clients.producer.KafkaProducer import org.apache.kafka.common.serialization.Serializer object KafkaClient { private val LOG = LogUtil.getLogger(classOf[KafkaClient]) val factory = new KafkaClientFactory class KafkaClientFactory extends java.io.Serializable { def getKafkaClient(config: KafkaConfig): KafkaClient = { val consumerConfig = config.getConsumerConfig val zkClient = new ZkClient(consumerConfig.zkConnect, consumerConfig.zkSessionTimeoutMs, consumerConfig.zkConnectionTimeoutMs, ZKStringSerializer) new KafkaClient(config, zkClient) } } } class KafkaClient(config: KafkaConfig, zkClient: ZkClient) { import org.apache.gearpump.streaming.kafka.lib.util.KafkaClient._ private val consumerConfig = config.getConsumerConfig def getTopicAndPartitions(consumerTopics: List[String]): Array[TopicAndPartition] = { try { ZkUtils.getPartitionsForTopics(zkClient, consumerTopics).flatMap { case (topic, partitions) => partitions.map(TopicAndPartition(topic, _)) }.toArray } catch { case e: Exception => LOG.error(e.getMessage) throw e } } def getBroker(topic: String, partition: Int): Broker = { try { val leader = ZkUtils.getLeaderForPartition(zkClient, topic, partition) .getOrElse(throw new RuntimeException( s"leader not available for TopicAndPartition($topic, $partition)")) ZkUtils.getBrokerInfo(zkClient, leader) .getOrElse(throw new RuntimeException(s"broker info not found for leader $leader")) } catch { case e: Exception => LOG.error(e.getMessage) throw e } } def createConsumer(topic: String, partition: Int, startOffsetTime: Long): KafkaConsumer = { val broker = getBroker(topic, partition) val soTimeout = consumerConfig.socketTimeoutMs val soBufferSize = consumerConfig.socketReceiveBufferBytes val clientId = consumerConfig.clientId val fetchSize = consumerConfig.fetchMessageMaxBytes val consumer = new SimpleConsumer(broker.host, broker.port, soTimeout, soBufferSize, clientId) KafkaConsumer(topic, partition, startOffsetTime, fetchSize, consumer) } def createProducer[K, V](keySerializer: Serializer[K], valueSerializer: Serializer[V]): KafkaProducer[K, V] = { new KafkaProducer[K, V](config.getProducerConfig, keySerializer, valueSerializer) } def createTopic(topic: String, partitions: Int, replicas: Int): Boolean = { try { if (AdminUtils.topicExists(zkClient, topic)) { LOG.info(s"topic $topic exists") true } else { AdminUtils.createTopic(zkClient, topic, partitions, replicas) LOG.info(s"created topic $topic") false } } catch { case e: Exception => LOG.error(e.getMessage) throw e } } def close(): Unit = { zkClient.close() } }
Example 2
Source File: KafkaIntSpec.scala From kafka-configurator with BSD 3-Clause "New" or "Revised" License | 5 votes |
package common import cakesolutions.kafka.testkit.KafkaServer import kafka.utils.ZkUtils import org.apache.kafka.clients.admin.AdminClient import org.apache.kafka.clients.admin.AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG import org.scalatest.BeforeAndAfterAll import org.scalatest.concurrent.PatienceConfiguration import org.scalatest.time.{ Millis, Seconds, Span } import scala.collection.JavaConverters._ import scala.concurrent.duration._ abstract class KafkaIntSpec extends BaseSpec with BeforeAndAfterAll with PatienceConfiguration { override implicit val patienceConfig = PatienceConfig(Span(3, Seconds), Span(250, Millis)) val kafkaServer = new KafkaServer() val kafkaPort = kafkaServer.kafkaPort val zkSessionTimeout = 30 seconds val zkConnectionTimeout = 30 seconds lazy val zkUtils = ZkUtils(s"localhost:${kafkaServer.zookeeperPort}", zkSessionTimeout.toMillis.toInt, zkConnectionTimeout.toMillis.toInt, isZkSecurityEnabled = false) lazy val kafkaAdminClient = AdminClient.create(Map[String, AnyRef]( BOOTSTRAP_SERVERS_CONFIG -> s"localhost:$kafkaPort" ).asJava) override def beforeAll() = kafkaServer.startup() override def afterAll() = { kafkaAdminClient.close() zkUtils.close() kafkaServer.close() } }
Example 3
Source File: KafkaAdminUtils.scala From gimel with Apache License 2.0 | 5 votes |
package com.paypal.gimel.common.storageadmin import java.util.Properties import kafka.admin._ import kafka.server.ConfigType import kafka.utils.ZkUtils import org.I0Itec.zkclient.{ZkClient, ZkConnection} import org.I0Itec.zkclient.exception.ZkMarshallingError import org.I0Itec.zkclient.serialize.ZkSerializer import org.apache.kafka.common.security.JaasUtils import com.paypal.gimel.logger.Logger object KafkaAdminUtils { val logger = Logger() val isSecurityEnabled = JaasUtils.isZkSecurityEnabled() val sessionTimeOutInMs: Int = 10 * 1000 val connectionTimeOutInMs: Int = 10 * 1000 val zkClient: (String) => ZkClient = new ZkClient(_: String, sessionTimeOutInMs, connectionTimeOutInMs, GimelZKStringSerializer) val zkConnection: (String) => ZkConnection = new ZkConnection(_: String, sessionTimeOutInMs) def isTopicExists(zookKeeperHostAndPort: String, kafkaTopicName: String): Boolean = { def MethodName: String = new Exception().getStackTrace().apply(1).getMethodName() logger.info(" @Begin --> " + MethodName) val client = zkClient(zookKeeperHostAndPort) val connect = zkConnection(zookKeeperHostAndPort) val zkUtil: ZkUtils = new ZkUtils(client, connect, isSecurityEnabled) val result = AdminUtils.topicExists(zkUtil, kafkaTopicName) connect.close() result } } object GimelZKStringSerializer extends ZkSerializer { @throws(classOf[ZkMarshallingError]) def serialize(data: Object): Array[Byte] = { data.asInstanceOf[String].getBytes("UTF-8") } @throws(classOf[ZkMarshallingError]) def deserialize(bytes: Array[Byte]): Object = { if (bytes == null) { null } else { new String(bytes, "UTF-8") } } }
Example 4
Source File: KafkaCollector.scala From Swallow with Apache License 2.0 | 5 votes |
package com.intel.hibench.common.streaming.metrics import java.io.{FileWriter, File} import java.util.Date import java.util.concurrent.{TimeUnit, Future, Executors} import com.codahale.metrics.{UniformReservoir, Histogram} import kafka.utils.{ZKStringSerializer, ZkUtils} import org.I0Itec.zkclient.ZkClient import scala.collection.mutable.ArrayBuffer class KafkaCollector(zkConnect: String, metricsTopic: String, outputDir: String, sampleNumber: Int, desiredThreadNum: Int) extends LatencyCollector { private val histogram = new Histogram(new UniformReservoir(sampleNumber)) private val threadPool = Executors.newFixedThreadPool(desiredThreadNum) private val fetchResults = ArrayBuffer.empty[Future[FetchJobResult]] def start(): Unit = { val partitions = getPartitions(metricsTopic, zkConnect) println("Starting MetricsReader for kafka topic: " + metricsTopic) partitions.foreach(partition => { val job = new FetchJob(zkConnect, metricsTopic, partition, histogram) val fetchFeature = threadPool.submit(job) fetchResults += fetchFeature }) threadPool.shutdown() threadPool.awaitTermination(30, TimeUnit.MINUTES) val finalResults = fetchResults.map(_.get()).reduce((a, b) => { val minTime = Math.min(a.minTime, b.minTime) val maxTime = Math.max(a.maxTime, b.maxTime) val count = a.count + b.count new FetchJobResult(minTime, maxTime, count) }) report(finalResults.minTime, finalResults.maxTime, finalResults.count) } private def getPartitions(topic: String, zkConnect: String): Seq[Int] = { val zkClient = new ZkClient(zkConnect, 6000, 6000, ZKStringSerializer) try { ZkUtils.getPartitionsForTopics(zkClient, Seq(topic)).flatMap(_._2).toSeq } finally { zkClient.close() } } private def report(minTime: Long, maxTime: Long, count: Long): Unit = { val outputFile = new File(outputDir, metricsTopic + ".csv") println(s"written out metrics to ${outputFile.getCanonicalPath}") val header = "time,count,throughput(msgs/s),max_latency(ms),mean_latency(ms),min_latency(ms)," + "stddev_latency(ms),p50_latency(ms),p75_latency(ms),p95_latency(ms),p98_latency(ms)," + "p99_latency(ms),p999_latency(ms)\n" val fileExists = outputFile.exists() if (!fileExists) { val parent = outputFile.getParentFile if (!parent.exists()) { parent.mkdirs() } outputFile.createNewFile() } val outputFileWriter = new FileWriter(outputFile, true) if (!fileExists) { outputFileWriter.append(header) } val time = new Date(System.currentTimeMillis()).toString val count = histogram.getCount val snapshot = histogram.getSnapshot val throughput = count * 1000 / (maxTime - minTime) outputFileWriter.append(s"$time,$count,$throughput," + s"${formatDouble(snapshot.getMax)}," + s"${formatDouble(snapshot.getMean)}," + s"${formatDouble(snapshot.getMin)}," + s"${formatDouble(snapshot.getStdDev)}," + s"${formatDouble(snapshot.getMedian)}," + s"${formatDouble(snapshot.get75thPercentile())}," + s"${formatDouble(snapshot.get95thPercentile())}," + s"${formatDouble(snapshot.get98thPercentile())}," + s"${formatDouble(snapshot.get99thPercentile())}," + s"${formatDouble(snapshot.get999thPercentile())}\n") outputFileWriter.close() } private def formatDouble(d: Double): String = { "%.3f".format(d) } }
Example 5
Source File: KafkaConsumer.scala From Swallow with Apache License 2.0 | 5 votes |
package com.intel.hibench.common.streaming.metrics import java.util.Properties import kafka.api.{OffsetRequest, FetchRequestBuilder} import kafka.common.ErrorMapping._ import kafka.common.TopicAndPartition import kafka.consumer.{ConsumerConfig, SimpleConsumer} import kafka.message.MessageAndOffset import kafka.utils.{ZKStringSerializer, ZkUtils, Utils} import org.I0Itec.zkclient.ZkClient class KafkaConsumer(zookeeperConnect: String, topic: String, partition: Int) { private val CLIENT_ID = "metrics_reader" private val props = new Properties() props.put("zookeeper.connect", zookeeperConnect) props.put("group.id", CLIENT_ID) private val config = new ConsumerConfig(props) private val consumer = createConsumer private val earliestOffset = consumer .earliestOrLatestOffset(TopicAndPartition(topic, partition), OffsetRequest.EarliestTime, -1) private var nextOffset: Long = earliestOffset private var iterator: Iterator[MessageAndOffset] = getIterator(nextOffset) def next(): Array[Byte] = { val mo = iterator.next() val message = mo.message nextOffset = mo.nextOffset Utils.readBytes(message.payload) } def hasNext: Boolean = { @annotation.tailrec def hasNextHelper(iter: Iterator[MessageAndOffset], newIterator: Boolean): Boolean = { if (iter.hasNext) true else if (newIterator) false else { iterator = getIterator(nextOffset) hasNextHelper(iterator, newIterator = true) } } hasNextHelper(iterator, newIterator = false) } def close(): Unit = { consumer.close() } private def createConsumer: SimpleConsumer = { val zkClient = new ZkClient(zookeeperConnect, 6000, 6000, ZKStringSerializer) try { val leader = ZkUtils.getLeaderForPartition(zkClient, topic, partition) .getOrElse(throw new RuntimeException( s"leader not available for TopicAndPartition($topic, $partition)")) val broker = ZkUtils.getBrokerInfo(zkClient, leader) .getOrElse(throw new RuntimeException(s"broker info not found for leader $leader")) new SimpleConsumer(broker.host, broker.port, config.socketTimeoutMs, config.socketReceiveBufferBytes, CLIENT_ID) } catch { case e: Exception => throw e } finally { zkClient.close() } } private def getIterator(offset: Long): Iterator[MessageAndOffset] = { val request = new FetchRequestBuilder() .addFetch(topic, partition, offset, config.fetchMessageMaxBytes) .build() val response = consumer.fetch(request) response.errorCode(topic, partition) match { case NoError => response.messageSet(topic, partition).iterator case error => throw exceptionFor(error) } } }