加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl和MongoDB二进制数据

发布时间:2020-12-15 21:50:33 所属栏目:大数据 来源:网络整理
导读:从 MongoDB手册: By default,all database strings are UTF8. To save images,binaries, and other non-UTF8 data,you can pass the string as a reference to the database. 我正在抓取页面并希望存储内容以供以后处理. 我不能依赖meta-charset,因为很多页
从 MongoDB手册:

By default,all database strings are UTF8. To save images,binaries,
and other non-UTF8 data,you can pass the string as a reference to the
database.

我正在抓取页面并希望存储内容以供以后处理.

>我不能依赖meta-charset,因为很多页面都有utf8内容但错误地声明了iso-8859-1或类似内容
>所以不能使用Encode(不知道原始字符集)
>因此,我希望将内容简单地存储为字节流(二进制数据)以供以后处理

我的代码片段:

sub save {
    my ($self,$ok,$url,$fetchtime,$request ) = @_;

    my $rawhead = $request->headers_as_string;
    my $rawbody = $request->content;

    $self->db->content->insert(
        { "url" => $url,"rhead" => $rawhead,"rbody" => $rawbody } ) #using references here
      if $ok;

    $self->db->links->update(
        { "url" => $url },{
            '$set' => {
                'status'       => $request->code,'valid'        => $ok,'last_checked' => time(),'fetchtime'    => $fetchtime,}
        }
    );
}

但得到错误:

Wide character in subroutine entry at
/opt/local/lib/perl5/site_perl/5.14.2/darwin-multi-2level/MongoDB/Collection.pm
line 296.

这是我存储数据的唯一地方.

问题:在MondoDB中存储二进制数据的唯一方法是对它们进行编码,例如:用base64?

解决方法

它看起来像是另一个关于_utf8_ flag的悲伤故事……

我可能错了,但似乎HTTP :: Message的headers_as_string和content方法将它们的字符串作为字符序列返回.但MongoDB驱动程序期望显式传递给它的字符串作为’二进制文件’成为八位字节序列 – 因此警告剧.

一个相当丑陋的解决方法是在你的代码中删除$rawhead和$rawbody上的utf8标志(我不知道它应该由MongoDB驱动程序本身完成吗?),通过这样的东西……

_utf8_off $rawhead; 
_utf8_off $rawbody; # ugh

另一种方法是使用encode(‘utf8’,$rawhead) – 但是当你从DB中提取值时你应该使用decode,我怀疑它不是更丑陋.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读