kafka.utils.ZKStringSerializer Scala Examples
The following examples show how to use kafka.utils.ZKStringSerializer.
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: ZookeeperHarness.scala From incubator-retired-gearpump with Apache License 2.0 | 5 votes |
package org.apache.gearpump.streaming.kafka.util import kafka.utils.{TestZKUtils, Utils, ZKStringSerializer} import kafka.zk.EmbeddedZookeeper import org.I0Itec.zkclient.ZkClient trait ZookeeperHarness { val zkConnect: String = TestZKUtils.zookeeperConnect val zkConnectionTimeout = 60000 val zkSessionTimeout = 60000 private var zookeeper: EmbeddedZookeeper = null private var zkClient: ZkClient = null def getZookeeper: EmbeddedZookeeper = zookeeper def getZkClient: ZkClient = zkClient def setUp() { zookeeper = new EmbeddedZookeeper(zkConnect) zkClient = new ZkClient(zkConnect, zkSessionTimeout, zkConnectionTimeout, ZKStringSerializer) } def tearDown() { zkClient.close() Utils.swallow(zookeeper.shutdown()) } }
Example 3
Source File: MetricsUtil.scala From Swallow with Apache License 2.0 | 5 votes |
package com.intel.hibench.common.streaming.metrics import com.intel.hibench.common.streaming.Platform import kafka.admin.AdminUtils import kafka.utils.ZKStringSerializer import org.I0Itec.zkclient.ZkClient object MetricsUtil { val TOPIC_CONF_FILE_NAME = "metrics_topic.conf" def getTopic(platform: Platform, sourceTopic: String, producerNum: Int, recordPerInterval: Long, intervalSpan: Int): String = { val topic = s"${platform}_${sourceTopic}_${producerNum}_${recordPerInterval}" + s"_${intervalSpan}_${System.currentTimeMillis()}" println(s"metrics is being written to kafka topic $topic") topic } def createTopic(zkConnect: String, topic: String, partitions: Int): Unit = { val zkClient = new ZkClient(zkConnect, 6000, 6000, ZKStringSerializer) try { AdminUtils.createTopic(zkClient, topic, partitions, 1) while (!AdminUtils.topicExists(zkClient, topic)) { Thread.sleep(100) } } catch { case e: Exception => throw e } finally { zkClient.close() } } }
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) } } }