Package Com.xing.stream Import kafka.serializer.StringDecoder import org.apache.spark.SparkConf Import Org.apache.spark.streaming.kafka.KafkaUtils import org.apache.spark.streaming.
{Seconds, StreamingContext}/** * Created by Dengni on 2016/12/16. */class Streamingfirst {} object Streamingfirst {def main (args:array[string]) {val brokers = "192.168.184.1 88:9092, 192.168.184.178:9092, 192.168.184.168:9092 "val topics =" Meinv "val sparkconf = new sparkconf (). setApp Name ("Kafkastreaming"). Setmaster ("local[2]") val SSC = new StreamingContext (Sparkconf,seconds (6)) Ssc.checkpoint ( "W_checkpoints")//windows path val topicset = Topics.split (","). Toset val kafkaparams = map[string, String] ("Meta Data.broker.list "-> brokers"//{"@timestamp": "2016-12-14t16:26:21.746z", "beat": {"hostname": "Root", "name": " Root "," version ":" 5.1.1 "}," Metricset ": {" module ":" System "," name ":" Process "," RTT ": 28025}," system ": {" process ": {" CmdLine ":" \ "c:\\windows\\system32\\searchfilterhost.exe\ "0 624 628 644 8192 632", "CPU": {"start_time": "2016-12-14t16:24:15.240z", "total": {"pct": 0. 000000}}, "Memory": {"RSS": {"bytes": 7495680, "pct": 0.000400}, "Share": 0, "size": 1806336}, "name": " SearchFilterHost.exe "," Pgid ": 0," pid ": 8776," ppid ": 2524," state ":" Running "," username ":" NT Authority\\System "}," Type ":" Metricsets "} val lines = kafkautils.createdirectstream[string, String,stringdecoder, Stringdecoder] (SSC, Kafkaparams,topicset)//val message = Lines.map (_._1) map (_._1) data is empty null Val message = Lines.map (_._2)//map (_. _2) is the Kafka of the data val words = Message.flatmap (_.split (":")) val wordcounts = Words.map (x => (x, 1L)). Reduceb
Ykey (_ + _) Wordcounts.print ()//message.print () checked Ssc.start () ssc.awaittermination ()}
Refer to the program referenced by Kafka large data http://blog.csdn.net/haohaixingyun/article/details/53647963