datastax php 驱动程序的 Cassandra 准备语句错误

Cassandra Prepared Statements error with datastax php driver

我在使用 DataStax php 驱动程序 1.0.0-rc 和 Cassandra 2.2.3 准备语句时遇到奇怪的错误。我在这条线上遇到了异常:

$statement = $this->session->prepare("SELECT ? FROM ? WHERE ? = ?");

我看到这个错误:

"error_code":33562624
"error_message":"Bind variables cannot be used for keyspace names"

下面是 class 用于与 Cassandra 通信的存根:

class ClsCassandra extends ClsDbObject
{
    private $hostname="";
    private $username="";
    private $password="";
    private $keyspace="";
    private $poi_table="";
    private $poi_table_key_field="";
    private $poi_table_content_field="";

    private $cluster = NULL;
    private $session = NULL;

    private $threads = 1;

    function __construct()
    {
        ...
        ...
        //
        // i set up all the properties above
        //
        ...
        ...
    }

    public function runQuery(&$error)
    {
        try 
        {           
            $this->cluster   = Cassandra::cluster()
            ->withContactPoints($this->hostname)
            ->withCredentials($this->username, $this->password)
            ->withIOThreads($this->threads)
            ->build();

            $this->session = $this->cluster->connect($this->keyspace);

            // error on next line...
            $statement = $this->session->prepare("SELECT ? FROM ? WHERE ? = ?");
            $results = $this->session->execute($statement, new Cassandra\ExecutionOptions(array(
                    'arguments' => array($this->poi_table_content_field, $this->poi_table, $this->poi_table_key_field, $keypattern)
            )));

        }
        catch(Cassandra\Exception $ce)
        {
            $error->setError($ce->getCode(), $ce->getMessage(), $ce->getTraceAsString());
            $this->log(LOG_LEVEL, $error->getErrorMessage(), __FILE__, __LINE__, __CLASS__);
            return false;
        }
        return true;
    }

    ...
    ...
    ...
}

如果我将 Simple Statemetn 与标准 select 查询一起使用,它会起作用。

有什么建议吗?

您看到此错误是因为您只能将变量绑定到 WHERE 子句。准备好的语句机制不仅仅是一个美化的字符串格式化程序。它有关于什么可以绑定和不能绑定的规则,以及在发送到 Cassandra 时必须如何绑定以消除歧义。

您需要尝试这样的操作:

$statement = $this->session->prepare(
    "SELECT key1, key2, col1, col2 FROM yourKeyspaceName.yourTableName WHERE key1 = ? AND key2 = ?");
$results = $this->session->execute($statement, new Cassandra\ExecutionOptions(array(
    'arguments' => array($key1,$key2)
    )));