当前位置: 首页>>代码示例>>Scala>>正文


Scala ZkUtils类代码示例

本文整理汇总了Scala中kafka.utils.ZkUtils的典型用法代码示例。如果您正苦于以下问题:Scala ZkUtils类的具体用法?Scala ZkUtils怎么用?Scala ZkUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了ZkUtils类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: 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))
  }

} 
开发者ID:javipacheco,项目名称:spark-android-log,代码行数:52,代码来源:KafkaUtilities.scala

示例2: Cluster

//设置package包名称以及导入依赖的类
package net.kemuridama.kafcon.model

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

import spray.json._
import kafka.utils.ZkUtils

import net.kemuridama.kafcon.protocol.ZooKeeperBrokerJsonProtocol

case class Cluster(
  id: Int,
  name: String,
  zookeepers: List[String]
) extends ZooKeeperBrokerJsonProtocol {

  private val sessionTimeout = 10 * 1000
  private val connectionTimeout = 10 * 1000

  private var connectionState: ConnectionState = ConnectionState.Disconnected

  private def brokerPath(id: Int) = "/brokers/ids/%d".format(id)

  def withZkUtils[T](func: ZkUtils => T): Future[T] = Future {
    val zkUtils = ZkUtils(zookeepers.mkString(","), sessionTimeout, connectionTimeout, false)
    val ret = func(zkUtils)
    zkUtils.close
    ret
  }

  def getConnectionState: ConnectionState = connectionState

  def getAllBrokers: Future[List[Broker]] = withZkUtils { zk =>
    zk.getAllBrokersInCluster.toList.map { broker =>
      val (data, stat) = zk.readDataMaybeNull(brokerPath(broker.id))
      data.map(_.parseJson.convertTo[ZooKeeperBroker].toBroker(id, broker.id))
    } flatten
  }

  def getAllTopics: Future[List[String]] = withZkUtils(_.getAllTopics.toList)

  def toClusterResponseData(brokers: List[Broker], topics: List[Topic]): ClusterResponseData = {
    ClusterResponseData(
      id              = id,
      name            = name,
      zookeepers      = zookeepers,
      brokers         = brokers,
      topics          = topics,
      partitionCount  = topics.map(_.partitions.size).sum,
      messageCount    = topics.map(_.messageCount).sum,
      connectionState = getConnectionState
    )
  }

} 
开发者ID:kemuridama,项目名称:kafcon,代码行数:56,代码来源:Cluster.scala


注:本文中的kafka.utils.ZkUtils类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。