Я использую драйвер Datastax Cassandra для php и хотел бы иметь возможность проверить, не выполнен ли запрос, и ничего не найдено в базе данных. Прямо сейчас, если запрос не удается, логи сообщают следующее
PHP Fatal error: Uncaught exception 'Cassandra\\Exception\\RuntimeException' with message 'No hosts available for the control connection' in /var/www/html/includes/conditions.php:153\nStack trace:\n#0 /var/www/html/includes/conditions.php(153): Cassandra\\DefaultCluster->connect('loop_non_hadoop')\n#1 /var/www/html/index.php(159): check_key('0x851702c16d824...')\n#2 {main}\n thrown in /var/www/html/includes/conditions.php on line 153, referer: http://172.29.195.10:5050/html/index.php
Если запрос выполнен успешно, он возвращает данные, как и ожидалось. Вот функция
function check_key($key){
$cluster = Cassandra::cluster()->build();
//set this to the appropriate keyspace
$keyspace = 'loop_non_hadoop';
$session = $cluster->connect($keyspace);
$query = "SELECT * FROM events WHERE key=$key";
$statement = new Cassandra\SimpleStatement($query);
$results = $session->execute($statement);
if(empty($results)){
return;
}
else {
foreach ($results as $row) {
echo "<tr id='cell'><td>Title</td><td>" . $row['title'] . "</td></tr>";
echo "<tr><td>Location</td><td>" . $row['locationName'] . "</td></tr>";
echo "<tr id='cell'><td>City</td><td>" . $row['city'] . "</td></tr>";
echo "<tr><td>State</td><td>" . $row['state'] . "</td></tr>";
echo "<tr id='cell'><td>Zip</td><td>" . $row['zipcode'] . "</td></tr>";
echo "<tr><td>Address</td><td>" . $row['street'] . "</td></tr>";
echo "<tr id='cell'><td>Description</td><td>" . $row['summary'] . "</td></tr>";
echo "<tr><td>Url</td><td>" . $row['url'] . "</td></tr>";
echo "<tr id='cell'><td>Start</td><td>" . $row['start'] . "</td></tr>";
echo "<tr><td>End</td><td>" . $row['end'] . "</td></tr>";
}
return true;
}
}
Заранее спасибо за помощь
Обновить
Эта предыдущая ошибка, которую я опубликовал, могла быть связана с проблемой, с которой я столкнулся при сбое моей ВМ и cassandra, в любом случае, мне все равно нужно проверить пустые результаты.
Спасибо
Обновить
Следуя предложениям, я сделал следующее
function check_key($key){
try{
$cluster = Cassandra::cluster()->build();
//set this to the appropriate keyspace
$keyspace = 'loop_non_hadoop';
$session = $cluster->connect($keyspace);
$query = "SELECT * FROM events WHERE key=$key";
$statement = new Cassandra\SimpleStatement($query);
$results = $session->execute($statement);
foreach ($results as $row) {
echo "<tr id='cell'><td>Title</td><td>" . $row['title'] . "</td></tr>";
echo "<tr><td>Location</td><td>" . $row['locationName'] . "</td></tr>";
echo "<tr id='cell'><td>City</td><td>" . $row['city'] . "</td></tr>";
echo "<tr><td>State</td><td>" . $row['state'] . "</td></tr>";
echo "<tr id='cell'><td>Zip</td><td>" . $row['zipcode'] . "</td></tr>";
echo "<tr><td>Address</td><td>" . $row['street'] . "</td></tr>";
echo "<tr id='cell'><td>Description</td><td>" . $row['summary'] . "</td></tr>";
echo "<tr><td>Url</td><td>" . $row['url'] . "</td></tr>";
echo "<tr id='cell'><td>Start</td><td>" . $row['start'] . "</td></tr>";
echo "<tr><td>End</td><td>" . $row['end'] . "</td></tr>";
}
return true;
}
catch(Exception $e){
//Casandra error
var_dump($e->getMessage());
}
finally {
return;
}
}
Спасибо за предложения
использование попробуй поймать:
function check_key($key){
try{
$cluster = Cassandra::cluster()->build();
//set this to the appropriate keyspace
$keyspace = 'loop_non_hadoop';
$session = $cluster->connect($keyspace);
$query = "SELECT * FROM events WHERE key=$key";
$statement = new Cassandra\SimpleStatement($query);
$results = $session->execute($statement);
if(empty($results)){
return;
}
else {
foreach ($results as $row) {
echo "<tr id='cell'><td>Title</td><td>" . $row['title'] . "</td></tr>";
echo "<tr><td>Location</td><td>" . $row['locationName'] . "</td></tr>";
echo "<tr id='cell'><td>City</td><td>" . $row['city'] . "</td></tr>";
echo "<tr><td>State</td><td>" . $row['state'] . "</td></tr>";
echo "<tr id='cell'><td>Zip</td><td>" . $row['zipcode'] . "</td></tr>";
echo "<tr><td>Address</td><td>" . $row['street'] . "</td></tr>";
echo "<tr id='cell'><td>Description</td><td>" . $row['summary'] . "</td></tr>";
echo "<tr><td>Url</td><td>" . $row['url'] . "</td></tr>";
echo "<tr id='cell'><td>Start</td><td>" . $row['start'] . "</td></tr>";
echo "<tr><td>End</td><td>" . $row['end'] . "</td></tr>";
}
return true;
}
}catch(\Exception $e){
//Casandra error
var_dump($e->getMessage());
}
}
Других решений пока нет …