44import com .clickhouse .client .api .data_formats .ClickHouseBinaryFormatReader ;
55import com .clickhouse .client .api .data_formats .RowBinaryWithNamesAndTypesFormatReader ;
66import com .clickhouse .client .api .metrics .ClientMetrics ;
7+ import com .clickhouse .client .api .query .GenericRecord ;
78import com .clickhouse .client .api .query .QueryResponse ;
89import com .clickhouse .client .api .query .QuerySettings ;
9- import com .clickhouse .data . ClickHouseFormat ;
10+ import com .clickhouse .client . api . query . Records ;
1011import lombok .extern .slf4j .Slf4j ;
1112
1213import 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