skipcheck

TM1 client · Walkthrough 2 of 3

Read and write cells

You read a few values from SData's SalesCube with an MDX query, write a budget value, and read it back.

  • A client of a TM1 server with IBM's SData sample. Connect and list your cubes shows how to make one. The steps here call it tm1.
  • SalesCube's dimensions, in order: actvsbud (Actual and Budget), region (countries), model (car models), account1 (Units, Sales and more) and month.
  1. Read with MDX. An MDX query picks the cells: here, the units of one model sold in France from January to March. TM1 answers with a cellset, which stays on the server until you delete it.

    // Units of one model sold in France, January to March
    String mdx = """
            SELECT {[month].[month].[Jan], [month].[month].[Feb], [month].[month].[Mar]} ON COLUMNS
            FROM [SalesCube]
            WHERE ([actvsbud].[actvsbud].[Actual], [region].[region].[France],
              [model].[model].[S Series 1.8 L Sedan], [account1].[account1].[Units])""";
    // TM1 keeps the result as a cellset: get its cells' values with it,
    // then delete it
    Cellset cellset = tm1.cellsets()
        .execute(mdx, ODataQuery.none().expand(Cellset.CELLS, cells -> cells.select(CellsetCell.VALUE)))
        .block();
    List<String> months = List.of("Jan", "Feb", "Mar");
    for (int i = 0; i < months.size(); i++) {
        System.out.println(months.get(i) + ": " + cellset.cells().get(i).value());
    }
    tm1.cellsets().delete(cellset.id()).block();

    Output

    Jan: 1410
    Feb: 1526
    Mar: 1567
    # Units of one model sold in France, January to March
    mdx = """
        SELECT {[month].[month].[Jan], [month].[month].[Feb], [month].[month].[Mar]} ON COLUMNS
        FROM [SalesCube]
        WHERE ([actvsbud].[actvsbud].[Actual], [region].[region].[France],
          [model].[model].[S Series 1.8 L Sedan], [account1].[account1].[Units])"""
    # TM1 keeps the result as a cellset: get its cells' values with it, then delete it
    cellset = tm1.cellsets.execute(mdx, ODataQuery.none().expand(Cellset.CELLS, lambda cells: cells.select(CellsetCell.VALUE)))
    for month, cell in zip(["Jan", "Feb", "Mar"], cellset.cells, strict=True):
        print(f"{month}: {cell.value}")
    tm1.cellsets.delete(cellset.id)

    Output

    Jan: 1410
    Feb: 1526
    Mar: 1567
    // Units of one model sold in France, January to March
    var mdx = """
        SELECT {[month].[month].[Jan], [month].[month].[Feb], [month].[month].[Mar]} ON COLUMNS
        FROM [SalesCube]
        WHERE ([actvsbud].[actvsbud].[Actual], [region].[region].[France],
          [model].[model].[S Series 1.8 L Sedan], [account1].[account1].[Units])
        """;
    // TM1 keeps the result as a cellset: get its cells' values with it, then delete it
    var cellset = await tm1.Cellsets.ExecuteAsync(mdx, ODataQuery.None().Expand(Cellset.Fields.Cells, cells => cells.Select(CellsetCell.Fields.Value)));
    string[] months = ["Jan", "Feb", "Mar"];
    for (var i = 0; i < months.Length; i++)
    {
        Console.WriteLine($"{months[i]}: {cellset.Cells![i].Value}");
    }
    await tm1.Cellsets.DeleteAsync(cellset.Id!);

    Output

    Jan: 1410
    Feb: 1526
    Mar: 1567
    // Units of one model sold in France, January to March
    const mdx = `
      SELECT {[month].[month].[Jan], [month].[month].[Feb], [month].[month].[Mar]} ON COLUMNS
      FROM [SalesCube]
      WHERE ([actvsbud].[actvsbud].[Actual], [region].[region].[France],
        [model].[model].[S Series 1.8 L Sedan], [account1].[account1].[Units])`;
    // TM1 keeps the result as a cellset: get its cells' values with it, then delete it
    const cellset = await tm1.cellsets.execute(
      mdx,
      ODataQuery.none().expand(Cellset.Fields.Cells, (cells) => cells.select(CellsetCell.Fields.Value)),
    );
    const months = ["Jan", "Feb", "Mar"];
    months.forEach((month, i) => console.log(`${month}: ${String(cellset.Cells![i]!.Value)}`));
    await tm1.cellsets.delete(cellset.ID!);

    Output

    Jan: 1410
    Feb: 1526
    Mar: 1567
  2. Write a cell. Name an element of each dimension, in the cube's order, and give the value. The cell must be a leaf: TM1 doesn't spread a value over a consolidation, such as a quarter.

    // A cell is an element of each of the cube's dimensions, in the cube's order
    Map<String, String> cell = new LinkedHashMap<>();
    cell.put("actvsbud", "Budget");
    cell.put("region", "France");
    cell.put("model", "S Series 1.8 L Sedan");
    cell.put("account1", "Units");
    cell.put("month", "Jan");
    tm1.cells().update("SalesCube", cell, 150).block();
    # A cell is an element of each of the cube's dimensions, in the cube's order
    cell = {"actvsbud": "Budget", "region": "France", "model": "S Series 1.8 L Sedan", "account1": "Units", "month": "Jan"}
    tm1.cells.update("SalesCube", cell, 150)
    // A cell is an element of each of the cube's dimensions, in the cube's order
    var cell = new Dictionary<string, string>
    {
        ["actvsbud"] = "Budget",
        ["region"] = "France",
        ["model"] = "S Series 1.8 L Sedan",
        ["account1"] = "Units",
        ["month"] = "Jan",
    };
    await tm1.Cells.UpdateAsync("SalesCube", cell, 150);
    // A cell is an element of each of the cube's dimensions, in the cube's order
    const cell = { actvsbud: "Budget", region: "France", model: "S Series 1.8 L Sedan", account1: "Units", month: "Jan" };
    await tm1.cells.update("SalesCube", cell, 150);
  3. Read it back. The same kind of query, for the cell you wrote:

    String mdx = "SELECT {[month].[month].[Jan]} ON COLUMNS FROM [SalesCube]"
            + " WHERE ([actvsbud].[actvsbud].[Budget], [region].[region].[France],"
            + " [model].[model].[S Series 1.8 L Sedan], [account1].[account1].[Units])";
    Cellset cellset = tm1.cellsets()
        .execute(mdx, ODataQuery.none().expand(Cellset.CELLS, cells -> cells.select(CellsetCell.VALUE)))
        .block();
    System.out.println("Budget for Jan: " + cellset.cells().get(0).value());
    tm1.cellsets().delete(cellset.id()).block();

    Output

    Budget for Jan: 150
    mdx = (
        "SELECT {[month].[month].[Jan]} ON COLUMNS FROM [SalesCube]"
        " WHERE ([actvsbud].[actvsbud].[Budget], [region].[region].[France],"
        " [model].[model].[S Series 1.8 L Sedan], [account1].[account1].[Units])"
    )
    cellset = tm1.cellsets.execute(mdx, ODataQuery.none().expand(Cellset.CELLS, lambda cells: cells.select(CellsetCell.VALUE)))
    print(f"Budget for Jan: {cellset.cells[0].value}")
    tm1.cellsets.delete(cellset.id)

    Output

    Budget for Jan: 150
    var mdx =
        "SELECT {[month].[month].[Jan]} ON COLUMNS FROM [SalesCube]"
        + " WHERE ([actvsbud].[actvsbud].[Budget], [region].[region].[France],"
        + " [model].[model].[S Series 1.8 L Sedan], [account1].[account1].[Units])";
    var cellset = await tm1.Cellsets.ExecuteAsync(mdx, ODataQuery.None().Expand(Cellset.Fields.Cells, cells => cells.Select(CellsetCell.Fields.Value)));
    Console.WriteLine($"Budget for Jan: {cellset.Cells![0].Value}");
    await tm1.Cellsets.DeleteAsync(cellset.Id!);

    Output

    Budget for Jan: 150
    const mdx =
      "SELECT {[month].[month].[Jan]} ON COLUMNS FROM [SalesCube]" +
      " WHERE ([actvsbud].[actvsbud].[Budget], [region].[region].[France]," +
      " [model].[model].[S Series 1.8 L Sedan], [account1].[account1].[Units])";
    const cellset = await tm1.cellsets.execute(
      mdx,
      ODataQuery.none().expand(Cellset.Fields.Cells, (cells) => cells.select(CellsetCell.Fields.Value)),
    );
    console.log(`Budget for Jan: ${String(cellset.Cells![0]!.Value)}`);
    await tm1.cellsets.delete(cellset.ID!);

    Output

    Budget for Jan: 150
  • Write many cells in one request with the cell service, or by position in a cellset with the cellset service.
  • Read a large query a page at a time with the cellset service's stream.
  • Try changes out without touching the cube's data, in a sandbox.