2016-02-09 46 views
0

我在JavaRDD集合上調用isEmpty時遇到Apache Spark問題,即使集合爲空,它也會返回false。Apache Spark isEmpty false,但集合爲空

這裏的示例代碼(修改,因爲它從我的最後一年的項目是和我不能發表任何代碼):

sampleRdd = inputRdd.filter(someFilterFunction) 
if(sampleRdd.isEmpty()) { 
     return inputRdd.first(); 
} else { 
     return sampleRdd.first(); // JVM points error on this line 
} 

問題是有時條件是假的所以sampleRdd.isEmpty()返回false這意味着它因此不爲空執行過程進行到return語句在那裏它試圖檢索收集的first()元素,但它拋出異常:

Exception in thread "main" java.lang.UnsupportedOperationException: empty collection 
at org.apache.spark.rdd.RDD$$anonfun$first$1.apply(RDD.scala:1314) 
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:147) 
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:108) 
at org.apache.spark.rdd.RDD.withScope(RDD.scala:306) 
at org.apache.spark.rdd.RDD.first(RDD.scala:1311) 
at org.apache.spark.api.java.JavaRDDLike$class.first(JavaRDDLike.scala:510) 
at org.apache.spark.api.java.AbstractJavaRDDLike.first(JavaRDDLike.scala:47) 
. 
. 
. 

有一點我很想念?我目前在本地機器上運行它,因爲它還沒有完全開發。

感謝

編輯:要添加更多信息JVM點到線sampleRdd.first()當我得到這個錯誤,所以初始inputRdd不爲空

EDIT2:我寫的過濾器之前打印的inputRDD大小一些多餘的線條和sampleRDD後,這樣的過濾器:

System.out.println(inputRdd.count()); // Returns nonzero possitive int eg.100 
sampleRdd = inputRdd.filter(someFilterFunction) 
System.out.println(sampleRdd.count()); // Returns int eg. 1 
System.out.println(sampleRdd.count()); // Sometimes returns different int than first call eg.3 
if(sampleRdd.isEmpty()) { 
     return inputRdd.first(); 
} else { 
     return sampleRdd.first(); // JVM points error on this line 
} 

而且我發現很有趣的現象,那就是有時inputRdd.count()回報100但杉st sampleRdd.count()返回1,第二個sampleRdd.count()返回3或與第一個呼叫基本不同的號碼。所以基本上看起來像兩個調用之間的變化大小sampleRdd,因此我認爲有時它可能會改變爲在傳遞條件並試圖調用first()返回錯誤。

任何想法可能是什麼原因造成的?

+0

嗨,也許你可以讓println確認它在'sampleRdd.first()'行失敗,你也可以使用count()方法來看看它會返回什麼。 – Hlib

+0

是的,我已經做了,有時計數()返回數字> 0,它仍然失敗從sampleRdd檢索元素。這裏的問題我猜是它不是按順序評估它,或者它在過濾器完成之前評估isEmpty或者其他東西 – MichaelDD

+0

也許你也可以做sampleRdd.collect.get(0)? (我知道這是不一樣的) – Hlib

回答

1

如果inputRdd最初是空的,該怎麼辦?

在這種情況下,sampleRdd也是空的。因此samplerdd.isEmpty評估爲trueinputRdd.first()拋出異常。

+0

噢,是的,我忘記指出錯誤是在線sampleRdd.first()被調用(至少這是JVM指向的地方) – MichaelDD

+0

只是爲了澄清'inputRdd'你的意思是它沒有元素或元素都是'空'嗎?因爲我在執行過濾器之前試着對count()進行計數,並且我得到了正數非零計數,所以'inputRdd'中肯定有元素 – MichaelDD