chrontext.chrontext
The hybrid query engine of chrontext. Initialize Engine using: - A SPARQL Database: either in the form of a SPARQL endpoint or an embedded Oxigraph SPARQL database - A Virtualized Database: one of the supported databases: - A Python defined database (could be anything) - Google Cloud BigQuery - OPC UA History Access
Construct a new hybrid query engine.
Specify exactly one of sparql_endpoint
and sparql_embedded_oxigraph
.
Parameters
- resources: The templates associated with each
- sparql_endpoint: A SPARQL endpoint (a URL)
- sparql_embedded_oxigraph: An embedded oxigraph SPARQL database, a Store-object.
Execute a query
Parameters
- query: The SPARQL query.
- native_dataframe: Return columns with chrontext-native formatting. Useful for round-trips into e.g. maplib.
- include_datatypes: Return datatypes of the results DataFrame (returns SolutionMappings instead of DataFrame).
Returns
The query result.
A virtualized database implemented in Python.
See the tutorial in README.md for guidance on how to use this class. This API is subject to change, it will be possible to specify what parts of the SPARQL query may be pushed down into the database. For advanced use, the resource_sql_map may be omitted, in which case the VirtualizedQuery will be provided to the query method. The user must then translate this VirtualizedQuery (built on SPARQL Algebra) to the target query language.
Parameters
- database: An instance of a class containing a query method.
- resource_sql_map: A dict providing a sqlalchemy Select for each resource.
- sql_dialect: The SQL dialect accepted by the query method.
A virtualized BigQuery database
To be able to connect to BigQuery, provide the path to the JSON key. For each resource name in chrontext that you want to associate with BigQuery, provide an sqlalchemy Select or Table that contains each of the parameters referenced in the corresponding template provided to Engine.
See test_bigquery.py in the tests for usage.
Parameters
- resource_sql_map: The SQLs associated with the resources
- key_json_path: Path to JSON containing Key to connect to BigQuery.
A DataProduct is a SPARQL query which is annotated with types. It defines a virtual SQL table.
Create a new data product from a SPARQL query and the types of the columns. The SPARQL should be a SELECT query and should explicitly include projected variables (don't use *).
>>> dp1 = DataProduct(query=query, types={
... "farm_name":RDFType.Literal("http://www.w3.org/2001/XMLSchema#string"),
... "turbine_name":RDFType.Literal("http://www.w3.org/2001/XMLSchema#string"),
... "t":RDFType.Literal("http://www.w3.org/2001/XMLSchema#dateTime"),
... "v":RDFType.Literal("http://www.w3.org/2001/XMLSchema#double")})
Parameters
- query: The SPARQL SELECT Query that defines the data product
- types: The types of each of the variables in the data product
A Catalog maps SPARQL queries to virtual SQL tables.
Create a new data product catalog, which defines virtual tables.
Parameters
- data_products: The data products in the catalog. Keys are the table names.
The type of a column containing a RDF variable. For instance, xsd:string is RDFType.Literal("http://www.w3.org/2001/XMLSchema#string")
A prefix that can be used to ergonomically build iris.
Create a new prefix.
Parameters
- prefix: The name of the prefix
- iri: The prefix IRI.
A variable in a template.
An RDF literal.
Create a new RDF Literal
Parameters
- value: The lexical representation of the value.
- data_type: The data type of the value (an IRI).
- language: The language tag of the value.
Create a new parameter for a Template.
Parameters
- variable: The variable.
- optional: Can the variable be unbound?
- allow_blank: Can the variable be bound to a blank node?
- rdf_type: The type of the variable. Can be nested.
- default_value: Default value when no value provided.
Parameters
- arguments: The arguments to the template.
- list_expander: (How) should we list-expand?
Returns
An OTTR Template. Note that accessing parameters- or instances-fields returns copies. To change these fields, you must assign new lists of parameters or instances.
A template instance.
Parameters
- iri: The IRI of the template to be instantiated.
- arguments: The arguments for template instantiation.
- list_expander: (How) should we do list expansion?
The xsd namespace, for convenience.
An OTTR Triple Pattern used for creating templates. This is the basis pattern which all template instances are rewritten into. Equivalent to:
>>> ottr = Prefix("http://ns.ottr.xyz/0.4/")
... Instance(ottr.suf("Triple"), subject, predicate, object, list_expander)
Parameters
- subject:
- predicate:
- object:
- list_expander:
Returns
Returns
IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
Parameters
- uri: The URI of the Flight server (see engine.serve_flight())
- metadata: gRPC metadata to add to each request
Execute a query
Parameters
- query: The SPARQL query.
- native_dataframe: Return columns with chrontext-native formatting. Useful for round-trips into e.g. maplib.
- include_datatypes: Return datatypes of the results DataFrame (returns SolutionMappings instead of DataFrame).
Returns
The query result.