php 中的 datastax cassandra 查询结果解码

datastax cassandra query result decoding in php

我正在使用 Cassandra 并使用 Datastax PHP CQL。

给定以下查询,我应该如何解码结果?

$cql = SELECT * FROM  company WHERE userid = 1001 ORDER BY gpsTime DESC LIMIT 1;                           
$statement = new Cassandra\SimpleStatement($cql);
$result = $this->session->execute($statement);

我尝试解码:

foreach($result as $row){
    // Get values from row and add to array
    array_push($allVehicleInfo,array(
        'userID'    => $row['userid']->value,
        'gpsTime'   => $row['gpstime']->seconds,
         'latitude' => $row['latitude']->value )
    );
}

但是,我得到一个错误:

Message: Undefined property: Cassandra\Decimal::$value


我的 table 架构是:

$cql = "CREATE TABLE " company " (
    userID bigint,
    userName ascii,
    latitude decimal,
    longitude decimal,
    accuracy float,
    altitude float,
    gpsTime timestamp );

value 是一个函数而不是 属性,所以你必须做 $row['latitude']->value()

正如 Alex 所说,在您的示例中 value 不是 属性。相反,请查看您尝试解码的 class 的文档。 The documentation can be found here.

如果您不确定要处理哪个 class,可以使用内置函数 get_class() - see get_class() manual.

一旦您知道 class 您正在处理什么,您就可以查看 DataStax PHP 驱动程序文档,了解您需要调用哪个函数来检索正确的值。

例如,如果您正在处理 Cassandra\Timestamp class,则必须调用 time() 函数来检索时间作为 intSee Timestamp documentation.

在您的情况下,您可能正在处理 Float,因此检查 the Float documentation 并查看您需要调用函数 value() 来检索 float Cassandra\Float 实例的值。


代码解:

foreach($result as $row){
    // Get values from row and add to array
    array_push($allVehicleInfo,array(
        'userID'    => $row['userid']->value(),
        'gpsTime'   => $row['gpstime']->seconds,
         'latitude' => $row['latitude']->value() )
    );
}