安装MongoDB驱动支持perl
vim packages.txt
Data::Types
DateTime
DateTime::Tiny
ExtUtils::MakeMaker
File::Slurp
File::Temp
FileHandle
JSON
Test::Exception
Test::Warn
Tie::IxHash
Try::Tiny
boolean
ExtUtils::MakeMaker
Class::Method::Modifiers
DateTime
Digest::MD5
Moose
Tie::IxHash
XSLoader
MongoDB
然后安装cpanm,可以解决依赖。
cat packages.txt | cpanm
perl操作MongoDB
其他
存储json数组数据
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use diagnostics;
use JSON;
use Encode;
#use URI::Escape;
use Data::Dumper;
use MongoDB ();
require("getcfg.pl");
my($cfgPath,$funcFile) = @ARGV;
my $json = new JSON;
my $host;
my $database;
# 读取配置
sub loadCfg{
$host = &getcfg($cfgPath, "MongoDB", "host");
$database = &getcfg($cfgPath, "MongoDB", "database");
}
sub cache{
my $client = MongoDB::MongoClient->new(host => $host, port => 27017);
my $db = $client->get_database($database);
my $collection = $db->get_collection($funcFile);
open(OUT, $funcFile) or die $!;
while(<OUT>){
my $obj = $json->decode($_);
#say "json数据为:".Dumper($obj);
for my $item(@{$obj->{'results'}}){
say "开始缓存json数据"."$item";
#utf8::encode($item);
$collection->insert_one($item);
}
}
close OUT;
}
&loadCfg;
&cache;
#!/usr/bin/perl
use strict;
use warnings;
use MongoDB ();
use Data::Dumper qw(Dumper);
my $client = MongoDB::MongoClient->new(host => 'mongodb://172.19.238.62:27017,172.19.238.63:27017,172.19.238.64:27017', replica_set_name => 'replication',);
my $db = $client->get_database( 'example_' . $$ . '_' . time );
my $people_coll = $db->get_collection('people');
$people_coll->insert_one( {
name => 'First',
});
$people_coll->insert_one( {
name => 'Second',
});
my $people = $people_coll->find;
while (my $p = $people->next) {
print Dumper $p;
}
$db->drop;