簡體   English   中英

如何遍歷行的迭代器

[英]How to iterate through an Iterator of Rows

我想迭代我的迭代器的前幾行,我正在嘗試像下面這樣迭代所有文件:

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);
}


    

我的問題是如何將迭代限制為僅 20 次? 謝謝

我建議使用 int 計數並在計數完成時中斷:

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;
    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM