forked from ClickHouse/clickhouse-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
167 lines (131 loc) · 5.76 KB
/
app.cpp
File metadata and controls
167 lines (131 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// app.cpp
#include "clickhouse/client.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
static std::string OpenSSLErrorString() {
unsigned long err = ERR_get_error();
if (err == 0) return {};
char buf[256];
ERR_error_string_n(err, buf, sizeof(buf));
return std::string(buf);
}
int main() {
using clickhouse::Client;
using clickhouse::ClientOptions;
using clickhouse::Block;
using clickhouse::ColumnUInt64;
using clickhouse::ColumnString;
using clickhouse::ExternalTables;
// ---- Connection parameters (adjust to your environment) ----
const std::string host = "clickhouse.liveaction.com";
const int port = 9440; // native TLS port (commonly 9440)
const std::string user = "default";
const std::string password = "clickhouse";
// Path to the CA certificate (or CA bundle) that signed the server cert.
const std::string ca_pem = "/home/mandrews/clickhouse-pinned-ca-bundle.pem";
// Optional: for mutual TLS (mTLS). Leave empty if not required.
const std::string client_cert_file = "";
const std::string client_key_file = "";
try {
// Initialize OpenSSL (safe even on newer OpenSSL where it's mostly no-op)
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
using CtxPtr = std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>;
CtxPtr ctx(SSL_CTX_new(TLS_client_method()), &SSL_CTX_free);
if (!ctx) {
throw std::runtime_error("SSL_CTX_new failed: " + OpenSSLErrorString());
}
// Enforce verification of the server certificate
SSL_CTX_set_verify(ctx.get(), SSL_VERIFY_PEER, nullptr);
// Load CA used to verify the server certificate
if (SSL_CTX_load_verify_locations(ctx.get(), ca_pem.c_str(), nullptr) != 1) {
throw std::runtime_error("SSL_CTX_load_verify_locations failed: " + OpenSSLErrorString());
}
#if defined(USE_MTLS)
// Load client cert/key for mTLS
if (SSL_CTX_use_certificate_file(ctx.get(), client_cert_pem.c_str(), SSL_FILETYPE_PEM) != 1) {
throw std::runtime_error("SSL_CTX_use_certificate_file failed: " + OpenSSLErrorString());
}
if (SSL_CTX_use_PrivateKey_file(ctx.get(), client_key_pem.c_str(), SSL_FILETYPE_PEM) != 1) {
throw std::runtime_error("SSL_CTX_use_PrivateKey_file failed: " + OpenSSLErrorString());
}
if (SSL_CTX_check_private_key(ctx.get()) != 1) {
throw std::runtime_error("SSL_CTX_check_private_key failed: " + OpenSSLErrorString());
}
#endif
// Build ClickHouse options
ClientOptions opts;
opts.SetHost(host)
.SetPort(port)
.SetUser(user)
.SetPassword(password);
// ---- Enable TLS and specify CA cert manually ----
ClientOptions::SSLOptions ssl;
// Important: if you use an external SSL_CTX, prefer not to also set CA files
// in SSLOptions (to avoid ambiguity). Put trust material in the SSL_CTX (above).
ssl.SetUseDefaultCALocations(false);
// Attach the external context (this is the key fix for your version)
ssl.SetExternalSSLContext(ctx.get());
opts.SetSSLOptions(ssl);
// ---- Create client and run a simple query ----
Client client(opts);
client.Execute("CREATE TABLE IF NOT EXISTS default.numbers (id UInt64, name String) ENGINE = Memory");
/// Insert some values.
{
Block block;
auto id = std::make_shared<ColumnUInt64>();
id->Append(1);
id->Append(7);
auto name = std::make_shared<ColumnString>();
name->Append("one");
name->Append("seven");
block.AppendColumn("id" , id);
block.AppendColumn("name", name);
client.Insert("default.numbers", block);
}
/// Select values inserted in the previous step.
client.Select("SELECT id, name FROM default.numbers", [] (const Block& block)
{
for (size_t i = 0; i < block.GetRowCount(); ++i) {
std::cout << block[0]->As<ColumnUInt64>()->At(i) << " "
<< block[1]->As<ColumnString>()->At(i) << "\n";
}
}
);
/// Select values inserted in the previous step using external data feature
/// See https://clickhouse.com/docs/engines/table-engines/special/external-data
{
Block block1, block2;
auto id = std::make_shared<ColumnUInt64>();
id->Append(1);
block1.AppendColumn("id" , id);
auto name = std::make_shared<ColumnString>();
name->Append("seven");
block2.AppendColumn("name", name);
const std::string _1 = "_1";
const std::string _2 = "_2";
const ExternalTables external = {{_1, block1}, {_2, block2}};
client.SelectWithExternalData("SELECT id, name FROM default.numbers where id in (_1) or name in (_2)",
external, [] (const Block& block)
{
for (size_t i = 0; i < block.GetRowCount(); ++i) {
std::cout << block[0]->As<ColumnUInt64>()->At(i) << " "
<< block[1]->As<ColumnString>()->At(i) << "\n";
}
}
);
}
/// Delete table.
client.Execute("DROP TABLE default.numbers");
return 0;
} catch (const std::exception& e) {
std::cerr << "ClickHouse error: " << e.what() << "\n";
return 1;
}
}