【问题标题】:How to retrieve composite column from Cassandra table in PHP如何在 PHP 中从 Cassandra 表中检索复合列
【发布时间】:2017-09-23 04:09:40
【问题描述】:

我有一个 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 \Cassandra\SimpleStatement($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 值

【问题讨论】:

  • 看来{{"urllarge": "1.jpg", "urlmedium": "2.jpg"}, {"urllarge": "3.jpg", "urlmedium": "4.jpg"}} 是一个无效的json。你能把整行转储吗?

标签: json cassandra cassandra-2.1 datastax-php-driver


【解决方案1】:

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

  1. 创建一个函数以将反序列化/未编组的对象转换为 JSON。
  2. 以 JSON 格式从 Cassandra 检索值。

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

<?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 格式的数据需要更少的应用程序代码,同时还将处理转移到服务器上。这可能是您应用程序需求的首选。

注意:此示例使用 v1.3.0 的 DataStax PHP 驱动程序,该驱动程序添加了将查询字符串直接传递给 Session::execute()Session::executeAsync() 的支持。如果您使用的是早期版本,则需要将所有查询字符串转换为 Cassandra\Statement 对象,然后再传递给 $session-&gt;execute(...)

【讨论】:

    猜你喜欢
    • 2014-03-01
    • 2017-05-28
    • 1970-01-01
    • 2016-09-16
    • 2011-02-24
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 2017-07-17
    相关资源
    最近更新 更多