The DataIO library opens the given files and allows to read them using an Iterator, a Stream or an RxJava's Observable. This library doesn't load the full file in memory (this is not the case for XML and ODS files).
The easiest is to include DataIO as a maven dependency:
<dependency>
<groupId>be.ugent.idlab.knows</groupId>
<artifactId>dataio</artifactId>
<version>2.4.2</version>
</dependency>Check the maven central repository for the latest version.
Or if you want to build & install locally:
Run the following command:
mvn install
Run the following command:
mvn test
Interfaces which allows to open a file (remote, database, SPARQL or WoT) and get the corresponding input stream.
getInputStream: opens the file and returns an inputstream.
A local or remote file access gives the file's content in UTF-8:
decompressed, without byte order mark, and transcoded from the encoding it is given.
The encoding is for text formats:
an XML document declares its own encoding, and Excel and ODS files are binary, so keep UTF-8 (the default) for them.
A CSV file's encoding goes to either the access or the CSVW configuration, which decodes the access's stream.
A remote file access follows redirects to http(s) URLs, except from https to http,
and sends its headers to the original host only.
For bytes in another encoding, such as a message, VirtualAccess.of(bytes, encoding) does the same.
getDataTypes: This method returns a map of datatypes. References to values are mapped to their datatypes, if available.
getContentType: gives the content type of the access object.
Interface for unbounded sources, where each message is a document of its own.
open() connects and gives a MessageStream, whose next() waits for the next message and gives its bytes,
or null once the source has ended.
Parse each message with a format iterator, and close the stream to release the connection;
another thread may close it to stop a waiting next().
MessageAccess access = new KafkaMessageAccess("localhost:9092", "people", null, Map.of());
try (MessageStream stream = access.open()) {
byte[] message;
while ((message = stream.next()) != null) {
try (SourceIterator records = new JSONSourceIterator(new VirtualAccess(message), "$.people[*]")) {
// ... read the message's records ...
}
}
}| Implementation | Messages |
|---|---|
KafkaMessageAccess(bootstrapServers, topic, groupId, properties) |
the values of the topic's records, in partition order; tombstones are skipped. A null groupId gives a random consumer group. A group without committed offsets reads the topic from its earliest offset, so messages published before the consumer starts are read too. properties are extra consumer properties, which override these defaults. The stream does not end by itself. |
WebSocketMessageAccess(uri, headers) |
each complete text message (as UTF-8) or binary message. The stream ends when the server closes the connection; after a connection error, next() throws it as an IOException once the received messages are read. |
WebSocketAccess instead collects the messages of a time window into one InputStream.
Interface which is an implementation of an Iterator, which overrides the remove and forEachRemaining as these function are trivial for each implementation.
Setting a source up costs more than reading it: a parser has to be built and the iterator's
expression compiled. A caller that reads many sources with the same configuration — one
record's worth of data at a time, for instance — can construct a single iterator and point
it at each source in turn with reset(Access), instead of constructing one per source.
XMLSourceIterator iterator = new XMLSourceIterator(firstAccess, "/people/person");
// ... read the first source ...
iterator.reset(secondAccess); // same iterator, same XPath, another sourceThe XML, JSON and CSV(W) iterators support this; the others throw
UnsupportedOperationException rather than returning the previous source's records. What is
kept alive differs per format:
| Iterator | Kept between sources | Effect |
|---|---|---|
XMLSourceIterator |
Saxon processor, document builder and XPath compiler, so the iterator's XPath is compiled once | ~4x faster over 300 sources of 10 records |
JSONSourceIterator |
the surfing configuration, which compiles the iteration path | ~3x faster, same measurement |
CSVWSourceIterator |
nothing: a CSV source has no expression to compile | no gain; offered so callers can treat every reference formulation alike |
A reused iterator is stateful: it belongs to whoever reset it until it is drained, and cannot be shared between threads. Constructing one per source remains safe.
As JSONPath is not yet standardized, compatibility issues may arise. We follow the implementation of JsonSurfer, with following additions
- A combination of the child operators will be reduced to a single child operator. In practice, this means that
$.['child']becomes$['child']. - In test cases, you may find a construction like
$.[*]. This construction will also be reduced to$[*].
Interface and implementations for streaming the records from sources.
Implementation of the Flow interface, using RxJava under the hood. Implementations of RxJava's Observable for the different records are provided.
open(args): this function opens the corresponding files (using an Access object) and initiates the iterator and other needed values to allow the creation of records (eg CSVSourceIterator initiates a header value).
Interface which generalizes the access to data
get(String value); returns a list of objects associated to the given string value.
getDataType(String value) returns the IRI of the datatype of a reference in the record
getValues(String reference) returns the elements of a reference's result, in result order, as NaturalValues:
a lexical form with the full IRI of its XSD datatype, such as http://www.w3.org/2001/XMLSchema#integer.
A JSONPath reference gives the one result of a definite path, or each element of an indefinite path's result.
A JSON object or array has no lexical form (hasLexicalForm() is false).
Null elements are left out, a reference without result gives the empty list,
and an invalid reference throws an InvalidReferenceException.
The datatype is the record's getDataType when it gives one,
such as an RDB column's datatype (INT gives xsd:integer),
and otherwise follows from the value: xsd:string, xsd:integer, xsd:double or xsd:boolean.
_PATH is a magic property that can be used to obtain a reverse path through the document to reach a specific object. Index notation can be used to grab a specific element.
Suppose a file people.json
{
"people": [
{
"firstName": "John",
"lastName": "Doe",
"phoneNumbers": [
"0123-4567-8888",
"0123-4567-8910"
]
}
]
}And suppose a JSON path $.people.[*], then a specific path for the first "people" object would be $.people.[0].
This object's _PATH property would resolve to [0,people], and _PATH[1] would resolve to people.