2.spark-streaming实战
park Streaming--实战篇
摘要:

object GenerateChar {
def generateContext(index : Int) : String = {
import scala.collection.mutable.ListBuffer
val charList = ListBuffer[Char]()
for(i <- 65 to 90)
charList += i.toChar
val charArray = charList.toArray
charArray(index).toString
}
def index = {
import java.util.Random
val rdm = new Random
rdm.nextInt(7)
}
def main(args: Array[String]) {
val listener = new ServerSocket(9998)
while(true){
val socket = listener.accept()
new Thread(){
override def run() = {
println("Got client connected from :"+ socket.getInetAddress)
val out = new PrintWriter(socket.getOutputStream,true)
while(true){
Thread.sleep(500)
val context = generateContext(index) //产生的字符是字母表的前七个随机字母
println(context)
out.write(context + '\n')
out.flush()
}
socket.close()
}
}.start()
}
}
}


object ScoketStreaming {
def main(args: Array[String]) {
//创建一个本地的StreamingContext,含2个工作线程
val conf = new SparkConf().setMaster("local[2]").setAppName("ScoketStreaming")
val sc = new StreamingContext(conf,Seconds(10)) //每隔10秒统计一次字符总数
//创建珍一个DStream,连接master:9998
val lines = sc.socketTextStream("master",9998)
val words = lines.flatMap(_.split(" "))
val wordCounts = words.map(x => (x , 1)).reduceByKey(_ + _)
wordCounts.print()
sc.start() //开始计算
sc.awaitTermination() //通过手动终止计算,否则一直运行下去
}
}


Got client connected from :/192.168.31.128
C
G
B
C
F
G
D
G
B


-------------------------------------------
Time: 1459426750000 ms
-------------------------------------------
(B,1)
(G,1)
(C,1)
-------------------------------------------
Time: 1459426760000 ms
-------------------------------------------
(B,5)
(F,3)
(D,4)
(G,3)
(C,3)
(E,1)


When running a Spark Streaming program locally, do not use “local” or “local[1]” as the master URL. Either ofthese means that only one thread
will be used for running tasks locally. If you are using a input DStream based on a receiver (e.g. sockets, Kafka, Flume, etc.), then the single
thread will be used to run the receiver,leaving no thread for processing the received data. 当在本地运行Spark Streaming程序时,Master的URL不能设置为"local"或"local[1]",这两种设置都意味着你将会只有一个线程来运行作业,如果你的Input DStream基于一个接收器
(如Kafka,Flume等),那么只有一个线程来接收数据,而没有多余的线程来处理接收到的数据。


object FileStreaming {
def main(args: Array[String]) {
val conf = new SparkConf().setMaster("local").setAppName("FileStreaming")
val sc = new StreamingContext(conf,Seconds(5))
val lines = sc.textFileStream("/home/hadoop/wordCount")
val words = lines.flatMap(_.split(" "))
val wordCounts = words.map(x => (x , 1)).reduceByKey(_ + _)
sc.start()
sc.awaitTermination()
}
}


object QueueStream {
def main(args: Array[String]) {
val conf = new SparkConf().setMaster("local[2]").setAppName("queueStream")
//每1秒对数据进行处理
val ssc = new StreamingContext(conf,Seconds(1))
//创建一个能够push到QueueInputDStream的RDDs队列
val rddQueue = new mutable.SynchronizedQueue[RDD[Int]]()
//基于一个RDD队列创建一个输入源
val inputStream = ssc.queueStream(rddQueue)
val mappedStream = inputStream.map(x => (x % 10,1))
val reduceStream = mappedStream.reduceByKey(_ + _)
reduceStream.print
ssc.start()
for(i <- 1 to 30){
rddQueue += ssc.sparkContext.makeRDD(1 to 100, 2) //创建RDD,并分配两个核数
Thread.sleep(1000)
}
ssc.stop()
}
}


-------------------------------------------
Time: 1459595433000 ms //第1个输出
-------------------------------------------
(4,10)
(0,10)
(6,10)
(8,10)
(2,10)
(1,10)
(3,10)
(7,10)
(9,10)
(5,10)
............
............
-------------------------------------------
Time: 1459595463000 ms //第30个输出
-------------------------------------------
(4,10)
(0,10)
(6,10)
(8,10)
(2,10)
(1,10)
(3,10)
(7,10)
(9,10)
(5,10)


object StateFull {
def main(args: Array[String]) {
//定义状态更新函数
val updateFunc = (values: Seq[Int], state: Option[Int]) => {
val currentCount = values.foldLeft(0)(_ + _)
val previousCount = state.getOrElse(0)
Some(currentCount + previousCount)
}
val conf = new SparkConf().setMaster("local[2]").setAppName("stateFull")
val sc = new StreamingContext(conf, Seconds(5))
sc.checkpoint(".") //设置检查点,存储位置是当前目录,检查点具有容错机制
val lines = sc.socketTextStream("master", 9999)
val words = lines.flatMap(_.split(" "))
val wordDstream = words.map(x => (x, 1))
val stateDstream = wordDstream.updateStateByKey[Int](updateFunc)
stateDstream.print()
sc.start()
sc.awaitTermination()
}
}


-------------------------------------------
Time: 1459597690000 ms
-------------------------------------------
(B,3)
(F,1)
(D,1)
(G,1)
(C,1)
-------------------------------------------
Time: 1459597700000 ms //会累计之前的值
-------------------------------------------
(B,5)
(F,3)
(D,4)
(G,4)
(A,2)
(E,5)
(C,4)

Spark Straming最大的优点在于处理数据采用的是粗粒度的处理方式(一次处理一小批的数据),这种特性也更方便地实现容错恢复机制,其DStream是在RDD上的高级
抽象,所以其极容易与RDD进行互操作。
2.spark-streaming实战的更多相关文章
- Spark入门实战系列--7.Spark Streaming(下)--实时流计算Spark Streaming实战
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .实例演示 1.1 流数据模拟器 1.1.1 流数据说明 在实例演示中模拟实际情况,需要源源 ...
- spark streaming 实战
最近在学习spark的相关知识, 重点在看spark streaming 和spark mllib相关的内容. 关于spark的配置: http://www.powerxing.com/spark-q ...
- Spark Streaming实战
1.Storm 和 SparkStreaming区别 Storm 纯实时的流式处理,来一条数据就立即进行处理 SparkStreaming 微批处理,每次处理 ...
- 倾情大奉送--Spark入门实战系列
这一两年Spark技术很火,自己也凑热闹,反复的试验.研究,有痛苦万分也有欣喜若狂,抽空把这些整理成文章共享给大家.这个系列基本上围绕了Spark生态圈进行介绍,从Spark的简介.编译.部署,再到编 ...
- 《大数据Spark企业级实战 》
基本信息 作者: Spark亚太研究院 王家林 丛书名:决胜大数据时代Spark全系列书籍 出版社:电子工业出版社 ISBN:9787121247446 上架时间:2015-1-6 出版日期:20 ...
- Spark入门实战系列
转自:http://www.cnblogs.com/shishanyuan/p/4699644.html 这一两年Spark技术很火,自己也凑热闹,反复的试验.研究,有痛苦万分也有欣喜若狂,抽空把这些 ...
- 日志=>flume=>kafka=>spark streaming=>hbase
日志=>flume=>kafka=>spark streaming=>hbase 日志部分 #coding=UTF-8 import random import time ur ...
- Spark入门实战系列--7.Spark Streaming(上)--实时流计算Spark Streaming原理介绍
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Spark Streaming简介 1.1 概述 Spark Streaming 是Spa ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记二十一之铭文升级版
铭文一级: DataV功能说明1)点击量分省排名/运营商访问占比 Spark SQL项目实战课程: 通过IP就能解析到省份.城市.运营商 2)浏览器访问占比/操作系统占比 Hadoop项目:userA ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记十八之铭文升级版
铭文一级: 功能二:功能一+从搜索引擎引流过来的 HBase表设计create 'imooc_course_search_clickcount','info'rowkey设计:也是根据我们的业务需求来 ...
随机推荐
- Python中的url编码问题
>>> import urllib >>> a = "PythonTab中文网" >>> a 'PythonTab\xe4\x ...
- mysql server id一样导致报错
(root@localhost) 16:03:38 [(none)]> show slave status \G; Last_IO_Errno: 1593 Last_IO_Error: Fata ...
- php redis安装使用
下载redis-windows-master.解压点击redis-server.exe运行服务端 redis设置访问密码 修改redis.conf文件配置, # requirepass foobare ...
- oracle autotrace使用
通过以下方法可以把Autotrace的权限授予Everyone, 如果你需要限制Autotrace权限,可以把对public的授权改为对特定user的授权. D:\oracle\ora92>sq ...
- OD 实验(二十) - 对反调试程序的逆向分析(一)
程序: Keyfile.dat 里的内容 该文件中要至少有 9 个 ReverseMe.A: 运行程序 用 OD 打开该程序,运行 弹出的是错误的对话框 该程序发现 OD 对它的调试,所以该程序对 O ...
- C 语言 - Unicode 解决中文问题
问题: 打印一句中文 #include <stdio.h> int main() { char str[] = "你好,世界"; printf("%s\n&q ...
- PHP正则表达式详解
正则表达式定义 正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取出符合某个条件的子串等. 列目录时, ...
- Nginx配置ProxyCache缓存
利用nginx cache缓存网站数据 nginx本身就有缓存功能,能够缓存静态对象,比如图片.CSS.JS等内容直接缓存到本地,下次访问相同对象时,直接从缓存即可,无需访问后端静态服务器以及存储存储 ...
- 浪潮openStack云
- [Z] Linux下进程的文件访问权限
原文链接:http://blog.csdn.net/chosen0ne/article/details/10581883 对进程校验文件访问权限包括两个部分,一是确定进程的角色(属于哪个用户或者组), ...