第102讲: 动手实战Spark Streaming自定义Receiver并进行调试和测试
来源:互联网 发布:现在windows出到几了 编辑:程序博客网 时间:2024/05/19 15:40
有兴趣想学习国内整套Spark+Spark Streaming+Machine learning顶级课程的,可加我qq 471186150。共享视频,性价比超高!
1:SparkStreaming虽然说已经支持了很多不同类型的数据来源。但是有时候可能我们的一些数据来源非常特殊 ,不是它天然默认支持的,这时候就要自定义Receiver。而自定义Receiver,一般都是基于网络的方式。因为你传数据的话,一般是从另外一个网络端口传过来,至于传的协议是另外一码事。
2:从本质上来说,SparkStreaming中的所有Receiver,都是自定义的Receiver。所以你要想自定义一个Receiver,最最简单的方式,你就是看下已有的Receiver怎么去实现。
具体步骤:http://spark.apache.org/docs/latest/streaming-custom-receivers.html
class CustomReceiver(host: String, port: Int) extends Receiver[String](StorageLevel.MEMORY_AND_DISK_2) with Logging { def onStart() { // Start the thread that receives data over a connection new Thread("Socket Receiver") { override def run() { receive() }//开启线程调receiver()方法 }.start() } def onStop() { // There is nothing much to do as the thread calling receive() // is designed to stop by itself if isStopped() returns false } /** Create a socket connection and receive data until receiver is stopped */ private def receive() { var socket: Socket = null var userInput: String = null try { // Connect to host:port,Receiver的时候就连上Socket socket = new Socket(host, port) // Until stopped or connection broken continue reading val reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")) userInput = reader.readLine() while(!isStopped && userInput != null) { store(userInput) userInput = reader.readLine()//每读一行,存一次,一直循环 } reader.close() socket.close() // Restart in an attempt to connect again when server is active again restart("Trying to connect again")//先stop,然后再start() } catch { case e: java.net.ConnectException => // restart if could not connect to server restart("Error connecting to " + host + ":" + port, e) case t: Throwable => // restart if there is any other error restart("Error receiving data", t) } }}
上面就已经自定义完了一个Receiver,下面就new出它的对象,传进去。因为返回的是Dstream,以前对Dstream怎么操作,继续怎么操作,这里先从flatMap开始。// Assuming ssc is the StreamingContextval customReceiverStream = ssc.receiverStream(new CustomReceiver(host, port))val words = lines.flatMap(_.split(" "))...
0 0
- 第102讲: 动手实战Spark Streaming自定义Receiver并进行调试和测试
- 大数据IMF传奇行动绝密课程第102课:动手实战Spark Streaming自定义Receiver并进行调试和测试
- 第90讲,Spark streaming基于kafka 以Receiver方式获取数据 原理和案例实战
- 第82讲:Spark Streaming第一讲:案例动手实战并在电光石火间理解其工作原理
- 第103讲: 动手实战联合使用Spark Streaming、Broadcast、Accumulator实现在线黑名单过滤和计数
- 第82课:Spark Streaming第一课:案例动手实战并在电光石火间理解其工作原理
- 第103课:动手实战联合使用Spark Streaming、Broadcast、Accumulator实现在线黑名单过滤和计数
- 第103课:动手实战联合使用Spark Streaming、Broadcast、Accumulator实现在线黑名单过滤和计数
- 第83讲:Scala和Java二种方式实战Spark Streaming开发
- 第89讲:Spark Streaming on Kafka解析和安装实战
- 第93讲:Spark Streaming updateStateByKey案例实战和内幕源码
- IMF传奇行动第82课:Spark Streaming第一课:案例动手实战并在电光石火间理解其工作原理
- 大数据IMF传奇行动绝密课程第82课:Spark Streaming案例动手实战并在电光石火间理解其工作原理
- 0083.使用Scala和Java实战Spark Streaming开发第二讲Spark Streaming
- 图解StreamingContext、DStream、Receiver 第三讲spark streaming
- Spark streaming基于kafka 以Receiver方式获取数据 原理和案例实战
- Spark streaming基于kafka 以Receiver方式获取数据 原理和案例实战
- Spark定制班第31课:集群运行模式下的Spark Streaming调试和难点解决实战经验分享
- 【HTML/CSS】浮动结构深入浅出
- android对话框自动弹出软键盘
- Linux系统下find指令详细用法与示例及Linux下的atime ,ctime,mtime
- 类型转换、输入与错误
- 多进程中的SharedPreferences
- 第102讲: 动手实战Spark Streaming自定义Receiver并进行调试和测试
- THUCTC源码解读(三)
- android异步消息机制,源码层面彻底解析(一)
- EM算法总结
- LeetCode 94. Binary Tree Inorder Traversal(二叉树的中序遍历)
- C++基础知识(三)—— 常量
- Android+ eclipse开发快捷键
- stm32资源
- 剑指offerJava实现 二维数组的查找