第一个sparkstream例子
2015-08-15 10:01
337 查看
目录
用[TOC]来生成目录:
目录
第一个SPARKSTREAM例子
第一个SPARKSTREAM例子
在这个例子中,程序从监听TCP套接字的数据服务器获取文本数据,然后计算文本中包含的单词数。做法如下:首先,我们导入Spark Streaming的相关类以及一些从StreamingContext获得的隐式转换到我们的环境中,为我们所需的其他类(如DStream)提供有用的方法。StreamingContext 是Spark所有流操作的主要入口。然后,我们创建了一个具有两个执行线程以及1秒批间隔时间(即以秒为单位分割数据流)的本地StreamingContext。
/** * Created by root on 15-8-15. */ import org.apache.spark._ import org.apache.spark.streaming._ import org.apache.spark.streaming.StreamingContext._ object NetWorkWordCount { def main (args: Array[String]) { val conf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount") val ssc = new StreamingContext(conf, Seconds(1)) val lines = ssc.socketTextStream("localhost", 9999) val words = lines.flatMap(_.split(" ")) // Count each word in each batch val pairs = words.map(word => (word, 1)) val wordCounts = pairs.reduceByKey(_ + _) // Print the first ten elements of each RDD generated in this DStream to the console wordCounts.print() ssc.start() // Start the computation ssc.awaitTermination() // Wait for the computation to terminate } }
需要运行Netcat作为数据服务器
$ nc -lk 9999
然后在IDE里运行这个例子,在数据服务器端口输入单词,程序将监听到这些单词,并计算每个单词的个数。
相关文章推荐
- 微信公众号开发
- iOS 代理与block 逆向传值 学习
- Ubuntu下安装xampp总结
- 立帖为证
- uva 1473 - Dome of Circus (几何 + 二分)
- meta标签viewport详解
- 串口入门知识及总结
- Android开发中使用SharedPreferences记录用户是否第一次启动程序
- 01背包
- Android休眠唤醒和wakeup_source机制的使用(2)
- JSP学习笔记(一)
- java中Statement详细用法
- Python开发环境的搭建(win7)
- python 数值类型,判断类型
- Java 创建型模式:单态模式,原型模式,工厂方法,抽象工厂,建造者模式
- 设计模式--观察者模式
- Android开发专栏之ListView一两种适配器的使用和优化
- extends ActionBarActivity 出现的问题
- Spring常用annotation标签
- [LintCode] Count 1 in Binary