spark任务中基于rdd的执行流程分析

下面是rdd的论文中,从hdfs读取日志数据的一个例子:



lines = spark.textFile("hdfs://...")  // lines is a org.apache.spark.rdd.MappedRDD
errors = lines.filter(_.startsWith("ERROR")) // errors is a org.apache.spark.rdd.FilteredRDD
errors.cache() // persist 到内存中
errors.count() // 触发action,计算errors有多少个,即ERROR的多少行
// Count errors mentioning MySQL:
errors.filter(_.contains("MySQL")).count()
// Return the time fields of errors mentioning
// HDFS as an array (assuming time is field
// number 3 in a tab-separated format):
errors.filter(_.contains("HDFS"))
        .map(_.split('\t')(3))
        .collect()


spark是一个org.apache.spark.SparkContext的实例,spark的应用从定义一个SparkContext开始:
textFile的定义如下:

1
2
3
4
5
6
7
8
/**
   * Read a text file from HDFS, a local file system (available on all nodes), or any
   * Hadoop-supported file system URI, and return it as an RDD of Strings.
   */
  def textFile(path: String, minPartitions: Int = defaultMinPartitions): RDD[String] = {
    hadoopFile(path, classOf[TextInputFormat], classOf[LongWritable], classOf[Text],
      minPartitions).map(pair => pair._2.toString).setName(path)
  }

hadoopFile创建了一个org.apache.spark.rdd.HadoopRDD,
而在HadoopRDD上调用map则生成了一个MappedRDD: 
1
2
3
4
/**
  * Return a new RDD by applying a function to all elements of this RDD.
  */
def map[U: ClassTag](f: T => U): RDD[U] = new MappedRDD( this , sc.clean(f))

errors.cache()并不会立即执行,它的作用是在RDD的计算完成后,将结果cache起来,以供以后的计算使用,

这样的话可以加快以后运算的速度。

errors.count() 就触发了一个action,这个时候就需要向集群提交job了:

1
2
3
4
/**
  * Return the number of elements in the RDD.
  */
 def count(): Long = sc.runJob(this, Utils.getIteratorSize _).sum


提交后,SparkContext会将runJob提交到DAGScheduler,DAGScheduler会将当前的DAG划分成Stage,

然后生成TaskSet后通过TaskScheduler的submitTasks提交tasks,而这又会调用SchedulerBackend,

SchedulerBackend会将这些任务发送到Executor去执行。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值