Couchbase C++ SDK 1.4.0 (rev. 59aa900)
Loading...
Searching...
No Matches
analytics_row Class Reference

Represents a single row of a streaming analytics result. More...

#include <couchbase/analytics_row.hxx>

Public Member Functions

 analytics_row ()=default
 analytics_row (codec::binary content)
 Constructs a row from raw JSON bytes.
template<typename Serializer = codec::tao_json_serializer, typename Document = typename Serializer::document_type, std::enable_if_t< codec::is_serializer_v< Serializer >, bool > = true>
auto content_as () const -> Document
 Decodes the row content into the requested document type using the given serializer.
auto content_as_binary () const -> const codec::binary &
 Returns the raw binary content of this row.

Detailed Description

Represents a single row of a streaming analytics result.

The raw JSON bytes can be decoded via content_as().

Since
1.4.0
Volatile
Should not be used in production

Constructor & Destructor Documentation

◆ analytics_row() [1/2]

analytics_row ( )
default
Since
1.4.0
Volatile
Should not be used in production

◆ analytics_row() [2/2]

analytics_row ( codec::binary content)
inlineexplicit

Constructs a row from raw JSON bytes.

Parameters
contentraw JSON bytes for this row
Since
1.4.0
Volatile
Should not be used in production

Member Function Documentation

◆ content_as()

template<typename Serializer = codec::tao_json_serializer, typename Document = typename Serializer::document_type, std::enable_if_t< codec::is_serializer_v< Serializer >, bool > = true>
auto content_as ( ) const -> Document
inlinenodiscard

Decodes the row content into the requested document type using the given serializer.

Only call this on a row obtained from a successful next() (falsy error, engaged optional). The end-of-stream and error sentinels carry an empty analytics_row, and decoding empty/malformed content throws the serializer's parse exception — gate on the three-state next() contract (check the error, then the optional) before decoding.

#include <spdlog/fmt/bundled/format.h>
#include <tao/json.hpp>
// After the fmt header: these specialize fmt::formatter, so fmt has to be declared first.
#include <cstdint>
#include <string>
int
main(int argc, const char* argv[])
{
if (argc != 4) {
fmt::print("USAGE: ./analytics_stream couchbase://127.0.0.1 Administrator password\n");
return 1;
}
const std::string connection_string{ argv[1] };
const std::string username{ argv[2] };
const std::string password{ argv[3] };
auto [connect_err, cluster] =
couchbase::cluster::connect(connection_string, couchbase::cluster_options(username, password))
.get();
if (connect_err) {
fmt::print("unable to connect to the cluster: {}\n", connect_err);
return 1;
}
// Analytics streaming mirrors the query API: the same three-state next(), the same iterator,
// the same end-of-stream metadata.
auto [err, result] =
cluster.analytics_query_stream("SELECT i AS n FROM array_range(0, 2000) AS i ORDER BY i").get();
if (err) {
fmt::print("unable to start the streaming analytics query: {}\n", err);
return 1;
}
// The row signature comes from the response preamble, so it is available before the stream has
// been drained (unlike the metadata).
if (const auto signature = result.signature(); signature) {
// codec::binary is a byte vector; reinterpret to chars to print it as the JSON text it is.
fmt::println(
"signature: {}",
std::string{ reinterpret_cast<const char*>(signature->data()), signature->size() });
}
std::uint64_t sum{ 0 };
while (true) {
auto [row_err, row] = result.next().get();
if (row_err) {
fmt::print("streaming analytics query failed mid-stream: {}\n", row_err);
return 1;
}
if (!row) {
break; // clean end of stream
}
const auto value = row->content_as<couchbase::codec::tao_json_serializer, tao::json::value>();
sum += value.at("n").as<std::uint64_t>();
}
fmt::println("sum of the streamed rows: {}", sum);
auto [meta_err, meta] = result.meta_data().get();
if (meta_err) {
fmt::print("unable to retrieve the analytics metadata: {}\n", meta_err);
return 1;
}
fmt::println("status={}, rows={}", meta.status(), meta.metrics().result_count());
cluster.close().get();
return 0;
}
/*
$ ./analytics_stream couchbase://127.0.0.1 Administrator password
signature: {"*":"*"}
sum of the streamed rows: 1999000
status=success, rows=2000
*/
Template Parameters
Serializerthe serializer to use (defaults to tao_json_serializer)
Documentthe document type to decode into (defaults to the serializer's document_type)
Returns
the decoded document
Exceptions
theserializer's deserialization exception if the content is empty or not valid JSON
Since
1.4.0
Volatile
Should not be used in production

◆ content_as_binary()

auto content_as_binary ( ) const -> const codec::binary &
inlinenodiscard

Returns the raw binary content of this row.

Returns
raw JSON bytes
Since
1.4.0
Volatile
Should not be used in production

The documentation for this class was generated from the following file: