參考mongodb
vim packages.txtjson
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,能夠解決依賴。vim
cat packages.txt | cpanm
參考數組
存儲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;