在创建持久存储和模型并将 RDF/XML 文件解析到其中时,Perl 的 Redland 库和 RDF::Redland 绑定有很好的文档,例如这里和这里。
另一方面,我找不到任何关于如何从我的持久存储中检索模型的示例,并且明显的方法不起作用。
这就是我创建商店的方式:
my $storage =
new RDF::Redland::Storage( "hashes", "test",
"new='yes',hash-type='bdb',dir='data'" )
or die "Failed to create RDF::Redland::Storage\n";
my $model = new RDF::Redland::Model( $storage, "" )
or die "Failed to create RDF::Redland::Model for storage\n";
my $uri = new RDF::Redland::URI("file:test.rdf");
my $parser = new RDF::Redland::Parser( "rdfxml", "application/rdf+xml" )
or die "Failed to find parser\n";
my $stream = $parser->parse_as_stream( $uri, $uri );
my $count = 0;
while ( !$stream->end ) {
$model->add_statement( $stream->current );
$count++;
$stream->next;
}
$stream = undef;
$storage = undef;
$model = undef;
print "Done. There were $count statements.\n";
这有效并告诉我我有 300k 的声明。
然后我尝试用另一个脚本检索这些数据:
my $storage =
new RDF::Redland::Storage( "hashes", "test",
"hash-type='bdb',dir='data'" )
or die "Failed to create RDF::Redland::Storage\n";
my $model = new RDF::Redland::Model( $storage, "" )
or die "Failed to create RDF::Redland::Model for storage\n";
$model->sync; # tried with and without this line
my $stream = $model->as_stream;
while ( !$stream->end ) {
print "Statement: ", $stream->current->as_string, "\n";
$stream->next;
}
这绝对不会打印任何内容。
谢谢你的帮助!