marc nicole
marc nicole

Reputation: 83

How to iterate through an Iterator of Rows

I want to iterate over the few first Rows of my iterator , i am trying like the following to iterate over all the file:

Dataset<Row> DF = spark.read().format("csv").load("longfile.csv");
Iterator<Row> iter = DF.toLocalIterator();


while (iter.hasNext()) { // i want a condition here to iterate only over the 20 first lines of my CSV
    Row item = iter.next();
    System.out.println(item);
}


    

My question is how to limit the iteration to only 20 ? THanks

Upvotes: 1

Views: 722

Answers (1)

Med Elgarnaoui
Med Elgarnaoui

Reputation: 1667

I suggest to use a int count and break when the count is completed :

Dataset<Row> DF = spark.read().format("csv").load("longfile.csv");
Iterator<Row> iter = DF.toLocalIterator();

int count = 0;

while (iter.hasNext()) { // i want a condition here to iterate only over the 20 first lines of my CSV
    count++;
    Row item = iter.next();
    System.out.println(item);
    if(count > 20) break;
    
}

Upvotes: 1

Related Questions