kafka基本操作

启动kafka

 bin/kafka-server-start.sh -daemon config/server.properties

创建消费者

bin/kafka-console-consumer.sh --bootstrap-server 192.168.142.100:9092 --from-beginning --topic newTopic

创建生产者

bin/kafka-console-producer.sh --broker-list 192.168.142.100:9092 --topic newTopic

使用flume监控文件并输出到kafka

1. 配置文件 flume2kafka.conf
#定义了当前agent的名字叫做a1
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /root/log/log.txt
a1.sources.r1.shell=/bin/sh -c

# Describe the sink
a1.sinks.k1.type = logger

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1

a1.sinks.k1.channel = c1

# 指定Flume sink
#a1.sinks.k1.type = logger
a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.topic = aiqiyi
a1.sinks.k1.brokerList = 192.168.142.100:9092
a1.sinks.k1.requiredAcks = 1
a1.sinks.k1.batchSize = 100
2. 启动flume
bin/flume-ng agent -c conf -f conf/flume2kafka.conf --name a1 -Dflume.root.logger=INFO,console

3. 修改log.txt文件
4. 启动kafka消费
bin/kafka-console-consumer.sh --bootstrap-server 192.168.142.100:9092 --topic aiqiyi  --from-beginning

IDEA scala代码

import java.util.Properties
import kafka.admin.{AdminUtils, RackAwareMode}
import kafka.consumer.{Consumer, ConsumerConfig}
import kafka.javaapi.producer.Producer
import kafka.producer.{KeyedMessage, ProducerConfig}
import kafka.utils.ZkUtils
import org.apache.kafka.common.security.JaasUtils
import collection.JavaConversions._


object test {
// 创建topic
  /* def main(args: Array[String]): Unit = {
    val zkUtils = ZkUtils.
      apply("192.168.142.100:2181", 30000, 30000, JaasUtils.isZkSecurityEnabled)
    AdminUtils.createTopic(zkUtils, "newTopic", 1, 1, new Properties(), RackAwareMode.Enforced)
    zkUtils.close()
  }
*/

  def main(args: Array[String]): Unit = {
    //testProducer
    testConsumer
  }
/*
  def testProducer {
    val props = new Properties()
    props.put("serializer.class", "kafka.serializer.StringEncoder")
    props.put("metadata.broker.list", "192.168.142.100:9092,192.168.142.101:9092,192.168.142.102:9092")
    props.put("request.required.acks", "1")
    val config = new ProducerConfig(props)
    val producer = new Producer[String, String](config)
    (1 to 100).foreach((i: Int) => {
      val msg = new KeyedMessage("newTopic", "key", "msg" + i)
      producer.send(msg)
      Thread.sleep(1000)
    })
    producer.close()
  }*/

  def testConsumer {
    val topic = "newTopic"
    val props = new Properties()
    props.put("zookeeper.connect", "192.168.142.100:2181,192.168.142.101:2181,192.168.142.102:2181")
    props.put("group.id", "testGroup")
    val consumerConnector = Consumer.createJavaConsumerConnector(new ConsumerConfig(props))
    val topicCountMap = Map(topic -> new Integer(1))
    val messageStreams = consumerConnector.createMessageStreams(topicCountMap)
    val stream = messageStreams.get(topic).get(0)
    val iterator = stream.iterator()
    while (iterator.hasNext) {
      val msg = new String(iterator.next.message)
      println(msg)
    }
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值