2017-10-28 36 views
-1

假設我試圖刪除這個正則表達式「RT \ s * @ USER \ w \ w {8}:\ s *」 並且我想在我的RDD中刪除這種形式的正則表達式。如何使用RDD去除PySpark中的某些正則表達式?

我現在RDD是:

text = sc.textFile(...) 
delimited = text.map(lambda x: x.split("\t")) 

和這裏就是我試圖刪除正則表達式的一部分。 我試着做下面的RDD轉換來擺脫每一個匹配這個正則表達式的字符串,但它都給我一個錯誤。

abc = delimited.map(lambda x: re.sub(r"RT\s*@USER\w\w{8}:\s*", " ", x)) 
TypeError: expected string or buffer 

abc = re.sub(r"RT\s*@USER\w\w{8}:\s*", " ", delimited) 
TypeError: expected string or buffer 

abc = delimited.map(lambda x: re.sub(r"RT\s*@USER\w\w{8}:\s*", " ", text)) 
Exception: It appears that you are attempting to broadcast an RDD or reference an RDD from an action or transformation. RDD transformations and actions can only be invoked by the driver, not inside of other transformations; for example, rdd1.map(lambda x: rdd2.values.count() * x) is invalid because the values transformation and count action cannot be performed inside of the rdd1.map transformation. For more information, see SPARK-5063. 

我想刪除這個正則表達式,這樣我可以繼續到下一個RDD轉換。我如何在PySpark中創建這段代碼?

回答

0

re.sub需要一個字符串。

  • 在第一個匿名函數:

    lambda x: re.sub(r"RT\s*@USER\w\w{8}:\s*", " ", x) 
    

    x是一個列表,因爲你在先前的變換分割線。

  • 在第二次嘗試,你傳遞一個RDD:delimeted

  • 在代碼的第三個片段傳遞另一個RDD:text

如果你想刪除你的列表中的每個元素這個正則表達式,試試這個:

abc = delimited.map(lambda l: [re.sub(r"RT\s*@USER\w\w{8}:\s*", " ", x) for x in l]) 
+0

非常感謝... – kys92