Skip to content

Commit 2c98389

Browse files
committed
fix client to accept configuration
1 parent 35359bb commit 2c98389

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

examples/client/src/main/java/com/clickhouse/examples/formats/ProtobufMain.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import com.clickhouse.client.ClickHouseClient;
44
import com.clickhouse.client.ClickHouseConfig;
5+
import com.clickhouse.client.ClickHouseCredentials;
6+
import com.clickhouse.client.ClickHouseException;
57
import com.clickhouse.client.ClickHouseNode;
68
import com.clickhouse.client.ClickHouseNodeSelector;
79
import com.clickhouse.client.ClickHouseProtocol;
810
import com.clickhouse.client.ClickHouseRequest;
911
import com.clickhouse.client.ClickHouseResponse;
12+
import com.clickhouse.client.config.ClickHouseClientOption;
1013
import com.clickhouse.client.http.config.ClickHouseHttpOption;
1114
import com.clickhouse.config.ClickHouseOption;
1215
import com.clickhouse.data.ClickHouseFormat;
@@ -27,14 +30,10 @@
2730
public class ProtobufMain {
2831

2932
private static final Logger log = Logger.getLogger(ProtobufMain.class.getName());
30-
private final String host = "localhost";
31-
private final int port = 8123;
3233

3334
public void write(List<Message> messages) {
3435
try (ClickHouseClient client = getClient()) {
35-
ClickHouseRequest.Mutation mutation =
36-
client.write(ClickHouseNode.builder().host(host).port(ClickHouseProtocol.HTTP, port).build());
37-
36+
ClickHouseRequest.Mutation mutation = client.write(getServer());
3837
mutation.table("default.ui_events");
3938
mutation.format(ClickHouseFormat.Protobuf);
4039
ClickHouseResponse response = mutation.data((out) -> {
@@ -55,8 +54,7 @@ public void write(List<Message> messages) {
5554

5655
public List<Message> read() {
5756
try (ClickHouseClient client = getClient()) {
58-
ClickHouseRequest request =
59-
client.read(ClickHouseNode.builder().host(host).port(ClickHouseProtocol.HTTP, port).build());
57+
ClickHouseRequest request = client.read(getServer());
6058

6159
request.table("default.ui_events");
6260
request.format(ClickHouseFormat.Protobuf);
@@ -70,6 +68,9 @@ public List<Message> read() {
7068
}
7169

7270
return messages;
71+
} catch (ClickHouseException e) {
72+
log.log(Level.SEVERE, "Failed to read data from server: " + getClient().getConfig(), e);
73+
throw new RuntimeException(e);
7374
} catch (Exception e) {
7475
log.log(Level.SEVERE, "Failed to write data", e);
7576
throw new RuntimeException(e);
@@ -115,4 +116,14 @@ protected ClickHouseClient getClient() {
115116
.nodeSelector(ClickHouseNodeSelector.of(ClickHouseProtocol.HTTP))
116117
.build();
117118
}
119+
120+
protected ClickHouseNode getServer() {
121+
return ClickHouseNode.builder()
122+
.host(System.getProperty("chHost", "localhost"))
123+
.addOption(ClickHouseClientOption.SSL.getKey(), String.valueOf(Boolean.getBoolean("chSsl")))
124+
.port(ClickHouseProtocol.HTTP, Integer.getInteger("chPort", 8123))
125+
.credentials(ClickHouseCredentials.fromUserAndPassword(
126+
System.getProperty("chUser", "default"), System.getProperty("chPassword", "")))
127+
.build();
128+
}
118129
}

0 commit comments

Comments
 (0)