22
33import com .clickhouse .client .ClickHouseClient ;
44import com .clickhouse .client .ClickHouseConfig ;
5+ import com .clickhouse .client .ClickHouseCredentials ;
6+ import com .clickhouse .client .ClickHouseException ;
57import com .clickhouse .client .ClickHouseNode ;
68import com .clickhouse .client .ClickHouseNodeSelector ;
79import com .clickhouse .client .ClickHouseProtocol ;
810import com .clickhouse .client .ClickHouseRequest ;
911import com .clickhouse .client .ClickHouseResponse ;
12+ import com .clickhouse .client .config .ClickHouseClientOption ;
1013import com .clickhouse .client .http .config .ClickHouseHttpOption ;
1114import com .clickhouse .config .ClickHouseOption ;
1215import com .clickhouse .data .ClickHouseFormat ;
2730public 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