简体   繁体   中英

Datastax Cassandra PHP Driver

I am using the Datastax Cassandra driver for php and would like to be able to check if a query fails do to no results being found in the database. Right now if the query fails the logs report the following

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

If the query succeeds it returns the data as expected. Here is the function

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;
    }
}   

Thanks in advance for your help

Update

That previous error I posted may have been related to an issue I had with my VM and cassandra crashing, either way, I still need to check for empty results.

Thanks

Update

Following the suggestions posted I ended up doing the following

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;
    }
}

Thanks for the suggestions

use 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());
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM