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设计:也是根据我们的业务需求来 ...
随机推荐
- 微软Azure平台 cloud service动态申请证书并绑定证书碰到的坑
我们有一个saas平台 部分在azure的cloud service 使用lets encrypt来申请证书.每一个商家申请域名之后就需要通过Lets encrypt来得到证书并绑定证书. 主要碰到的 ...
- 【Leetcode 371】Sum of Two Integers
问题描述:不使用+是或-操作符进行整数的加法运算 int getSum(int a, int b); 我的思路:把整数化成二进制进行运算,注意类型是int,也就是要考虑负数.关于负数的二进制表示可见之 ...
- (转)Inno Setup入门(二十)——Inno Setup类参考(6)
本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17251041 存储框 存储框也是典型的窗口可视化组件,同编辑框类似, ...
- (转)Linq DataTable的修改和查询
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- java内存占用问题(一)
Nocturne 2012-12-24 java数组内存占用问题. 30 Contact[] ca = new Contact[10]; while(x<10){ ca[x]=new ...
- DataTable 树形构造加全部
DataTable dtGx = new DataTable(); dtGx = SqlHelper.SqlGetDataTable(StrSql, "tbUserGx"); th ...
- kotlin学习三:初步认识kotlin(第二篇)
上一章熟悉了kotlin基本的变量和函数声明,并明白了如何调用函数.本章再来看一些其他有用的东西 包括: 1. kotlin代码组织结构 2. when语法 3. 循环迭代语法 4. try表达式 1 ...
- Python代码审计中一些需要重点关注的项
SQL注入: 如果是常规没有进行预编译,或者直接使用原生的进行拼凑,那么在view的时候就需要多去观察了 [PythonSQL预编译]https://www.cnblogs.com/sevck/p/6 ...
- Linux - samba 服务
暂时关闭 iptables 防火墙 [root@sch01ar ~]# systemctl stop iptables.service 暂时关闭 firewall 防火墙 [root@sch01ar ...
- EMQ、Websocket、MQTT
mqtt.fx的安装和使用 https://blog.csdn.net/nicholaszao/article/details/79211965 EMO 使用说明 http://emqtt.com/d ...