List getting empty

apache-spark

#1

The list is getting empty after the size function. below is the code and the result out put.

scala> val products = Source.fromFile ("/data/retail_db/products/part-00000").getLines
products: Iterator[String] = non-empty iterator

scala> products.take(3).foreach(println)
1,2,Quest Q64 10 FT. x 10 FT. Slant Leg Instant U,59.98,http://images.acmesports.sports/Quest+Q64+10+FT.+x+10+FT.+Slant+Leg+Instant+Up+Canopy
2,2,Under Armour Men’s Highlight MC Football Clea,129.99,http://images.acmesports.sports/Under+Armour+Men’s+Highlight+MC+Football+Cleat
3,2,Under Armour Men’s Renegade D Mid Football Cl,89.99,http://images.acmesports.sports/Under+Armour+Men’s+Renegade+D+Mid+Football+Cleat

scala> products.size
res5: Int = 1342

scala> products.take(3).foreach(println)

scala> products.size
res7: Int = 0


#2

Iterators are data structures that allows to iterate over a sequence of elements. They have a hasNext method for checking if there is a next element available, once the element is returned it will discard from the iterator. So, if you want to reuse the iterator again then you have to regenerate the iterator.