【慕课网实战】Spark Streaming实时流处理项目实战笔记十之铭文升级版
铭文一级:
第八章:Spark Streaming进阶与案例实战
updateStateByKey算子
需求:统计到目前为止累积出现的单词的个数(需要保持住以前的状态)
java.lang.IllegalArgumentException: requirement failed: The checkpoint directory has not been set. Please set it by StreamingContext.checkpoint().
需求:将统计结果写入到MySQL
create table wordcount(
word varchar(50) default null,
wordcount int(10) default null
);
通过该sql将统计结果写入到MySQL
insert into wordcount(word, wordcount) values('" + record._1 + "'," + record._2 + ")"
存在的问题:
1) 对于已有的数据做更新,而是所有的数据均为insert
改进思路:
a) 在插入数据前先判断单词是否存在,如果存在就update,不存在则insert
b) 工作中:HBase/Redis
2) 每个rdd的partition创建connection,建议大家改成连接池
window:定时的进行一个时间段内的数据处理
window length : 窗口的长度
sliding interval: 窗口的间隔
这2个参数和我们的batch size有关系:倍数
每隔多久计算某个范围内的数据:每隔10秒计算前10分钟的wc
==> 每隔sliding interval统计前window length的值
铭文二级:
第七章:Spark Streaming核心概念与编程
实战:Spark Streaming处理文件系统数据=>
与处理socket数据类似
1.建FileWordCount类
2.建监控的路径,本次为:/Users/rocky/data/imooc/ss
3.只需修改SocketTextStream成textFileStream
参数设置为file:///Users/rocky/data/imooc/ss/ /* 前面的“///”、最后的“/” */
4.vi test.log //里面有内容,然后cp到监控的路径
nc监控6789端口即可
注意事项:
官网Basic Sources
1、必须每次相同的文件格式
2、必须使用移动的方式将内容move到路径
3、一旦移动,无法再修改里面的内容
第八章:Spark Streaming进阶与案例实战
实战:使用UpdateStateByKey算子统计到目前为止累计出现的单词个数
copy一个NetworkWordCount类改成StatefulWordCount
步骤一、将reduceBykey改成UpdateStateByKey
官网代码(两个重要参数:newValues、running):
def updateFunction(newValues: Seq[Int], runningCount: Option[Int]): Option[Int] = {
val newCount = ... // add the new values with the previous running count to get the new count
Some(newCount)
}
步骤二、自定义代码:
def updateFunction(currentValues: Seq[Int], preValues: Option[Int]): Option[Int] = {
val current = currentValues.sum
val pre = preValues.getOrElse(0)
Some(current + pre)
}
步骤三、修改代码:
ssc.checkpoint(".") //一定要设置,运行后文件夹根目录会出现receivedBlockMetadata文件夹
ps:checkpoint一般生产上设置到HDFS的某个文件夹
val result = lines.flatMap(_.split(" ")).map((_,1))
val state = result.updateStateByKey[Int](updateFunction _)
state.print()
实战:计算到目前为止累计出现的单词个数写到mysql中:
ps:mysql知识复习
mysql -uroot -proot //登录mysql
create database imooc_spark; //建立imooc_spark数据库
use imooc_spark; //使用数据库
show tables; //查看表
select * from wordcount; //查看表内容
复制一个类文件(删掉UpdateStateByKey算子的相关内容)
步骤一、copy一个StatefulWordCount类改成ForeachRDDApp类
停掉之前运行的程序,删掉receivedBlockMetadata的文件内容
步骤二、在mysq建表wordcount
word varchar(50) default null,
wordcount int(10) default null
步骤三、提供的自定义代码:
package com.imooc.spark
import java.sql.DriverManager
import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}
/**
* 使用Spark Streaming完成词频统计,并将结果写入到MySQL数据库中
*/
object ForeachRDDApp {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setAppName("ForeachRDDApp").setMaster("local[2]")
val ssc = new StreamingContext(sparkConf, Seconds(5))
val lines = ssc.socketTextStream("localhost", 6789)
val result = lines.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _)
//result.print() //此处仅仅是将统计结果输出到控制台
//TODO... 将结果写入到MySQL
// result.foreachRDD(rdd =>{
// val connection = createConnection() // executed at the driver
// rdd.foreach { record =>
// val sql = "insert into wordcount(word, wordcount) values('"+record._1 + "'," + record._2 +")"
// connection.createStatement().execute(sql)
// }
// })
result.print()
result.foreachRDD(rdd => {
rdd.foreachPartition(partitionOfRecords => {
val connection = createConnection()
partitionOfRecords.foreach(record => {
val sql = "insert into wordcount(word, wordcount) values('" + record._1 + "'," + record._2 + ")"
connection.createStatement().execute(sql)
})
connection.close()
})
})
ssc.start()
ssc.awaitTermination()
}
/**
* 获取MySQL的连接
*/
def createConnection() = {
Class.forName("com.mysql.jdbc.Driver")
DriverManager.getConnection("jdbc:mysql://localhost:3306/imooc_spark", "root", "root")
}
}
报错分析:
1、connection.createStatement().execute(sql)//没有驱动包,自己引入
2、第一种官网连接会报序列化错误,自己改成partition式连接,如上面代码
3、重复执行,mysql数据库的列名会重复出现,自行使用Hbase或redis等数据库
4、改成连接池的方式
官网代码参考:
dstream.foreachRDD { rdd =>
rdd.foreachPartition { partitionOfRecords =>
// ConnectionPool is a static, lazily initialized pool of connections
val connection = ConnectionPool.getConnection()
partitionOfRecords.foreach(record => connection.send(record))
ConnectionPool.returnConnection(connection) // return to the pool for future reuse
}
}
实战:窗口函数的使用(摘自官网)
val windowedWordCounts = pairs.reduceByKeyAndWindow((a:Int,b:Int) => (a + b), Seconds(30), Seconds(10))
【慕课网实战】Spark Streaming实时流处理项目实战笔记十之铭文升级版的更多相关文章
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记七之铭文升级版
铭文一级: 第五章:实战环境搭建 Spark源码编译命令:./dev/make-distribution.sh \--name 2.6.0-cdh5.7.0 \--tgz \-Pyarn -Phado ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记十四之铭文升级版
铭文一级: 第11章 Spark Streaming整合Flume&Kafka打造通用流处理基础 streaming.conf agent1.sources=avro-sourceagent1 ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记二之铭文升级版
铭文一级: 第二章:初识实时流处理 需求:统计主站每个(指定)课程访问的客户端.地域信息分布 地域:ip转换 Spark SQL项目实战 客户端:useragent获取 Hadoop基础课程 ==&g ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记十六之铭文升级版
铭文一级: linux crontab 网站:http://tool.lu/crontab 每一分钟执行一次的crontab表达式: */1 * * * * crontab -e */1 * * * ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记十五之铭文升级版
铭文一级:[木有笔记] 铭文二级: 第12章 Spark Streaming项目实战 行为日志分析: 1.访问量的统计 2.网站黏性 3.推荐 Python实时产生数据 访问URL->IP信息- ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记十二之铭文升级版
铭文一级: ======Pull方式整合 Flume Agent的编写: flume_pull_streaming.conf simple-agent.sources = netcat-sources ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记十一之铭文升级版
铭文一级: 第8章 Spark Streaming进阶与案例实战 黑名单过滤 访问日志 ==> DStream20180808,zs20180808,ls20180808,ww ==> ( ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记九之铭文升级版
铭文一级: 核心概念:StreamingContext def this(sparkContext: SparkContext, batchDuration: Duration) = { this(s ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记八之铭文升级版
铭文一级: Spark Streaming is an extension of the core Spark API that enables scalable, high-throughput, ...
随机推荐
- Query to find the eligible indexes for rebuilding
Query to find the eligible indexes for rebuilding The following script can be used to determine whic ...
- 9.13 h5日记
9.13 面试题 为什么两个P出此案的效果不同,原因是 浏览器在解析第二个P的时候,因为字母没有空格,浏览器会认为这个单词没有打完,所以不会换行. 列表 ul ol dl li 1.无序列表 ul ( ...
- sharpsvn 继续,解决文件locked 问题,
方法中少个方法就会出现一些问题. 比如进行了断线测试,结果再操作时就出现了文件被锁的情况,最终查了官网的论坛,才得以解决 How to unlock if the working copy is lo ...
- Numpy array 合并
1.np.vstack() :垂直合并 >>> import numpy as np >>> A = np.array([1,1,1]) >>> ...
- SumatraPDF默认配置文件备份
background color of the non-document windows, traditionally yellow MainWindowBackground = #fff200 if ...
- VS2010插件 VS.PHP 调试开发php程序
VS 插件VS.PHP 调试PHP的方法;不得不说vs强大啊,此断点调试功能在zend都做不到 如图: 设置成功之后,就可以像调试 .Net程序一样试调Php程序了! 调试的步骤: 1.在需要调试的地 ...
- Netty Reator(三)Reactor 模型
Netty Reator(三)Reactor 模型 Netty 系列目录 (https://www.cnblogs.com/binarylei/p/10117436.html) 本文介绍 DC Sch ...
- java指针与引用(转载)
大家都知道java和C#中没有指针这个概念.但是也导致了编程中常常忽略了对象与引用的区别,难道java真的没有指针吗?句柄是什么?变量地址在哪里?没有地址是不可能的,关键是java中如何避免了指针这个 ...
- MySQL中varchar与char区别
MySQL中varchar与char区别(转) MySQL中varchar最大长度是多少? 一. varchar存储规则: 4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字 ...
- NETSHARP的JAVA开发环境配置
一:JAVA配置 1. netsharp使用java1.8/1.7版本,本文使用1.8版本 2.jdk下载地址:http://www.oracle.com/technetwork/java/javas ...