设为首页 收藏本站
查看: 1005|回复: 0

[经验分享] Kafka(4)Multiple Kafka and Scala Client

[复制链接]

尚未签到

发表于 2017-5-23 15:12:51 | 显示全部楼层 |阅读模式
  Kafka(4)Multiple Kafka and Scala Client

1. Create Multiple Nodes
>cp server.properties server1.properties
>cp server.properties server2.properties
The content are as follow:
config/sever1.properties
broker.id = 1
port =9093
log.dir=/tmp/kafka-logs-1

config/server2.properties
broker.id = 2
port = 9094
log.dir=/tmp/kafka-logs-2

Start the 2 nodes
>JMX_PORT=9997 bin/kafka-server-start.sh config/server1.properties &
>JMX_PORT=9998 bin/kafka-server-start.sh config/server2.properties &

Create the topic
>bin/kafka-create-topic.sh --zookeeper localhost:2181 --replica 2 --partition 1 --topic my-replicated-topic

List the topics
>bin/kafka-list-topic.sh --zookeeper localhost:2181

topic: my-replicated-topicpartition: 0leader: 1replicas: 1,2isr: 1,2 topic: testpartition: 0leader: 0replicas: 0isr: 0

Here is the command to kill one node
>pkill -9 -f config/server1.properties

2. The Scala Client
The Scala Class will be as follow:
        "com.rabbitmq"        %   "amqp-client"               % "3.1.4",
        "org.apache.kafka"          %   "kafka_2.10"            % "0.8.0" intransitive(),
        "com.yammer.metrics"        %   "metrics-core"          % "2.2.0",
        "com.twitter"         %%  "util-collection"           % "6.3.6",
        "com.101tec"          %   "zkclient"                  % "0.3"

The producer class
package com.sillycat.superduty.jobs.producer

import java.util.Properties

import kafka.javaapi.producer.Producer
import kafka.producer.{ KeyedMessage, ProducerConfig }

object NewTaskKafka extends App {
  val props2: Properties = new Properties()
  props2.put("zk.connect", "localhost:2181")
  props2.put("metadata.broker.list", "localhost:9092");
  props2.put("serializer.class", "kafka.serializer.StringEncoder")
  props2.put("zk.connectiontimeout.ms", "15000")

  val config: ProducerConfig = new ProducerConfig(props2)

  val producer: Producer[String, String] = new Producer[String, String](config)

  val data = new KeyedMessage[String, String]("test", "test-message, it is ok")
  producer.send(data)
  producer.close
}

The Consumer class
package com.sillycat.superduty.jobs.consumer

import kafka.api.{ FetchRequestBuilder, FetchRequest }
import kafka.javaapi.consumer.SimpleConsumer
import kafka.javaapi.FetchResponse
import kafka.javaapi.message.ByteBufferMessageSet
import scala.collection.JavaConversions._
import java.nio.ByteBuffer
import com.typesafe.scalalogging.slf4j.Logging
import java.util.Properties
import kafka.consumer.{Consumer, ConsumerConfig}
import scala.collection.JavaConverters._

object WorkerKafka extends App with Logging {

  val props = new Properties()

  props.put("group.id", "console-consumer-2222222")
  props.put("socket.receive.buffer.bytes", (2 * 1024 * 1024).toString)
  props.put("socket.timeout.ms", (ConsumerConfig.SocketTimeout).toString)
  props.put("fetch.message.max.bytes", (1024 * 1024).toString)
  props.put("fetch.min.bytes", (1).toString)
  props.put("fetch.wait.max.ms", (100).toString)
  props.put("auto.commit.enable", "true")
  props.put("auto.commit.interval.ms", (ConsumerConfig.AutoCommitInterval).toString)
  props.put("auto.offset.reset", "smallest")
  props.put("zookeeper.connect", "localhost:2181")
  props.put("consumer.timeout.ms", (-1).toString)
  props.put("refresh.leader.backoff.ms", (ConsumerConfig.RefreshMetadataBackoffMs).toString)


  val config   = new ConsumerConfig(props)
  val consumer = Consumer.createJavaConsumerConnector(config)

  val topicMap =  Map[String, Integer]("test" -> 1)

  println("about to get the comsumerMsgStreams")
  val consumerMap = consumer.createMessageStreams(topicMap.asJava)

  val streamz = consumerMap.get("test")

  val stream = streamz.iterator().next()

  println("listening... (?) ")

  val consumerIter = stream.iterator()
  while(consumerIter.hasNext()){
    System.out.println("MSG -> " + new String(consumerIter.next().message))
  }

}


We can use zkCli.sh to view some of the configuration.
>zkCli.sh -server localhost:2181
zkCli>ls /
zkCli>get /brokers/topics/my-replicated-topic
zkCli>get /brokers/topics/test

And here is the document for the zookeeper data structure
https://cwiki.apache.org/confluence/display/KAFKA/Kafka+data+structures+in+Zookeeper

We can also configure the multiple nodes for zookeeper in server.properties, there is a configuration like this.
zookeeper.connect=localhost:2182,localhost:2183


References:
http://kafka.apache.org/08/quickstart.html

https://cwiki.apache.org/confluence/display/KAFKA/Index

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-379796-1-1.html 上篇帖子: kafka常用命令 下篇帖子: kafka-design
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表