【问题标题】:Cassandra is not retrieving the correct integer valueCassandra 没有检索到正确的整数值
【发布时间】:2013-05-16 02:28:43
【问题描述】:

我使用的是 cql 3.0.0

我已经执行了查询:

INSERT INTO emp (empID, deptID, first_name, last_name)
VALUES (104, 15, 'jane', 'smith')

在检索此记录时,我得到以下值:

empid = h
deptid =  (blank value)
first_name = 'jane'
last_name = 'smith'

在搜索它时,我发现 h 相当于 utf-8 字符 104。utf-8 中的 15 也是空白的。 (参考链接:http://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=dec&unicodeinhtml=dec

我在创建表期间将列类型设置为 int,但在检索时我没有得到 int 值。

如何获取要检索的正确值。我不想要 utf-8 值。

谢谢

我正在使用 cassandra 1.2.4

下面是我用phpcassa写的代码:

require_once(__DIR__.'/../lib/autoload.php');

use phpcassa\Connection\ConnectionPool;
use phpcassa\ColumnFamily;
use phpcassa\SystemManager;
use phpcassa\Schema\StrategyClass;
use cassandra\Compression;
use cassandra\ConsistencyLevel;


$pool = new ConnectionPool("prod",array('X.X.X.X','X.X.X.X'));

$raw = $pool->get();
$rows = $raw->client->set_cql_version("3.0.0");


$rows = $raw->client->execute_cql3_query('USE prod;', Compression::NONE,    ConsistencyLevel::ONE );
$rows = $raw->client->execute_cql3_query('CREATE TABLE emp (
  empID int,
  deptID int,
  first_name varchar,
  last_name varchar,
  PRIMARY KEY (empID, deptID));
  ', Compression::NONE, ConsistencyLevel::ONE );  


$rows = $raw->client->execute_cql3_query('INSERT INTO emp (empID, deptID, first_name,     last_name)
  VALUES (104, 15, \'jane\', \'smith\');
  ', Compression::NONE, ConsistencyLevel::ONE );  


$rows = $raw->client->execute_cql3_query('SELECT * FROM emp WHERE empID IN (2,104)     ORDER BY deptID ASC;', Compression::NONE, ConsistencyLevel::ONE );  

echo "<pre>";
print_r($rows);
echo "<pre>";

生成的输出是:

cassandra\CqlRow Object
               (
                [key] => 
                [columns] => Array
                    (
                        [0] => cassandra\Column Object
                            (
                                [name] => empid
                                [value] => h
                                [timestamp] => 
                                [ttl] => 
                            )

                        [1] => cassandra\Column Object
                            (
                                [name] => deptid
                                [value] => 
                                [timestamp] => 
                                [ttl] => 
                            )

                        [2] => cassandra\Column Object
                            (
                                [name] => first_name
                                [value] => jane
                                [timestamp] => 
                                [ttl] => 
                            )

                        [3] => cassandra\Column Object
                            (
                                [name] => last_name
                                [value] => smith
                                [timestamp] => 
                                [ttl] => 
                            )

                    )

            )

【问题讨论】:

  • 您能分享您的 cassandra 版本和架构吗?
  • 我使用的是 cassandra 1.2.4

标签: cassandra phpcassa


【解决方案1】:

虽然答案有点晚,但我在为 Yii 框架编写自己的 cassandra-wrapper 时拼命寻找解决方案。我想出了类似以下类的东西,它消除了 cassandra 中不同数据类型(包括整数类型)的不正确二进制问题:

Yii::import('ext.phpcassa.autoload');
require_once('protected/extensions/phpcassa/autoload.php');

use phpcassa\Connection\ConnectionPool;
use phpcassa\ColumnFamily;
use phpcassa\ColumnSlice;
use phpcassa\SystemManager;
use phpcassa\Schema\StrategyClass;
use phpcassa\Schema\DataType;

class ACassandraConnection extends CApplicationComponent {

    protected $_pool = null;
    public $keyspace = null;
    public $servers = null; 

    /**
     * Establish connection to cassandra cluster
     * @return object 
     */
    private function _get_raw() {
        if ($this->_pool === null) {
            $this->_pool = new ConnectionPool($this->keyspace, $this->servers);
        }
        return $this->_pool->get();
    }


    public function cql_query($query) {
        $raw = $this->_get_raw();
        $cql_result = $raw->client->execute_cql3_query($query, cassandra\Compression::NONE, cassandra\ConsistencyLevel::ONE);
        $this->_pool->return_connection($raw); 
        return $cql_result;
    }

    public function cql_get_rows($cql_result) {
        if ($cql_result->type == 1) { 
            $rows = array(); 
            foreach ($cql_result->rows as $rowIndex => $cqlRow) { 
                $cols = array(); 
                foreach ($cqlRow->columns as $colIndex => $column) {
                    $type = DataType::get_type_for($cql_result->schema->value_types[$column->name]);
                    $cols[$column->name] = $type->unpack($column->value);
                }
                $rows[] = $cols;
            }
            return $rows; 
        } else {
            return null;
        }
    }


    /**
     * Perform garbage collection 
     */
    public function __destruct() {
        if($this->_pool !== null) {
            $this->_pool->close();
        }
    }

}

【讨论】:

  • 该解决方案适用于整数类型。但是,如果列类型是计数器,那么它不会将整数视为字节。它不适用于计数器列类型。
【解决方案2】:

我会说请查看您的解决方案,我已经尝试了与您描述的问题完全相同的方法,但它对我来说工作正常。可能在创建时您已使用 utf-8 作为其类型。

架构

CREATE COLUMNFAMILY test(empID int, deptID int, first_name text, last_name text, PRIMARY KEY(empID));

插入

INSERT INTO emp (empID, deptID, first_name, last_name) VALUES (104, 15, 'jane', 'smith');

检索

SELECT * FROM test ;

empid  | deptid | first_name | last_name
-------+--------+------------+-----------
104    |     15 |       jane |     smith

【讨论】:

  • 我正在使用下面的创建表语法。我没有使用 create columnfamily: ` CREATE TABLE emp (empID int, deptID int, first_name varchar, last_name varchar, PRIMARY KEY (empID, deptID));`
  • 不,我正在使用 phpcassa 库。我将更新我上面使用的代码。
  • 是的,我可以在 cqlsh 中查看。但它没有显示在使用 phpcassa 库的 php 中。
  • 那样的话,可能是@Tyler Hobbs 能给你一个完美的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 2021-10-31
相关资源
最近更新 更多