Skip to content

Commit 6c9cb9a

Browse files
committed
added records query example
1 parent c30c346 commit 6c9cb9a

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

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: 37 additions & 2 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;
@@ -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

Comments
 (0)