如何将二进制数据写入Cassandra blob - PHP

时间:2014-07-10 07:55:19

标签: php cassandra blob

我想使用YACassandraPDO保存Cassandra(CQL3)中的所有文件

enter code here

        $tmpName = $_FILES['content']['tmp_name'];
        $fp = fopen($tmpName, 'rb');
        $content = fread($fp, filesize($tmpName));

        $stmt = $cass_db->prepare ("INSERT INTO $table ( id, ctype, fname, content ) VALUES ( now(), :ctype, :fname, textAsBlob('$content')) ;");
        $stmt->bindValue (':ctype', $_FILES['content']['type']);
        $stmt->bindValue (':fname', $_FILES['content']['name']);
        $stmt->execute ();

如果它是普通/文本一切都好 但我无法保存任何二进制文件,我尽量不使用textAsBlob, - 无法保存任何类型的文件 结果是 PHP致命错误:未捕获异常'PDOException',消息'CQLSTATE [HY000] [2]输入长度= 1'

4 个答案:

答案 0 :(得分:1)

我的决定是:

写作

    $content = file_get_contents($_FILES['content']['tmp_name']);
    $content = bin2hex($content);
    $stmt = $cass_db->prepare ("INSERT INTO $table ( id, ctype, fname, content ) VALUES ( now  (), :ctype, :fname, asciiAsBlob('$content') );");
    $stmt->bindValue (':ctype', $_FILES['content']['type']);
    $stmt->bindValue (':fname', $_FILES['content']['name']);
    $stmt->execute ();

阅读

    $stmt = $cass_db->prepare ("SELECT blobAsascii(content) as content, ctype, fname FROM $table WHERE id = $id;");
    $stmt->execute ();
    $data = $stmt->fetchAll();
    $fp = fopen('/tmp/'.$data[0]['fname'], 'w');
    $content = pack("H*",$data[0]['content']);
    fwrite($fp, $content);
    fclose($fp);

答案 1 :(得分:0)

据我所知,YACassandraPDO不稳定。 请检查此PHP库http://evseevnn.github.io/php-cassandra-binary/

它是由我编写的,如何使用它与PDO包装器类似。也许它适合你。

答案 2 :(得分:0)

P.S。为什么你使用:pack(“H *”,$ data [0] ['content']); 而更简单的hex2bin($ data [0] ['content'])?

答案 3 :(得分:0)

您可以使用Blob类

将blob内容写入cassandra
$fileContent = file_get_contents($_FILES['content']['tmp_name']);
$blob = new \Cassandra\Blob($fileContent);
$content = $blob->bytes();
$stmt =  new Cassandra\SimpleStatement("INSERT INTO $table ( id, ctype, fname, content ) VALUES ( ?,?,?,?");
    $result = $session->execute($statement, new Cassandra\ExecutionOptions(array(
        'arguments' => array($id, $ctype, $fname,$content)
    )));