c++ zookeeper code example
Example: zookeeper c++ example
#include <zk/client.hpp>
#include <zk/multi.hpp>
#include <zk/server/configuration.hpp>
#include <zk/server/server.hpp>
#include <exception>
#include <iostream>
template <typename T>
void print_thing(const zk::future<T>& result)
{
try
{
T value(result.get());
std::cerr << value << std::endl;
}
catch (const std::exception& ex)
{
std::cerr << "Exception: " << ex.what() << std::endl;
}
}
int main()
{
zk::server::server server(zk::server::configuration::make_minimal("zk-data", 2181));
auto client = zk::client::connect("zk://127.0.0.1:2181")
.get();
client.get("/foo/bar")
.then(print_thing<zk::get_result>);
client.get_children("/foo")
.then(print_thing<zk::get_children_result>);
client.set("/foo/bar", "some data")
.then(print_thing<zk::set_result>);
client.create("/foo/baz", "more data")
.then(print_thing<zk::create_result>);
client.get("/foo/bar")
.then([client] (const auto& get_res)
{
zk::version foo_bar_version = get_res.get().stat().data_version;
zk::multi_op txn =
{
zk::op::check("/foo", zk::version::any()),
zk::op::check("/foo/baz", foo_bar_version),
zk::op::create("/foo/bap", "hi", nullopt, zk::create_mode::sequential),
zk::op::erase("/foo/bzr"),
};
client.commit(txn).then(print_thing<zk::multi_result>);
});
client.close();
}