nc -lk 9999
val conf = new SparkConf().setAppName("test")
val sc = new SparkContext(sc)
val ssc = new StreamingContext(sc,Second(1))
或者直接
val ssc = new StreamingContext(SparkConf,Second(1))
val lines = ssc.socketTextStream(args(0),args(1).toInt,StorageLevel.MEMORY_AND_DISK_SER)
...
ssc.start()
ssc.awaitTermination()
transformation 和 output 来实现
1. 一旦context 启动,没有新的streaming 计算可以被设置和添加进来
2. 一旦context 被停止,它不能被再次启动
3. 在JVM 中只有一个StreamingContext 在同一时间可以被激活
4.在StreamingContext 执行时,stop()同时停止了SparkContext。 为了仅停止StreamingContext,将stopSparkContext的stop 选项设置为 false
5.SparkContext 可以重用来创建多个StreamContexts 一直到前一个StreamingContext 被停止时,(不停止SparkContext),才能创建下一个StreamingContext.
updateStateByKey(func)
使用Spark Streaming 编程
====使用Spark Streaming 处理HDFS上的数据
streamingContext.fileStream[KeyClass,ValueClass,InputFormatClass](dataDirectory)
===使用Spark Streaming 处理有状态的数据
ssc.checkPoint(".")
val lines = ssc.socketTextStream("localhost",6789)
val result = lines.flatMap(_.split(" ")).map((_,1))
val state = result.updateStateByKey(updateFunction)
state.print()
ssc.start()
ssc.waitTermination()
def upateFuntion(currentValues:Sql[Int],perValues:Option[Int]):Option[Int]={
val curr = currentValues.sum
val pre = perValues.getOrElse(0)
Some(curr+pre)
}
updateFunction 方法说明: 用当前batch的数据去更新已有的数据
注意事项:
1. 如果长期要保存一份key的state的话,那么Spark Streaming 是要求必须 checkpoint 的。 要设置一个 checkpoint 目录。 开启 checkpoint 机制
2. 那么每个key 对应的state 除了在内存有之外,在checkpoint 目录中也会有一份,以便以后内存数据丢失时可以从checkpoint 中恢复数据
3. 开启checkpoint 机制,只要调用 ssc.checkpoint方法,设置一个hdfs 目录即可
需求 : 使用 SparkStreaming 整合DataFrame 完成词频统计分析
使用 Spark Streaming 整合 Spark Sql 完成 WordCount 统计
result.foreachRDD(rdd=>{
if(rdd.count() !=0){
// 得到sqlContext 实例
val sqlContext = SQLContextSington.getInstance(rdd.SparkContext)
import sqlContext.implicit._
val df = rdd.map(x=>word(x)).toDF
df.registerTempTable("tb_word")
// 使用SparkSQL 进行词频统计
sqlContext.sql("select word,count(*) from tb_word group by word").show
}
})
ssc.start()
ssc.waitTermination()
// SQLContext 获取实例单词类
object SQLContextSington {
@ transient private val instance:SQLContext=_
def getInstance(sparkContext:SparkContext):SQLContext={
if(instace==null){
instace = new SQLContext(SparkContext)
}
instance
}
}
case class Word(word:String)
val conf = new SparkConf().setAppName("test")
val sc = new SparkContext(sc)
val ssc = new StreamingContext(sc,Second(1))
或者直接
val ssc = new StreamingContext(SparkConf,Second(1))
val lines = ssc.socketTextStream(args(0),args(1).toInt,StorageLevel.MEMORY_AND_DISK_SER)
...
ssc.start()
ssc.awaitTermination()
transformation 和 output 来实现
1. 一旦context 启动,没有新的streaming 计算可以被设置和添加进来
2. 一旦context 被停止,它不能被再次启动
3. 在JVM 中只有一个StreamingContext 在同一时间可以被激活
4.在StreamingContext 执行时,stop()同时停止了SparkContext。 为了仅停止StreamingContext,将stopSparkContext的stop 选项设置为 false
5.SparkContext 可以重用来创建多个StreamContexts 一直到前一个StreamingContext 被停止时,(不停止SparkContext),才能创建下一个StreamingContext.
updateStateByKey(func)
使用Spark Streaming 编程
====使用Spark Streaming 处理HDFS上的数据
streamingContext.fileStream[KeyClass,ValueClass,InputFormatClass](dataDirectory)
===使用Spark Streaming 处理有状态的数据
ssc.checkPoint(".")
val lines = ssc.socketTextStream("localhost",6789)
val result = lines.flatMap(_.split(" ")).map((_,1))
val state = result.updateStateByKey(updateFunction)
state.print()
ssc.start()
ssc.waitTermination()
def upateFuntion(currentValues:Sql[Int],perValues:Option[Int]):Option[Int]={
val curr = currentValues.sum
val pre = perValues.getOrElse(0)
Some(curr+pre)
}
updateFunction 方法说明: 用当前batch的数据去更新已有的数据
注意事项:
1. 如果长期要保存一份key的state的话,那么Spark Streaming 是要求必须 checkpoint 的。 要设置一个 checkpoint 目录。 开启 checkpoint 机制
2. 那么每个key 对应的state 除了在内存有之外,在checkpoint 目录中也会有一份,以便以后内存数据丢失时可以从checkpoint 中恢复数据
3. 开启checkpoint 机制,只要调用 ssc.checkpoint方法,设置一个hdfs 目录即可
需求 : 使用 SparkStreaming 整合DataFrame 完成词频统计分析
使用 Spark Streaming 整合 Spark Sql 完成 WordCount 统计
result.foreachRDD(rdd=>{
if(rdd.count() !=0){
// 得到sqlContext 实例
val sqlContext = SQLContextSington.getInstance(rdd.SparkContext)
import sqlContext.implicit._
val df = rdd.map(x=>word(x)).toDF
df.registerTempTable("tb_word")
// 使用SparkSQL 进行词频统计
sqlContext.sql("select word,count(*) from tb_word group by word").show
}
})
ssc.start()
ssc.waitTermination()
// SQLContext 获取实例单词类
object SQLContextSington {
@ transient private val instance:SQLContext=_
def getInstance(sparkContext:SparkContext):SQLContext={
if(instace==null){
instace = new SQLContext(SparkContext)
}
instance
}
}
case class Word(word:String)