Kafka 命令行集合
启动 zookeeper 默认端口2181
bin\windows\zookeeper-server-start.bat config\zookeeper.properties
启动Kafka服务 默认端口9092
bin\windows\kafka-server-start.bat config\server.properties
创建主题:
.\bin\windows\kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test(2.8.0以上kafka适用)
创建生产者:
.\bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test
创建消费者:
.\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning
查看topic:
.\bin\windows\kafka-topics.bat --describe --zookeeper localhost:2181 --topic
前面启动窗口每个命令行必须单独占一个,不能关闭。
Scala 程序
import org.apache.kafka.clients.consumer.ConsumerConfig
import org.apache.spark.SparkConf
import org.apache.spark.streaming.kafka010.{ConsumerStrategies, KafkaUtils, LocationStrategies}
import org.apache.spark.streaming.{Seconds, StreamingContext}
object test04_sock {
def main(args: Array[String]): Unit = {
val sparkconf = new SparkConf().setMaster("local[*]").setAppName("sparkstreaming")
val ssc = new StreamingContext(sparkconf, Seconds(3))
val kafkaPara:Map[String,Object]=Map(
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG->"localhost:9092",
ConsumerConfig.GROUP_ID_CONFIG->"test",
"key.deserializer" ->
"org.apache.kafka.common.serialization.StringDeserializer",
"value.deserializer" ->
"org.apache.kafka.common.serialization.StringDeserializer"
)
val kafakaDataDS = KafkaUtils.createDirectStream[String, String](
ssc,
LocationStrategies.PreferConsistent,
ConsumerStrategies.Subscribe[String, String](Set("test"), kafkaPara)
)
kafakaDataDS.map(_.value()).print()
ssc.start()
ssc.awaitTermination()
}
}