Skip to content

How to read WITH TOTALS value from ClickHouseResultSet #2692

@chenxin57085122

Description

@chenxin57085122

I'm using ClickHouse JDBC driver (version 0.4.6) to execute queries with WITH TOTALS clause.
How can I read this value

my java code:

    public static void readWithTotal1() {
        final String sql = "select host,count() AS count from default.nullarr001 GROUP BY host with TOTALS";

        final String field1 = "host";
        final String field2 = "count";

        Connection connection = null;
        ClickHouseStatement statement = null;
        ClickHouseResultSet resultSet = null;

        try {
            Properties properties = new Properties();
            properties.setProperty("user", CLICKHOUSE_USER);
            properties.setProperty("password", CLICKHOUSE_PASSWORD);
            properties.setProperty("database", CLICKHOUSE_DB);

            String jdbcUrl = "jdbc:clickhouse://" + CLICKHOUSE_HOST + ":" + CLICKHOUSE_PORT + "?ssl=true&sslmode=none";
            System.out.println(jdbcUrl);

            Class.forName("com.clickhouse.jdbc.ClickHouseDriver");
            connection = DriverManager.getConnection(jdbcUrl, properties);

            statement = (ClickHouseStatement) connection.createStatement();
            resultSet = (ClickHouseResultSet) statement.executeQuery(sql);
            ClickHouseRequest request = statement.getRequest();
            Optional<String> queryIdNorNull = request.getQueryId();
            if (queryIdNorNull.isPresent()) {
                System.out.println("queryId is " + queryIdNorNull.get());
            }

            while (resultSet.next()) {
                String host = resultSet.getString(field1);
                String count = resultSet.getString(field2);
                System.out.printf("host=%s count=%s", host, count);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (statement != null) statement.close();
                if (connection != null) connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

mynullsql.nullarr001 table structure

CREATE TABLE default.nullarr001
(
    host String
)
ENGINE = MergeTree
ORDER BY host;``

INSERT INTO default.nullarr001 VALUES('host1'),('host2'),('host3');

select host,count() from default.nullarr001 GROUP BY host with TOTALS;

clickhouse-cilent execute sql
select host,count() from default.nullarr001 GROUP BY host with TOTALS;

SELECT
host,
count()
FROM default.nullarr001
GROUP BY host
WITH TOTALS

Query id: 71cb91ee-5415-4a26-8a15-960be58f3c02

┌─host──┬─count()─┐
│ host1 │ 1 │
│ host2 │ 1 │
│ host3 │ 1 │
└───────┴─────────┘

Totals:
┌─host─┬─count()─┐
│ │ 3 │
└─────┴──────┘

http result

curl -u admin:Admin_123 -sX POST "localhost:8123/" \
  -H "Content-Type: text/plain" \
  --data "SELECT host,count() FROM nullarr001 GROUP BY host WITH TOTALS LIMIT 1"

host1    1

    200

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions