我正在使用用于php的Datastax Cassandra驱动程序,并且希望能够检查查询是否失败以及数据库中是否找不到结果。现在,如果查询失败,则日志会报告以下内容
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;
}
}
提前感谢您的帮助
更新
我发布的上一个错误可能与我的VM和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;
}
}
感谢您的建议
答案 0 :(得分:1)
使用try catch:
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());
}
}