forked from questdb/c-questdb-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline_sender_cpp_example.cpp
83 lines (71 loc) · 2.27 KB
/
line_sender_cpp_example.cpp
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
#include <questdb/ilp/line_sender.hpp>
#include <iostream>
using namespace std::literals::string_view_literals;
using namespace questdb::ilp::literals;
static bool example(std::string_view host, std::string_view port)
{
try
{
questdb::ilp::line_sender sender{host, port};
// We prepare all our table names and colum names in advance.
// If we're inserting multiple rows, this allows us to avoid
// re-validating the same strings over and over again.
auto table_name = "cpp_cars"_name;
auto id_name = "id"_name;
auto x_name = "x"_name;
auto y_name = "y"_name;
auto booked_name = "booked"_name;
auto passengers_name = "passengers"_name;
auto driver_name = "driver"_name;
sender
.table(table_name)
.symbol(id_name, "d6e5fe92-d19f-482a-a97a-c105f547f721"_utf8)
.column(x_name, 30.5)
.column(y_name, -150.25)
.column(booked_name, true)
.column(passengers_name, int64_t{3})
.column(driver_name, "Ranjit Singh"_utf8)
.at_now();
// To insert more records, call `sender.table(..)...` again.
sender.flush();
return true;
}
catch (const questdb::ilp::line_sender_error& err)
{
std::cerr
<< "Error running example: "
<< err.what()
<< std::endl;
return false;
}
}
static bool displayed_help(int argc, const char* argv[])
{
for (int index = 1; index < argc; ++index)
{
const std::string_view arg{argv[index]};
if ((arg == "-h"sv) || (arg == "--help"sv))
{
std::cerr
<< "Usage:\n"
<< "line_sender_c_example: [HOST [PORT]]\n"
<< " HOST: ILP host (defaults to \"localhost\".\n"
<< " PORT: ILP port (defaults to \"9009\"."
<< std::endl;
return true;
}
}
return false;
}
int main(int argc, const char* argv[])
{
if (displayed_help(argc, argv))
return 0;
auto host = "localhost"sv;
if (argc >= 2)
host = std::string_view{argv[1]};
auto port = "9009"sv;
if (argc >= 3)
port = std::string_view{argv[2]};
return !example(host, port);
}