Skip to content

Commit ae04bf0

Browse files
authored
Merge pull request #1709 from ClickHouse/add-more-v2-examples
Add more v2 examples
2 parents abe31c6 + 9ca7fc7 commit ae04bf0

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
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/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public static void main(String[] args) {
3737

3838
// Read data back
3939
SimpleReader reader = new SimpleReader(endpoint, user, password, database);
40+
reader.readDataUsingBinaryFormat();
41+
reader.readDataAll();
4042
reader.readData();
4143

4244
// Insert data using POJO

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

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
55
import com.clickhouse.client.api.data_formats.RowBinaryWithNamesAndTypesFormatReader;
66
import com.clickhouse.client.api.metrics.ClientMetrics;
7+
import com.clickhouse.client.api.query.GenericRecord;
78
import com.clickhouse.client.api.query.QueryResponse;
89
import com.clickhouse.client.api.query.QuerySettings;
9-
import com.clickhouse.data.ClickHouseFormat;
10+
import com.clickhouse.client.api.query.Records;
1011
import lombok.extern.slf4j.Slf4j;
1112

1213
import java.util.concurrent.Future;
@@ -34,8 +35,6 @@ public class SimpleReader {
3435

3536
Client client;
3637

37-
String database;
38-
3938
public SimpleReader(String endpoint, String user, String password, String database) {
4039
// Create a lightweight object to interact with ClickHouse server
4140
Client.Builder clientBuilder = new Client.Builder()
@@ -46,26 +45,29 @@ public SimpleReader(String endpoint, String user, String password, String databa
4645
.setDefaultDatabase(database);
4746

4847
this.client = clientBuilder.build();
49-
this.database = database;
5048
}
5149

5250
public boolean isServerAlive() {
5351
return client.ping();
5452
}
5553

56-
public void readData() {
54+
public void readDataUsingBinaryFormat() {
5755
try {
5856
// Read data from the table
5957
log.info("Reading data from table: {}", TABLE_NAME);
6058

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

6767
while (reader.hasNext()) {
68-
reader.next();
68+
reader.next(); // Read the next record from stream and parse it
69+
70+
// get values
6971
double id = reader.getDouble("id");
7072
String title = reader.getString("title");
7173
String url = reader.getString("url");
@@ -78,4 +80,45 @@ public void readData() {
7880
log.error("Failed to read data", e);
7981
}
8082
}
83+
84+
public void readDataAll() {
85+
try {
86+
log.info("Reading whole table and process record by record");
87+
final String sql = "select * from " + TABLE_NAME + " where title <> ''";
88+
89+
// Read whole result set and process it record by record
90+
client.queryAll(sql).forEach(row -> {
91+
double id = row.getDouble("id");
92+
String title = row.getString("title");
93+
String url = row.getString("url");
94+
95+
log.info("id: {}, title: {}, url: {}", id, title, url);
96+
});
97+
} catch (Exception e) {
98+
log.error("Failed to read data", e);
99+
}
100+
}
101+
102+
public void readData() {
103+
try {
104+
log.info("Reading data from table: {} using Records iterator", TABLE_NAME);
105+
final String sql = "select * from " + TABLE_NAME + " where title <> '' limit 10";
106+
Records records = client.queryRecords(sql).get(3, TimeUnit.SECONDS);
107+
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
113+
for (GenericRecord record : records) {
114+
double id = record.getDouble("id");
115+
String title = record.getString("title");
116+
String url = record.getString("url");
117+
118+
log.info("id: {}, title: {}, url: {}", id, title, url);
119+
}
120+
} catch (Exception e) {
121+
log.error("Failed to read data", e);
122+
}
123+
}
81124
}

0 commit comments

Comments
 (0)