Spark Streaming监控HDFS输入流
来源:互联网 发布:大数据培训 达内就业 编辑:程序博客网 时间:2024/05/29 09:48
Spark Streaming实时监控HDFS文件系统写入的数据。
以下是相关代码和注解以及特殊方法的源代码注释:
object SourceHdfs { /** * Definition HDFS checkpoint directory address */ val checkpointDirectory = "hdfs://master:9000/sparkStreaming/Checkpoint_Data" def main(args: Array[String]): Unit = { /** * Either recreate a StreamingContext from checkpoint data or create a new StreamingContext. */ val context = StreamingContext.getOrCreate(checkpointDirectory, createContext _) /** * Configure inputDStream source that HDFS address * No Receiver, SparkStreaming application monitor batch by timer */ val DStream = context.textFileStream("hdfs://master:9000/quality/clipper_erp/2017-07-11") val wordCount = DStream.flatMap(_.split(" ")).map(word => (word, 1)).reduceByKey(_+_) wordCount.print() context.start() context.awaitTermination() } /** * Create spark streamingContext function for getOrCreate method */ def createContext(): StreamingContext ={ val conf = new SparkConf() .setAppName("HDFSInputData") .setMaster("spark://master:7077") val ssc = new StreamingContext(conf, Seconds(10)) ssc.checkpoint(checkpointDirectory) ssc }}
以下是完整代码的地址(https://github.com/DragonTong/Streaming/blob/master/src/main/scala/streaming/SourceHdfs.scala)
1. checkpoint 保持RDD状态和容错
/** 1. Set the context to periodically checkpoint the DStream operations for driver 2. fault-tolerance. 3. @param directory HDFS-compatible directory where the checkpoint data will be reliably stored. 4. Note that this must be a fault-tolerant file system like HDFS. */def checkpoint(directory: String) { if (directory != null) { val path = new Path(directory) val fs = path.getFileSystem(sparkContext.hadoopConfiguration) fs.mkdirs(path) val fullPath = fs.getFileStatus(path).getPath().toString sc.setCheckpointDir(fullPath) checkpointDir = fullPath } else { checkpointDir = null } }
2. getOrCreate 创建StreamingContext
/** * Either recreate a StreamingContext from checkpoint data or create a new StreamingContext. * If checkpoint data exists in the provided `checkpointPath`, then StreamingContext will be * recreated from the checkpoint data. If the data does not exist, then the StreamingContext * will be created by called the provided `creatingFunc`. * * @param checkpointPath Checkpoint directory used in an earlier StreamingContext program * @param creatingFunc Function to create a new StreamingContext * @param hadoopConf Optional Hadoop configuration if necessary for reading from the * file system * @param createOnError Optional, whether to create a new StreamingContext if there is an * error in reading checkpoint data. By default, an exception will be * thrown on error. */ def getOrCreate( checkpointPath: String, creatingFunc: () => StreamingContext, hadoopConf: Configuration = SparkHadoopUtil.get.conf, createOnError: Boolean = false ): StreamingContext = { val checkpointOption = CheckpointReader.read( checkpointPath, new SparkConf(), hadoopConf, createOnError) checkpointOption.map(new StreamingContext(null, _, null)).getOrElse(creatingFunc()) }
编译程序,在集群环境下运行:
spark-submit –class streaming.SourceHdfs –master spark://master:7077 spark.streaming.pro-1.0-SNAPSHOT.jar
注:第二次运行时会报错需要把Checkpoint_Data文件夹里面的内容删除。深入理解Checkpoint后再解决这个BUG。
报错内容:
Exception in thread "main" org.apache.spark.SparkException: org.apache.spark.streaming.dstream.ShuffledDStream@321ca237 has not been initialized at org.apache.spark.streaming.dstream.DStream.isTimeValid(DStream.scala:312) at org.apache.spark.streaming.dstream.DStream$$anonfun$getOrCompute$1.apply(DStream.scala:333)at org.apache.spark.streaming.dstream.DStream$$anonfun$getOrCompute$1.apply(DStream.scala:333) at scala.Option.orElse(Option.scala:289) at org.apache.spark.streaming.dstream.DStream.getOrCompute(DStream.scala:330) at org.apache.spark.streaming.dstream.ForEachDStream.generateJob(ForEachDStream.scala:48) at org.apache.spark.streaming.DStreamGraph$$anonfun$1.apply(DStreamGraph.scala:117)at org.apache.spark.streaming.DStreamGraph$$anonfun$1.apply(DStreamGraph.scala:116) at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:241)at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:241) at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59) at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48) at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:241) at scala.collection.AbstractTraversable.flatMap(Traversable.scala:104) at org.apache.spark.streaming.DStreamGraph.generateJobs(DStreamGraph.scala:116) at org.apache.spark.streaming.scheduler.JobGenerator$$anonfun$restart$4.apply(JobGenerator.scala:234)at org.apache.spark.streaming.scheduler.JobGenerator$$anonfun$restart$4.apply(JobGenerator.scala:229) at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33) at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186) at org.apache.spark.streaming.scheduler.JobGenerator.restart(JobGenerator.scala:229) at org.apache.spark.streaming.scheduler.JobGenerator.start(JobGenerator.scala:98) at org.apache.spark.streaming.scheduler.JobScheduler.start(JobScheduler.scala:102) at org.apache.spark.streaming.StreamingContext$$anonfun$liftedTree1$1$1.apply$mcV$sp(StreamingContext.scala:583)at org.apache.spark.streaming.StreamingContext$$anonfun$liftedTree1$1$1.apply(StreamingContext.scala:578) at org.apache.spark.streaming.StreamingContext$$anonfun$liftedTree1$1$1.apply(StreamingContext.scala:578)at ... run in separate thread using org.apache.spark.util.ThreadUtils ... ()at org.apache.spark.streaming.StreamingContext.liftedTree1$1(StreamingContext.scala:578)at org.apache.spark.streaming.StreamingContext.start(StreamingContext.scala:572)at streaming.SourceHdfs$.main(SourceHdfs.scala:21)at streaming.SourceHdfs.main(SourceHdfs.scala)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:738) at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:187) at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:212) at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:126) at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)17/07/11 19:19:27 INFO spark.SparkContext: Invoking stop() from shutdown hook
阅读全文
0 0
- Spark Streaming监控HDFS输入流
- Spark Streaming 监控HDFS目录
- spark streaming读取HDFS
- Spark-Streaming 程序监控
- spark streaming 应用程序监控
- spark streaming 写入db,hdfs
- spark streaming 写hdfs问题
- spark streaming job 耗时监控
- 7.Spark Streaming:输入DStream之基础数据源以及基于HDFS的实时wordcount程序
- Spark Streaming场景应用- Spark Streaming计算模型及监控
- Python爬虫之Spark Streaming任务监控
- Python爬虫之Spark Streaming任务监控
- Spark Streaming 实时监控一个HDFS的文件夹,当新的文件进来(名字不能重复),将对新文件进行处理。
- Spark Streaming + Kafka + Opencv + Face Recognizer + HDFS Sequence File + Mysql
- Spark之Streaming实时监听Hdfs文件目录
- 实时计算实践(spark streaming+kafka+hdfs)
- [Spark]Spark Streaming 指南四 输入DStreams和Receivers
- Spark Streaming(下)--实时流计算Spark Streaming实战
- kotlin 学习笔记
- python3实现计算器基本功能
- C语言不匹配转换
- C#命名规则和编码规范
- x86汇编语言(三)
- Spark Streaming监控HDFS输入流
- 使用JS添加删除select选项
- BPMN2.0
- 解决apache启动错误:Could not reliably determine the server's fully qualified domain name
- grep及正则表达式
- Akka(13): 分布式运算:Cluster-Sharding-运算的集群分片
- 关于Mpush 消息推送 出现的问题
- C# SQL2008数据库存储中的存储过程例子
- UVA