Datastax Cassandra PHP 驱动程序
Posted
技术标签:
【中文标题】Datastax Cassandra PHP 驱动程序【英文标题】:Datastax Cassandra PHP Driver 【发布时间】:2016-02-27 05:16:51 【问题描述】:我正在使用适用于 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;
感谢您的建议
【问题讨论】:
【参考方案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());
【讨论】:
我会试试的,谢谢。对 PHP 相当陌生以上是关于Datastax Cassandra PHP 驱动程序的主要内容,如果未能解决你的问题,请参考以下文章
cassandra 数据库 - 获取时间高于 Netflix Astyanax 驱动程序而不是 datastax 驱动程序
如何使用 datastax 驱动程序创建 Cassandra 连接池
Datastax Cassandra Java 驱动程序 @QueryParameters fetchSize