簡體   English   中英

了解Spark執行中的DAG

[英]Understanding the DAG in Spark Execution

我想在Spark中運行代碼時更好地理解DAG執行。 我知道Spark是懶惰評估的,當我們執行任何操作(如count,show,cache)時,它會運行轉換命令。

但是我想知道在DAG執行這些操作的時間有多遠。

就像我在預測數據框上編寫以下命令一樣。

sorted_predictions=predictions.orderBy(['user','prediction'],ascending=[1,0])

def mapIdToString(x):
    """ This function takes in the predicted dataframe and adds the original Item string to it

    """

global data_map_var
d_map=data_map_var.value
data_row= x.asDict()
#print data_row

for name, itemID in d_map.items():
    if data_row['item']== itemID:
        return (data_row['user'],data_row['item'],name,data_row['rating'],data_row['prediction'])


sorted_rdd=sorted_predictions.map(mapIdToString)
In [20]:

sorted_rdd.take(5)
Out[20]:
[(353, 21, u'DLR_Where Dreams Come True Town Hall', 0, 0.896152913570404),
 (353, 2, u'DLR_Leading at a Higher Level', 1, 0.7186800241470337),
 (353,
  220,
  u'DLR_The Year of a Million Dreams Leadership Update',
  0,
  0.687175452709198),
 (353, 1, u'DLR_Challenging Conversations', 1, 0.6632049083709717),
 (353,
  0,
  u'DLR_10 Keys to Inspiring, Engaging, and Energizing Your People',
  1,
  0.647541344165802)]

sorted_df=sqlContext.createDataFrame(sorted_rdd,['user','itemId','itemName','rating','prediction'])


sorted_df.registerTempTable("predictions_df")

query = """ 
      select * from predictions_df 
      where user =353 
      and rating =0
      """
items_recommended=sqlContext.sql(query)

現在,當我運行以下命令時,我期待它,因為它是一個小查詢,它應該快速運行。 但是提供輸出需要很多時間。 看起來它一直到達DAG的頂部並再次執行所有的事情?

我不明白,因為當我執行sorted_rdd.take(5)命令時DAG會被破壞。 因此,如果我現在運行以下命令,那么執行此命令之后的所有內容都將執行,而不是之前

items_recommended.count()

那為什么它運行了一個小時? 我使用60個執行器和5個核心。 Sorted_rdd有450MM行。

EDIT1:

這是大衛回答的后續行動。 可以說我有以下命令。

對數據幀進行排序

sorted_predictions=predictions.orderBy(['user','prediction'],ascending=[1,0])
sorted_predictions.show(20)

sorted_rdd=sorted_predictions.map(mapIdToString)

sorted_rdd.take(5)

你是說每次我用.take()運行最后一個命令它會回到第一個orderBy並再次對數據框進行排序並再次運行所有命令嗎? 即使我做了sorted_prediction.show()來執行早期的排序命令?

編輯二:

如果我有如下功能:

def train_test_split(self,split_perc):

    """ This function takes the DataFrame/RDD of ratings and splits 
    it into Training, Validation and testing based on the splitting 
    percentage passed as parameters

    Param: ratings Dataframe of Row[(UserID,ItemID,ratings)]
    Returns: train, validation, test

    """

   # Converting the RDD back to dataframe to be used in DataFrame ml API

    #ratings=sqlContext.createDataFrame(split_sdf,["user", "item", "rating"])

    random_split=self.ratings_sdf.randomSplit(split_perc,seed=20)

    #return random_split[0],random_split[1],random_split[2]

    self.train=random_split[0]
    self.train.cache().count()

    # Converting the ratings column to float values for Validation and Test data
    self.validation=random_split[1].withColumn('rating',(random_split[1].rating>0).astype('double'))
    self.test=random_split[2].withColumn('rating',(random_split[2].rating>0).astype('double'))

    self.validation.cache().count()
    self.test.cache()

這個功能基本上是將數據幀分成train,val和test。 我將在機器學習任務中使用所有三個,因此將使用火車訓練算法和val進行超參數調整。

所以我在上面全部三個緩存。 但是為了使緩存可執行,我在所有三個上都執行了.count。 但是現在這個功能需要花費很多時間。 你認為這三個都需要一個.count,或者我可以在一個(test.count()上執行.count,它會執行上面函數中的所有命令,並且還會緩存train和val數據框嗎?我認為應該工作並且不需要三次計數?

我不明白,因為當我執行sorted_rdd.take(5)命令時DAG會被破壞。 所以在執行此命令之后的任何事情都將被執行而不是之前

Spark的懶惰評估擴展到將內容存儲在內存中。 除非您顯式cache()中間數據,否則它不會這樣做。 如果沒有cache()調用,Spark也需要在take(5)調用之前重新處理所有步驟。 為了解決這個問題,緩存你RDD在之前take這樣的行動

 sorted_rdd.cache().take(5)

解決編輯問題

你是說每次我用.take()運行最后一個命令它會回到第一個orderBy並再次對數據框進行排序並再次運行所有命令嗎? 即使我做了sorted_prediction.show()來執行早期的排序命令?

正確。 在下面的代碼中,Spark將需要運行所有步驟來創建predictions ,以及orderBy計算以顯示20行的sorted_predictions 然后它將運行所有步驟來創建predictionsorderBy計算和map計算以顯示5行sorted_rdd

sorted_predictions=predictions.orderBy(['user','prediction'],ascending=[1,0])
sorted_predictions.show(20)

sorted_rdd=sorted_predictions.map(mapIdToString)
sorted_rdd.take(5)

從評論

我認為緩存也是一個動作

緩存本身不是一個動作。 這是一個將RDD / DataFrame存儲在內存中的指令,但實際上這不會發生,直到一個動作運行(例如,count,take,show等)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM