How to get metrics within the result of N1QL query through PHP SDK


#1

I have this example where the results contains only the rows, how to find metrics where I need to get rows count of all results

$myBucket = $myCluster->openBucket();
$myBucket->enableN1ql(array('http://1.1.1.1:8093/','http://1.1.1.2:8093/'));
$query = CouchbaseN1qlQuery::fromString('SELECT * FROM default limit 10');
$res = $myBucket->query($query);
var_dump($res);

anybody can help please ?!


#2

@bashar, this functionality is not exposed yet, but if you like, you can use the following approach now (note that it is using internal interfaces, which are private and might change without notice)

$r = new ReflectionClass('CouchbaseBucket');
$private_interface = $r->getProperty('me');
$private_interface->setAccessible(true);

$h = new CouchbaseCluster('couchbase://localhost');
$b = $h->openBucket('travel-sample');

$query = json_encode(CouchbaseN1qlQuery::fromString('SELECT * FROM `travel-sample` limit 10')->toObject(), true);
$res = $private_interface->getValue($b)->n1ql_request($query, true);
$meta = json_decode($res['meta'], true);
$rows = array();
foreach ($res['results'] as $row) {
    $rows[] = json_decode($row, true);
}
var_dump($meta);
var_dump(count($rows));