Download the PHP package sweetrdf/quick-rdf-io without Composer
On this page you can find all versions of the php package sweetrdf/quick-rdf-io. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sweetrdf/quick-rdf-io
More information about sweetrdf/quick-rdf-io
Files in sweetrdf/quick-rdf-io
Package quick-rdf-io
Short Description Collection of parser and serializers compatible with sweetrdf/rdfInterface
License MIT
Homepage https://github.com/sweetrdf/quickRdfIo
Informations about the package quick-rdf-io
quickRdfIo
Collection of RDF parsers and serializers implementing the https://github.com/sweetrdf/rdfInterface interface.
Originally developed for the quickRdf library.
Supported formats
format | read/write | class | implementation | streaming[1] |
---|---|---|---|---|
rdf-xml | rw | RdfXmlParser, RdfXmlSerializer | own | yes |
n-triples | rw | NQuadsParser, NQuadsSerializer | own | yes |
n-triples* | rw | NQuadsParser, NQuadsSerializer | own | yes |
n-quads | rw | NQuadsParser, NQuadsSerializer | own | yes |
n-quads* | rw | NQuadsParser, NQuadsSerializer | own | yes |
turtle | rw | TriGParser, TrigSerializer | pietercolpaert/hardf | yes |
trig | rw | TriGParser, TrigSerializer | pietercolpaert/hardf | yes |
JsonLD | rw | JsonLdParser, JsonLdSerializer | ml/json-ld | no |
JsonLD[2] | w | JsonLdStreamSerializer | own[3] | yes |
[1] A streaming parser/serializer doesn't materialize the whole dataset in memory which assures constant (and low) memory footprint.
(this feature applies only to the parser/serializer - see the section on memory usage below)
[2] Use the jsonld-stream
value for the $format
parameter of the \quickRdfIo\Util::serialize()
to use this serializer.
[3] Outputs data only in the extremely flattened Json-LD but works in a streaming mode.
Installation
- Obtain the Composer
- Run
composer require sweetrdf/quick-rdf-io
Automatically generated documentation
https://sweetrdf.github.io/quickRdfIo/namespaces/quickrdfio.html
It's very incomplete but better than nothing.\ RdfInterface and ml/json-ld documentation is included.
Usage
Remark - there are calls to two other libraries in examples
sweetrdf/quick-rdf and sweetrdf/term-templates.
You may install them with composer require sweetrdf/quick-rdf
and composer require sweetrdf/term-templates
.
Basic parsing
Just use \quickRdfIo\Util::parse($input, $dataFactory, $format, $baseUri)
, where:
$input
can be (almost) "anything containing RDF" (an RDF string, a path to a file, an URL, an opened resource (result offopen()
), a PSR-7 response or a PSR-7 StreamInterface object).$dataFactory
is an object implementing the\rdfInterface\DataFactory
interface, e.g.new \quickRdf\DataFactory()
.$format
is an optional explicit RDF format indication for handling rare situtations when the format can't be autodetected. See thesrc/quickRdfIo/Util.php::getParser()
source code to see a list of all accepted$format
values.$baseUri
is an optional baseURI value (for some kind of$input
values it can be autodected).
Basic serialization
Just use \quickRdfIo\Util::serialize($data, $format, $output, $nmsp)
, where:
$data
is an object implementing the\rdfInterface\QuadIterator
interface, e.g. a Dataset or an iterator returned by the parser.$format
specifies an RDF serialization format, e.g.turtle
orntriples
. See thesrc/quickRdfIo/Util.php::getSeriazlier()
source code to see a list of all accepted$format
values.$output
is an optional parameter describing where the output should be written. If it's missing or null, output is returned as a string. If it's a string, it's treated as a path to open withfopen($output, 'wb')
. If it's a stream resource or PSR-8StreamInterface
instance, the output is just written into it.$nmsp
is an optional parameter used to pass desired RDF namespace aliases to the serializer. Note some formats like n-triples and n-quads don't support namespace aliases while in others (e.g. turtle) it's very common to use them.
Basic conversion
Basic filtering without a Dataset
It's worth noting that basic triples/quads filtering can be done in a memory efficient way without usage of a Dataset implementation.
Let's say we want to copy all triples with the https://vocabs.acdh.oeaw.ac.at/schema#hasIdentifier
predicate
from the test/files/puzzle4d_100k.nt
n-triples file into a ids.ttl
turtle file.
A typical approach would be to load data into a Dataset, filter them there and finally serialize the Dataset:
but it can be also done by using a "filtering generator" instead of the Dataset. With this approach we avoid materializing the whole dataset in memory which should both reduce memory footprint and speed things up a little:
Results are better but the memory footprint is still surprisingly high.
This is because of the DataFactory implementation we've used and performance optimizations it's applying
(which admitedly in our scenario only slow things down).
We can can optimize further by using as dumb as possible DataFactory implementation
(for that we need another package - sweetrdf/simple-rdf
):
As we can see the optimized implementation is 2.3 times faster and has 60 times lower memory footprint that a Dataset-based one.
Notes:
- Check the sweetrdf/term-templates library for more classes allowing to easily match triples/quads fulfilling given conditions.
- This approach is not limited to filtering. Simple triples/quads modifications can be applied similar way
(just adjust the "filtering generator"
foreach
loop body).
Manual parser/serializer instantiation
It's of course possible to instantiate particular parser/serializer explicitly.
This is the only option to fine-tune parser/serializer configuration, e.g.:
-
Create a strict n-triples parser
- Create a JsonLD serializer applying compacting with a context read from a given file and producing pretty-printed JSON:
Be aware that parsing/serialization with the manually created parser/serializer instance requires a little more code.
Compare
All versions of quick-rdf-io with dependencies
ext-mbstring Version *
ext-pcre Version *
ext-xmlreader Version *
zozlak/rdf-constants Version ^1.1
sweetrdf/rdf-interface Version ^2
sweetrdf/rdf-helpers Version ^2
pietercolpaert/hardf Version >=0.3.1 <1
ml/json-ld Version ^1.2