How to get value of a cell by column name in java using azure-kusto-java library

395 Views Asked by At
public KustoResultSetTable executeKustoQuery(ClientImpl client, String query) {

    KustoResultSetTable mainTableResult = null;
    try {
        KustoOperationResult results = client.execute(databaseName, query);
        mainTableResult = results.getPrimaryResults();
    } catch (DataServiceException | DataClientException e) {

        errorHandler(e, "Error while retrieving results from kusto query!");
    }
    return mainTableResult;
}

The above code returns me a result of this type

Name    | Age
XYZ AAA | 29

How can I get the value of 1st row under name column using Azure Kusto Java mainTableResult object

Expected String output - "XYZ AAA"

1

There are 1 best solutions below

0
Tomer Shetah On BEST ANSWER

You can do:

if (mainTableResult.first()) {
    int columnIndex = mainTableResult.findColumn("Name")
    return mainTableResult.getString(columnIndex);
} else {
    throw new UnsupportedOperationException(""); // Or any other error handling
}

A complete version is:

public String executeKustoQuery(ClientImpl client, String query) {

    KustoResultSetTable mainTableResult = null;
    try {
        KustoOperationResult results = client.execute("databaseName", query);
        mainTableResult = results.getPrimaryResults();
        if (mainTableResult.first()) {
            int columnIndex = mainTableResult.findColumn("Name")
            return mainTableResult.getString(columnIndex);
        } else {
            throw new UnsupportedOperationException(""); // Or any other error handling
        }
    } catch (DataServiceException | DataClientException e) {

        errorHandler(e, "Error while retrieving results from kusto query!");
    }
}