0

我是卡桑德拉的新手。我正在使用 Datastax 的 Cassandra PHP 驱动程序。我正在尝试使用多个插入创建批处理语句。给定表格模型:

CREATE TABLE real_time_log (
  du_id int,
  tag_id int,
  status int,
  time_stamp bigint,
  value float,
  PRIMARY KEY ((du_id, tag_id), status, time_stamp)
) 

我试图以两种方式插入以下值:

$batch = new Cassandra\BatchStatement();
$stmt = $this->cassandraDb->prepare('insert into real_time_log'
         . ' (du_id, tag_id, status, time_stamp, value) '
         . 'VALUES (?, ?, ?, ?, ?)');
foreach ($curData as $cData) {
    $values = explode(',', $cData);
    $stmtValues = array(
        'du_id' => 11111,
        'tag_id' => 22222,
        'status' => (int) $values[2],
        'time_stamp' => new Cassandra\Bigint($values[0]),
        'value' => (double) $values[1]
    );
    $batch->add($stmt, $stmtValues);
}
$this->cassandraDb->executeAsync($batch);

这会产生错误:

PHP Fatal error:  Uncaught exception 'Cassandra\Exception\InvalidArgumentException' with message 'Invalid value type'

同时,我尝试了一种更直接的方法,没有准备好的语句:

$batch = new Cassandra\BatchStatement();
foreach ($curData as $cData) {
    $values = explode(',', $cData);
    $stmtValues = array(
        11111,
        22222,
        (int) $values[2],
        new Cassandra\Bigint($values[0]),
        (double) $values[1]
     );
     $batch->add(new Cassandra\SimpleStatement('insert into real_time_log'
                        . ' (du_id, tag_id, status, time_stamp, value) '
                        . 'VALUES (' . implode(',', $stmtValues) . ')'));
 }
 $this->cassandraDb->executeAsync($batch);

这样一切正常,但肯定会慢很多。也许有人可以解释我正在做的准备好的陈述方法有什么问题?

先感谢您。

4

2 回答 2

1

乍一看,它应该可以工作 - 您是否尝试过明确地传递它float(更改(double) $values[1]new Cassandra\Float($values[1]))?

于 2015-10-23T04:03:28.940 回答
0

我已经为插入单行执行了此操作,并且工作正常:

$glid = 12345;
$modid = 'mobile';
$sysdate = date("YmdHis");

$sql = "insert into user (USR_ID,modid,membersince) values (?,?,?)";
$par = array(new Cassandra\Bigint($glid),$modid,new Cassandra\Bigint($sysdate));

$statement = $db->prepare($sql);
$db->execute($statement, array(
                 'arguments' => $par
));
于 2018-01-09T05:01:18.940 回答