如何从PHP中的Cassandra表中检索复合柱



我有一个cassandrahandler,在行中检索查询

class CassandraHandler
{
    private $keyspace = 'blabla'; //default is oyvent
    private $cluster = NULL;
    private $session = NULL;
    function __construct(){
        $this->cluster   =   Cassandra::cluster()
            ->build();       // connects to localhost by default
        $this->session   = $this->cluster->connect($this->keyspace);
    }
    /**
     * @return  Rows
     */
    public function execute($query){
        $statement = new CassandraSimpleStatement($query);
        $result    = $this->session->execute($statement);  
        return $result;
    }
}

当我用于普通列时,这很好,但是我无法在php

中获得我的照片列

我像这样创建了列

photos frozen<set<map<text,text>>>

我的JSON示例

{{"urllarge": "1.jpg", "urlmedium": "2.jpg"},
 {"urllarge": "3.jpg", "urlmedium": "4.jpg"}}

在这里如何使用PHP检索复合列?

$cassandraHandler = new CassandraHandlerClass(); 
 $rows = $cassandraHandler->fetchLatestPosts($placeids, $limit);
      foreach ($rows as $row) {
          $tmp = array();
          $tmp["userid"] = doubleval($row["userid"]);
          $tmp["fullname"] = $row["fullname"];
          $tmp["photos"] = $row["photos"]  //????????
       }

我知道PHP驱动程序的文档https://github.com/datastax/php-driver

但是我有点困惑..我只需要像在CQLSH

中获得JSON值

您有两个选项将复合材料转换为可用的JSON:

  1. 创建一个函数,将避难/未划分的对象转换为JSON。
  2. 从Cassandra中检索json的值。

这是一个演示这两个选项的示例:

<?php
$KEYSPACE_NAME = "stackoverflow";
$TABLE_NAME = "retrieve_composites";
function print_rows_as_json($rows) {
    foreach ($rows as $row) {
        $set_count = 0;
        echo "{"photos": [";
        foreach ($photos = $row["photos"] as $photo) {
            $map_count = 0;
            echo "{";
            foreach ($photo as $key => $value) {
                echo ""{$key}": "{$value}"";
                if (++$map_count < count($photo)) {
                    echo ", ";
                }
            }
            echo "}";
            if (++$set_count < count($photos)) {
                echo ", ";
            }
        }
        echo "]}" . PHP_EOL;
    }
}
// Override default localhost contact point
$contact_points = "127.0.0.1";
if (php_sapi_name() == "cli") {
    if (count($_SERVER['argv']) > 1) {
        $contact_points = $_SERVER['argv'][1];
    }
}
// Connect to the cluster
$cluster = Cassandra::cluster()
    ->withContactPoints($contact_points)
    ->build();
$session = $cluster->connect();
// Create the keypspace (drop if exists) and table
$session->execute("DROP KEYSPACE IF EXISTS {$KEYSPACE_NAME}");
$session->execute("CREATE KEYSPACE {$KEYSPACE_NAME} WITH replication = "
    . "{ 'class': 'SimpleStrategy', 'replication_factor': 1 }"
);
$session->execute("CREATE TABLE ${KEYSPACE_NAME}.{$TABLE_NAME} ( "
    . "id int PRIMARY KEY, "
    . "photos frozen<set<map<text, text>>> )"
);
// Create a multiple rows to retrieve
$session->execute("INSERT INTO ${KEYSPACE_NAME}.{$TABLE_NAME} (id, photos) VALUES ( "
    . "1, "
    . "{{'urllage': '1.jpg', 'urlmedium': '2.jpg'}, "
    . "{'urllage': '3.jpg', 'urlmedium': '4.jpg'}}"
    . ")");
$session->execute("INSERT INTO ${KEYSPACE_NAME}.{$TABLE_NAME} (id, photos) VALUES ( "
    . "2, "
    . "{{'urllage': '21.jpg', 'urlmedium': '22.jpg'}, "
    . "{'urllage': '23.jpg', 'urlmedium': '24.jpg'}}"
    . ")");
// Select and print the unmarshalled data as JSON
$rows = $session->execute("SELECT photos FROM ${KEYSPACE_NAME}.{$TABLE_NAME}");
print_rows_as_json($rows);
// Select the data as JSON and print the string
$rows = $session->execute("SELECT JSON photos FROM ${KEYSPACE_NAME}.{$TABLE_NAME}");
foreach ($rows as $row) {
    echo $row["[json]"] . PHP_EOL;
}

从上面的示例中,您可以看到选择数据作为JSON涉及的应用程序更少的代码,同时还将处理过程移至服务器上。这可能是您应用程序需求的首选选择。

注意:此示例使用DataStax PHP驱动程序的V1.3.0,该驱动程序添加了支持以直接传递查询字符串直接传递到Session::execute()Session::executeAsync()。如果您使用的是较早版本,则需要在传递到$session->execute(...)之前将所有查询字符串转换为CassandraStatement对象。

最新更新