@@ -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