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 ;
@@ -53,7 +54,7 @@ public boolean isServerAlive() {
5354 return client .ping ();
5455 }
5556
56- public void readData () {
57+ public void readDataUsingBinaryFormat () {
5758 try {
5859 // Read data from the table
5960 log .info ("Reading data from table: {}" , TABLE_NAME );
@@ -78,4 +79,38 @@ public void readData() {
7879 log .error ("Failed to read data" , e );
7980 }
8081 }
82+
83+ public void readDataAll () {
84+ try {
85+ log .info ("Reading whole table and process record by record" );
86+ final String sql = "select * from " + TABLE_NAME + " where title <> ''" ;
87+ client .queryAll (sql ).forEach (row -> {
88+ double id = row .getDouble ("id" );
89+ String title = row .getString ("title" );
90+ String url = row .getString ("url" );
91+
92+ log .info ("id: {}, title: {}, url: {}" , id , title , url );
93+ });
94+ } catch (Exception e ) {
95+ log .error ("Failed to read data" , e );
96+ }
97+ }
98+
99+ public void readData () {
100+ try {
101+ log .info ("Reading data from table: {} using Records iterator" , TABLE_NAME );
102+ final String sql = "select * from " + TABLE_NAME + " where title <> '' limit 10" ;
103+ Records records = client .queryRecords (sql ).get (3 , TimeUnit .SECONDS );
104+
105+ for (GenericRecord record : records ) {
106+ double id = record .getDouble ("id" );
107+ String title = record .getString ("title" );
108+ String url = record .getString ("url" );
109+
110+ log .info ("id: {}, title: {}, url: {}" , id , title , url );
111+ }
112+ } catch (Exception e ) {
113+ log .error ("Failed to read data" , e );
114+ }
115+ }
81116}
0 commit comments