2017-04-25 81 views
0

檢索卡桑德拉表複合列我有一個檢索行查詢一個CassandraHandler如何在PHP

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

的這個文檔,但我有點困惑。我只需要獲得json的價值就像我在cqlsh得到

+0

看來'{{「urllarge」:「1.jpg」,「urlmedium」:「2.jpg」}, {「urllarge」:「3.jpg」,「urlmedium」:「4.jpg」 }}'是一個無效的json。你可以請傾倒整行嗎? – ruhul

回答

0

你有兩個將複合材料轉換爲可用的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(...)之前將所有查詢字符串轉換爲Cassandra\Statement對象。