|
| 1 | +package com.clickhouse.examples.formats; |
| 2 | + |
| 3 | +import com.clickhouse.client.ClickHouseClient; |
| 4 | +import com.clickhouse.client.ClickHouseConfig; |
| 5 | +import com.clickhouse.client.ClickHouseCredentials; |
| 6 | +import com.clickhouse.client.ClickHouseException; |
| 7 | +import com.clickhouse.client.ClickHouseNode; |
| 8 | +import com.clickhouse.client.ClickHouseNodeSelector; |
| 9 | +import com.clickhouse.client.ClickHouseProtocol; |
| 10 | +import com.clickhouse.client.ClickHouseRequest; |
| 11 | +import com.clickhouse.client.ClickHouseResponse; |
| 12 | +import com.clickhouse.client.config.ClickHouseClientOption; |
| 13 | +import com.clickhouse.client.http.config.ClickHouseHttpOption; |
| 14 | +import com.clickhouse.config.ClickHouseOption; |
| 15 | +import com.clickhouse.data.ClickHouseFormat; |
| 16 | +import com.clickhouse.data.ClickHouseInputStream; |
| 17 | +import com.clickhouse.examples.protos.UIEvent; |
| 18 | +import com.google.protobuf.Message; |
| 19 | + |
| 20 | + |
| 21 | +import java.io.Serializable; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.logging.Level; |
| 27 | +import java.util.logging.Logger; |
| 28 | + |
| 29 | + |
| 30 | +public class ProtobufMain { |
| 31 | + |
| 32 | + private static final Logger log = Logger.getLogger(ProtobufMain.class.getName()); |
| 33 | + |
| 34 | + public void write(List<Message> messages) { |
| 35 | + try (ClickHouseClient client = getClient()) { |
| 36 | + ClickHouseRequest.Mutation mutation = client.write(getServer()); |
| 37 | + mutation.table("default.ui_events"); |
| 38 | + mutation.format(ClickHouseFormat.Protobuf); |
| 39 | + ClickHouseResponse response = mutation.data((out) -> { |
| 40 | + for (Message message : messages) { |
| 41 | + // it is important to write the size of the message before the message itself |
| 42 | + out.writeVarInt(message.getSerializedSize()); |
| 43 | + message.writeTo(out); |
| 44 | + } |
| 45 | + }).executeAndWait(); |
| 46 | + |
| 47 | + |
| 48 | + log.info("Response: " + response.getSummary()); |
| 49 | + } catch (Exception e) { |
| 50 | + log.log(Level.SEVERE, "Failed to write data", e); |
| 51 | + throw new RuntimeException(e); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public List<Message> read() { |
| 56 | + try (ClickHouseClient client = getClient()) { |
| 57 | + ClickHouseRequest request = client.read(getServer()); |
| 58 | + |
| 59 | + request.table("default.ui_events"); |
| 60 | + request.format(ClickHouseFormat.Protobuf); |
| 61 | + request.set("format_protobuf_use_autogenerated_schema", 1); // use the schema generated by ClickHouse |
| 62 | + |
| 63 | + ClickHouseResponse response = request.executeAndWait(); |
| 64 | + List<Message> messages = new ArrayList<>(); |
| 65 | + ClickHouseInputStream inputStream = response.getInputStream(); |
| 66 | + while (inputStream.available() > 0) { |
| 67 | + messages.add(UIEvent.parseDelimitedFrom(inputStream)); |
| 68 | + } |
| 69 | + |
| 70 | + 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); |
| 74 | + } catch (Exception e) { |
| 75 | + log.log(Level.SEVERE, "Failed to write data", e); |
| 76 | + throw new RuntimeException(e); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + public static void main(String[] args) { |
| 82 | + |
| 83 | + List<Message> messages = new ArrayList<>(); |
| 84 | + |
| 85 | + messages.add(UIEvent.newBuilder() |
| 86 | + .setUrl("http://example.com") |
| 87 | + .setUserId("user1") |
| 88 | + .setSessionId("session1") |
| 89 | + .setTimestamp(System.currentTimeMillis()) |
| 90 | + .setEvent("visit") |
| 91 | + .build()); |
| 92 | + |
| 93 | + messages.add(UIEvent.newBuilder() |
| 94 | + .setUrl("http://example.com") |
| 95 | + .setUserId("user1") |
| 96 | + .setSessionId("session1") |
| 97 | + .setTimestamp(System.currentTimeMillis()) |
| 98 | + .setDuration(1000) |
| 99 | + .setEvent("leave") |
| 100 | + .build()); |
| 101 | + |
| 102 | + |
| 103 | + ProtobufMain writer = new ProtobufMain(); |
| 104 | + writer.write(messages); |
| 105 | + |
| 106 | + List<Message> storedMessages = writer.read(); |
| 107 | + for (Message message : storedMessages) { |
| 108 | + log.info("Message: " + message); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + protected ClickHouseClient getClient() { |
| 113 | + Map<ClickHouseOption, Serializable> map = new HashMap<>(); |
| 114 | + map.put(ClickHouseHttpOption.CUSTOM_PARAMS, "format_protobuf_use_autogenerated_schema=1"); |
| 115 | + return ClickHouseClient.builder().config(new ClickHouseConfig(map)) |
| 116 | + .nodeSelector(ClickHouseNodeSelector.of(ClickHouseProtocol.HTTP)) |
| 117 | + .build(); |
| 118 | + } |
| 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 | + } |
| 129 | +} |
0 commit comments