ClickHouse / ClickHouse/clickhouse-java
How to read WITH TOTALS value from ClickHouseResultSet
- Lingua principale
- Java
- Stelle
- 1.6k
- Fork
- 636
- Merge medio
- 2g 23h
- PR unite (30g)
- 29
Descrizione
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 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
```
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Riproduci la query WITH TOTALS usando l'esempio JDBC e confronta le relative righe di ClickHouseResultSet con l'output HTTP e client mostrato. Inizia esaminando la gestione delle righe dei risultati in ClickHouseResultSet e ClickHouseStatement, quindi controlla i test o la documentazione JDBC esistenti. Il lavoro è completato quando il modo supportato per recuperare il valore di totals è documentato o coperto da un test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- java
- Ambito
- databases
- Tipo di issue
- Documentazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100