0
votes

I have mdx query and which results data in 2 columns. How do I read the data in second column using CellSet in java. I'm using CellSet.getcell(index) to read data from the 1st column in the mdx query.

| Column1 | Column2 |

| 111 | 222 |

1

1 Answers

0
votes

CellSet.getCell(index) actually allows you to access any cell, not just those in the first column. If there are 2 columns and 3 rows, then the cell ordinals are as follows:

       Col0  Col1
 Row0     0     1
 Row1     2     3
 Row2     4     5

As you see, to read the first column ("Col0") you need to read indexes 0, 2, 4, and to read the second column ("Col1") you need to read indexes 1, 3, 5.

If this raster-scan arrangement makes your head hurt, try using the cellSet.getCell(List<Integer>) API instead. It allows you to specify independent coordinates for each axis. For example, to read row 2, column 1, write cellSet.getCell(Arrays.asList(2, 1)). Remember that all indexes are 0-based, so the top left corner is (0, 0).

To print the values of the second column (column #1, 0-based), write the following:

CellSet cellSet;
CellSetAxis rowsAxis = cellSet.getAxes().get(1);
for (int i = 0; i < rowsAxis.getPositionCount(); i++) {
  Cell cell = cellSet.getCell(Arrays.asList(1, i));
  System.out.println(cell.getFormattedValue());
}