What is the correct usage of TableFilter (or is there a other solution available?)

Hi
I’m working on a Node that receives a lot of data (many rows) as input. I was looking for a way to not loop through all rows, but only looping through a defined selection. The best solution for me would be to get a specific row, something like:

BufferedDataTable inputTable = inData[0];
DataRow row = inputTable.getRow(10);

But such a function doesn’t exists and I don’t think there is an other solution for this? If there is, please let me know.

My second best shot was to use the TableFilter. I believe this filter is meant to select a certain range from the inputTable. So I tried to used it as follows:

TableFilter filter = TableFilter.filterRowsFromIndex(10);
CloseableRowIterator myiterator = inputTable.filter(filter).iterator();
while (myiterator.hasNext()) {
    DataRow currentRow = rowIterator.next();
    DataCell cell = currentRow.getCell(1);
    System.out.println(cell);
}

I noticed that the first row retrieved by this iterator is simply the first row in the inputTable, not the 10th as I defined in the TableFilter. Am I using the TableFilter in a wrong way? Any idea why it is not working? Or is there an other solution for my issue?

KR
Ashgard

1 Like

Hi @ashgard,

I asked a developer in our team about this, they should be getting back to you soon.

best,
Gabriel

Thanks for asking.
If they have an other solution for my issues then I am also happy with that.
Looking forward to their response.

Hi @ashgard,

Sorry for my delayed response. The TableFilter is indeed intended for what you are trying to accomplish. I am not yet sure why it does not work for you. The only two things I’d like to point out regarding the code snippet you provided are

  1. In line 2, you are creating a CloseableRowIterator called “myIterator”, but in line 4, you are referring to an iterator called “rowIterator”.
  2. The CloseableRowIterator should be managed with try-with-resources.

I just tried the following code snipped for me locally and it did in fact filter out the first 10 rows:

final BufferedDataTable inputTable = inData[0];
final TableFilter filter = TableFilter.filterRowsFromIndex(10);
try (CloseableRowIterator myIterator = inputTable.filter(filter).iterator()) {
    while (myIterator.hasNext()) {
        final DataRow currentRow = myIterator.next();
        final DataCell cell = currentRow.getCell(1);
        System.out.println(cell);
    }
}

What Table Storage format did you configure in File -> Preferences -> KNIME -> Data Storage?

Finally, it might also be worth pointing out that we will release a new Table API for improved performance with KNIME 4.3. With that new API, the code snippet you provided would look roughly like so:

final BufferedDataTable inputTable = inData[0];
final TableFilter filter = TableFilter.filterRowsFromIndex(10);
try (final RowCursor inCursor = inputTable.cursor()) {
    while (inCursor.canForward()) {
        final RowRead row = inCursor.forward();
        final IntValue i = row.getValue(1);
        System.out.println(i.getIntValue());
    }
}

Regards,

Marc

3 Likes

Hi @marc-bux

Thanks for your reply. Indeed you are right. I should have used myIterator rather then rowIterator. That solved my issues. I guess I complete overlooked this typo.

And good to hear that this is the desired solution for my issue.

Thanks again.
Ashgard