Spark streaming using Direct read Kafka method to obtain data __scala

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.