Skip to content

Commit 9ca7fc7

Browse files
committed
Added more query examples
1 parent 9302048 commit 9ca7fc7

File tree

2 files changed

+24
-7
lines changed
  • client-v2/src/main/java/com/clickhouse/client/api
  • examples/client-v2/src/main/java/com/clickhouse/examples/client_v2

2 files changed

+24
-7
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,9 +790,10 @@ public CompletableFuture<Records> queryRecords(String sqlQuery, QuerySettings se
790790

791791
/**
792792
* <p>Queries data in descriptive format and reads result to a collection.</p>
793-
* <p>Use this method for queries that would return only a few records only.</p>
794-
* @param sqlQuery
795-
* @return
793+
* <p>Use this method for queries that would return only a few records only because client
794+
* will read whole dataset and convert it into a list of GenericRecord</p>
795+
* @param sqlQuery - SQL query
796+
* @return - complete list of records
796797
*/
797798
public List<GenericRecord> queryAll(String sqlQuery) {
798799
try {
@@ -859,10 +860,18 @@ public String toString() {
859860
'}';
860861
}
861862

863+
/**
864+
* Returns unmodifiable map of configuration options.
865+
* @return - configuration options
866+
*/
862867
public Map<String, String> getConfiguration() {
863868
return Collections.unmodifiableMap(configuration);
864869
}
865870

871+
/**
872+
* Returns unmodifiable set of endpoints.
873+
* @return - set of endpoints
874+
*/
866875
public Set<String> getEndpoints() {
867876
return Collections.unmodifiableSet(endpoints);
868877
}

examples/client-v2/src/main/java/com/clickhouse/examples/client_v2/SimpleReader.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ public class SimpleReader {
3535

3636
Client client;
3737

38-
String database;
39-
4038
public SimpleReader(String endpoint, String user, String password, String database) {
4139
// Create a lightweight object to interact with ClickHouse server
4240
Client.Builder clientBuilder = new Client.Builder()
@@ -47,7 +45,6 @@ public SimpleReader(String endpoint, String user, String password, String databa
4745
.setDefaultDatabase(database);
4846

4947
this.client = clientBuilder.build();
50-
this.database = database;
5148
}
5249

5350
public boolean isServerAlive() {
@@ -60,13 +57,17 @@ public void readDataUsingBinaryFormat() {
6057
log.info("Reading data from table: {}", TABLE_NAME);
6158

6259
final String sql = "select * from " + TABLE_NAME + " where title <> '' limit 10";
60+
6361
// Default format is RowBinaryWithNamesAndTypesFormatReader so reader have all information about columns
6462
QueryResponse response = client.query(sql).get(3, TimeUnit.SECONDS);
63+
6564
// Create a reader to access the data in a convenient way
6665
ClickHouseBinaryFormatReader reader = new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream());
6766

6867
while (reader.hasNext()) {
69-
reader.next();
68+
reader.next(); // Read the next record from stream and parse it
69+
70+
// get values
7071
double id = reader.getDouble("id");
7172
String title = reader.getString("title");
7273
String url = reader.getString("url");
@@ -84,6 +85,8 @@ public void readDataAll() {
8485
try {
8586
log.info("Reading whole table and process record by record");
8687
final String sql = "select * from " + TABLE_NAME + " where title <> ''";
88+
89+
// Read whole result set and process it record by record
8790
client.queryAll(sql).forEach(row -> {
8891
double id = row.getDouble("id");
8992
String title = row.getString("title");
@@ -102,6 +105,11 @@ public void readData() {
102105
final String sql = "select * from " + TABLE_NAME + " where title <> '' limit 10";
103106
Records records = client.queryRecords(sql).get(3, TimeUnit.SECONDS);
104107

108+
// Get some metrics
109+
log.info("Data read successfully: {} ms", TimeUnit.NANOSECONDS.toMillis(records.getServerTime()));
110+
log.info("Total rows: {}", records.getResultRows());
111+
112+
// Iterate thru records
105113
for (GenericRecord record : records) {
106114
double id = record.getDouble("id");
107115
String title = record.getString("title");

0 commit comments

Comments
 (0)