天天看點

Spark大資料相關經典面試題總結 【一直更新...】

題目一: 想問一個問題:Spark Streaming 如何保證有序消費 kafka資料?topic多分區

如果是全局有序 kafka隻有在單partition才生效,多partitions不支援全局有序,或者比較難;

如果是局部有序 可以利用 相同的key映射到同一個partition的特點 保證 key内有序,

例如:指定key(比如order id),具有同1個key的所有消息,會發往同1個partition。也是有序的

參考:https://blog.csdn.net/bigtree_3721/article/details/80953197

2019-08-14
           
題目二:一個值得注意的點: val traffic = temp(2).trim.toLong

在做toLong 轉換的時候,一定要try catch

之前:  val traffic = temp(2).trim.toLong
			 
            之後:
	         var traffic = 0L  // 考慮到流量這個值可能髒資料,無法toLong
	          try{
	            traffic = temp(2).trim.toLong  // 考慮到空格的情況
	          }catch {
	            case e:Exception => traffic = 0L
	          }
           
2019-08-16
           
題目三:java.lang.OutOfMemoryError: GC overhead limit exceeded

at java.util.Arrays.copyOf(Arrays.java:3230)

/**
   * Returns a Java list that contains all rows in this Dataset.
   *
   * Running collect requires moving all the data into the application's driver process, and
   * doing so on a very large dataset can crash the driver process with OutOfMemoryError.
   *
   * @group action
   * @since 1.6.0
   */
  def collectAsList(): java.util.List[T] = withAction("collectAsList", queryExecution) { plan =>
    val values = collectFromPlan(plan)
    java.util.Arrays.asList(values : _*)
  }
           

參考:https://www.xttblog.com/?p=3347

題目四:以WordCount為例,分别畫出Spark和MapReduce執行流程
Spark大資料相關經典面試題總結 【一直更新...】

幾點說明:

A步驟:val lines = sc.textFile("") 從hdfs的block塊中讀取資料的時候,是按照預設分區為2 進行讀取,

即:1個RDD2個partition。

B步驟:val words = lines.flatMap(line => line.split(",")) flatMap本身就是将每一個輸入項映射到0個或多個輸出項(是以包含的是Seq 而不是單個項)

即:将資料打扁,一個二維資料搞成一維的

C步驟:val pairs = words.map(word => (word, 1)) Map是每一個元素都作用上相同的一個函數操作

D步驟:val wordClunts = pairs.reduceBykey(+) reduceBykey 算子會先在Map端做一個聚合【通過MapPartition操作完成】,然後再将聚合的資料進行shuffle操作

E步驟:wordClunts.collect().foreach(println) 将結果全部放入一個集合中,拉回到Driver端

Spark大資料相關經典面試題總結 【一直更新...】

參照https://blog.csdn.net/weixin_41227335/article/details/88364913

繼續閱讀