我在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()
返回錯誤。
任何想法可能是什麼原因造成的?
嗨,也許你可以讓println確認它在'sampleRdd.first()'行失敗,你也可以使用count()方法來看看它會返回什麼。 – Hlib
是的,我已經做了,有時計數()返回數字> 0,它仍然失敗從sampleRdd檢索元素。這裏的問題我猜是它不是按順序評估它,或者它在過濾器完成之前評估isEmpty或者其他東西 – MichaelDD
也許你也可以做sampleRdd.collect.get(0)? (我知道這是不一樣的) – Hlib