Search code examples
phpcassandraprepared-statementcqldatastax-php-driver

CQL PHP driver: invalid value type on prepared statement with good values


I am new to Cassandra. I am using Cassandra PHP driver from Datastax. I am trying to create batch statement with multiple inserts. Given the table model:

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)
) 

I am trying to insert following values in two ways:

$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);

This produces error:

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

Meanwhile I tried a more straight-forward approach without prepared statements:

$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);

This way everything works, but it is sure to be a lot slower. Maybe someone can explain what is wrong with the prepared statement approach I am doing?

Thank you in advance.


Solution

  • At first glance, it looks like it should work - have you tried explicitly passing it a float ( change (double) $values[1] to new Cassandra\Float($values[1]) )?