我是卡桑德拉的新手。我正在使用 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);
这样一切正常,但肯定会慢很多。也许有人可以解释我正在做的准备好的陈述方法有什么问题?
先感谢您。