【问题标题】:How to write binary data to Cassandra blob - PHP如何将二进制数据写入 Cassandra blob - PHP
【发布时间】:2016-04-14 14:40:13
【问题描述】:

我正在尝试使用 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'

【问题讨论】:

    标签: php cassandra blob


    【解决方案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);
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        附言为什么使用:pack("H*",$data[0]['content']); 而是更简单的 hex2bin($data[0]['content']) ?

        【讨论】:

          【解决方案4】:

          您可以使用 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)
              )));
          

          【讨论】:

            猜你喜欢
            • 2018-11-12
            • 1970-01-01
            • 2015-06-09
            • 2010-11-29
            • 2020-10-23
            • 2012-04-25
            • 1970-01-01
            • 2017-06-03
            • 2021-05-04
            相关资源
            最近更新 更多