Java Driver API reference
Query section
Query section consists of the following methods:
TypeDB Driver methods
Instantiating a TypeDB Core client
Description
In order to communicate with TypeDB Core databases via sessions and transactions, we first need to instantiate a TypeDB Core client. The created object connects our application with the running TypeDB server.
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
address |
The address (host:port) on which the TypeDB server is running |
String |
True |
|
parallelisation |
The number of threads to use for server communication |
int |
False |
Scales based on host machine’s number of available CPU cores |
Instantiating a TypeDB cluster client
Syntax
TypeDB.clusterClient(Set<String> addresses, TypeDBCredential credential, int parallelisation);
Description
In order to communicate with TypeDB cluster (TypeDB Enterprise & TypeDB Cloud) databases via sessions and transactions, we first need to instantiate a TypeDB Cluster client. The created object connects our application with the running TypeDB cluster.
When instantiating a TypeDB cluster client, it is sufficient to supply the address of just one server, as the addresses of the other servers will be relayed back to the client. However, to avoid failure in the unlikely event that the single server whose address is provided fails before communicating the addresses of the others, it is best practice to supply addresses of all the servers.
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
addresses |
Addresses (host:port) on which TypeDB cluster nodes are running |
Set of String |
True |
|
credential |
User credential and TLS encryption setting |
TypeDBCredential |
True |
|
parallelisation |
The number of threads to use for server communication |
int |
False |
Scales based on host machine’s number of available CPU cores |
Client methods
Create a session for a database
Description
Opens a communication tunnel (session) to the given database on the running TypeDB server.
For more information on the methods, available with sessions, see the Session methods section.
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
database |
The name of the database with which the session connects. |
String |
True |
N/A |
type |
The type of session to be created (DATA or SCHEMA) |
SessionType |
True |
N/A |
options |
Options for the session. |
False |
N/A |
Create a database
Retrieve a database
Delete a database
Create a user
Retrieve a user
Delete a user
Session methods
Open a transaction
Description
Opens a transaction to perform read or write queries on the database connected to the session.
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
transactionType |
The type of transaction to be created (READ or WRITE). |
Transaction.Type |
True |
|
options |
Options for the session. |
[ |
False |
N/A |
Transaction methods
Access Concept API methods
Description
Gets the ConceptManager
for this Transaction, providing access to all
Concept methods.
Logic methods
Description
Gets the LogicManager
for this Transaction, providing access to all
Logic methods.
Access Query API methods
Commit a write transaction
QueryManager methods
Execute a TypeQL Match (Get) query
Execute a TypeQL Match Aggregate query
Execute a TypeQL Match Group query
Execute a TypeQL Match Group Aggregate query
Execute a TypeQL Insert query
Execute a TypeQL Delete query
Execute a TypeQL Update query
Execute a TypeQL Explain query
Execute a TypeQL Define query
Options
The transaction query options object TypeDBOptions
can be used to override the default server behaviour
query processing. Use TypeDBOptions.core()
to create a new TypeDB Core options object, or TypeDBOptions.cluster()
for cluster client for TypeDB Enterprise & TypeDB Cloud.
The options are:
-
infer
: whether to enable inference for the provided query (only settable at transaction level and above, and only affects read transactions) (default:false
). -
explain
: whether to enable explanations for the provided query (only affects read transactions) (default:false
). -
parallel
: whether the server should use parallel or single-threaded execution (default:true
). -
prefetchSize
: a guideline number of answers that the server should send before the client issues a fresh request (default:50
). -
traceInference
: if enabled, outputs reasoning tracing graphs in the logging directory. Should be used withparallel = false
. (default:false
). -
prefetch
: if enabled, the first batch of answers is streamed to the client even without an explicit request for it (default:true
if query type ismatch
, otherwisefalse
). -
sessionIdleTimeoutMillis
: this timeout allows the server to close sessions if a client terminates or becomes unresponsive (default:30_000
). -
transactionTimeoutMillis
: this timeout will automatically kill transactions, preventing memory leaks in unclosed transactions (default:300_000
). -
schemaLockAcquireTimeoutMillis
: how long the client should wait if opening a session or transaction is blocked by a schema write lock (default:10_000
).
TypeDBClusterOptions
has an additional option:
-
readAnyReplica
: enables reading data from any replica, potentially boosting read throughput (default:false
).
Explicitly enable or disable inference
Description
Override the server defaults to enable or disable inference for the provided query (only settable at transaction level and above, and only affects read transactions).
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
enabled |
explicitly set inference on or off for this query |
boolean |
True |
Explicitly enable or disable explanations
Description
Override the server defaults to enable or disable explanation availability for the provided query (only affects read transactions).
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
enabled |
explicitly set explanations on or off for this query |
boolean |
True |
Force the server parallelism
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
parallel |
use parallel or single-threaded query execution |
boolean |
True |
Explicitly set query prefetch to a certain size
Description
Override the server defaults for answer batch streaming. This tells the server to pre-compute and stream this number of answers at a time. These are buffered in the client until read. A larger batch size causes the server to compute more answers at a time, blocking the transaction until the batch is computed.
Trace and log reasoning inference
Description
Create reasoning traces as graphs logged in the logging directory. Should be used with parallel = false
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
traceInference |
trace reasoning execution as graphs in log directory |
boolean |
True |
Explicitly enable or disable prefetch
Description
If enabled, the first batch of answers is streamed to the client even without an explicit request for it
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
prefetch |
if set to true, the first batch of answers is streamed to the client even without an explicit request for it |
boolean |
True |
Override default session idle timeout
Description
This timeout allows the server to close sessions if a client terminates or becomes unresponsive
Override default transaction timeout
Description
This timeout automatically closes transactions that exceed the configured time. This prevents memory leaks caused by transactions accidentally left unclosed, and kills unresponsive transactions.
Override default schema lock acquire timeout
Description
This timeout allows the server to close sessions if a client terminates or becomes unresponsive
Response section
Response section consists of the following methods:
Answer methods
ConceptMap methods
Retrieve a mapping of variables to concepts
Description
Produces a Map where keys are query variables, and values are concepts.
Returns
-
Map<String, Concept>
Retrieve concepts
Description
Return a collection of all the concepts in this ConceptMap.
Returns
-
Collection<Concept>
Retrieve a concept corresponding to a specific variable
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
var |
The string representation of a variable |
String |
Retrieve explainable concepts
Description
Gets the Explainables object for this ConceptMap, exposing which of the Concepts in this ConceptMap are explainable
ConceptMapGroup methods
Retrieve the ConceptMaps of the group
Returns
-
List<
ConceptMap
>
Explainables methods
Retrieve explainable relation
Retrieve explainable attribute
Retrieve explainable ownership
Description
Retrieves the explainable attribute ownership with the pair of (owner, attribute) variable names.
Concept methods
Get a Remote version of the concept.
ConceptManager methods
Retrieve an EntityType
Create or retrieve an EntityType
Description
Creates a new EntityType if none exists with the given label, otherwise retrieves the existing one.
Retrieve a RelationType
Create or retrieve a RelationType
Description
Creates a new RelationType if none exists with the given label, otherwise retrieves the existing one.
Retrieve an AttributeType
Create or retrieve an AttributeType
Description
Creates a new AttributeType if none exists with the given label, or retrieves the existing one.
Type methods
Type
has all the Concept
methods plus what follows.
Get a Remote version of the type.
Rename label
ThingType methods
ThingType
has all the Type
methods plus what follows.
Get a Remote version of the ThingType.
Add new playable role
Add attribute ownership
Syntax
thingType.asRemote(Transaction tx).setOwns(AttributeType attributeType, Set<TypeQLToken.Annotation> annotations);
Add attribute ownership (overriden)
Syntax
thingType.asRemote(Transaction tx).setOwns(AttributeType attributeType, AttributeType overriddenType, Set<TypeQLToken.Annotation> annotations);
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
attributeType |
The AttributeType to be owned by the instances of this type. |
[ |
True |
N/A |
overriddenType |
The AttributeType that this attribute ownership overrides, if applicable. |
[ |
False |
None |
annotations |
Adds annotations (KEY or UNIQUE) to the ownership. |
set of |
False |
Collections.emptySet(); |
Retrieve playable roles
Retrieve attributes
Description
Retrieves attribute types that the instances of this type are allowed to own directly or via inheritance.
Retrieve attributes
Retrieve attributes
Syntax
thingType.asRemote(Transaction tx).getOwns(AttributeType.ValueType valueType, Set<TypeQLToken.Annotation> annotations);
Description
Retrieves attribute types that the instances of this type are allowed to own directly or via inheritance.
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
valueType |
If specified, only attribute types of this ValueType will be retrieved. |
|
False |
None |
annotations |
Only retrieve attribute types owned with annotations (KEY or UNIQUE). |
set of |
False |
Collections.emptySet(); |
Retrieve attributes
Syntax
thingType.asRemote(Transaction tx).getOwnsExplicit(AttributeType.ValueType valueType, Set<TypeQLToken.Annotation> annotations);
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
valueType |
If specified, only attribute types of this ValueType will be retrieved. |
|
False |
None |
annotations |
Only retrieve attribute types owned with annotations (KEY or UNIQUE). |
set of |
False |
Collections.emptySet(); |
Remove role
EntityType methods
EntityType
has all the ThingType
methods plus what follows.
Set supertype
RelationType methods
RelationType
has all the ThingType
methods plus what follows.
Retrieve specific role
Description
Retrieve the role that this RelationType relates to, if it exists, with the specified label.
Set new role
Description
Sets the new role that this RelationType relates to. If we are setting an overriding type this way, we have to also pass the overridden type as a second argument.
Remove role
Set supertype
Get a Remote version of the RelationType.
RoleType methods
RoleType
has all the Type
methods plus what follows.
Get a Remote version of the role type.
AttributeType methods
AttributeType
has all the ThingType
methods plus what follows.
Set supertype
Retrieve all subtypes
Returns
-
Stream<
AttributeType
>
Retrieve all instances of this AttributeType
Returns
-
Stream<
Attribute
>
Retrieve direct and inherited owners of this Type of Attribute
Description
Retrieve all Things that own an attribute of this type.
Optionally, filtered by annotations provided.
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
annotations |
Only retrieve things that have an attribute of this type with annotations (KEY or UNIQUE). |
set of |
False |
Collections.emptySet(); |
Returns
-
Stream<
Thing
>
Retrieve direct owners of this Type of Attribute
Syntax
attributeType.asRemote(Transaction tx).getOwnersExplicit(Set<TypeQLToken.Annotation> annotations);
Description
Retrieve all Things that directly own an attribute of this type.
Optionally, filtered by annotations provided.
Input parameters
annotations |
---|
Only retrieve things that have an attribute of this type with annotations (KEY or UNIQUE). |
set of |
False |
Collections.emptySet(); |
Returns
-
Stream<
Thing
>
Get a Remote version of the AttributeType.
BooleanAttributeType methods
BooleanAttributeType
has all the AttributeType
methods plus what follows.
Put instance
Puts and returns a new instance of this BooleanAttributeType with the specified value. If an attribute with this value does not yet exist, it will be created.
Get instance
Returns the instance, if it exists, of this BooleanAttributeType with the specified value.
Set supertype
booleanAttributeType.asRemote(Transaction tx).setSupertype(AttributeType.Boolean booleanAttributeType);
Sets the supplied BooleanAttributeType as the supertype of the current BooleanAttributeType
Retrieve all subtypes
-
Stream<
AttributeType.Boolean
>
Retrieve all instances of this BooleanAttributeType
-
Stream<
BooleanAttribute
>
LongAttributeType methods
LongAttributeType
has all the AttributeType
methods plus what follows.
Put instance
Puts and returns a new instance of this LongAttributeType with the specified value. If an attribute with this value does not yet exist, it will be created.
Get instance
Set Supertype
DoubleAttributeType methods
DoubleAttributeType
has all the AttributeType
methods plus what follows.
Put instance
Puts and returns a new instance of this DoubleAttributeType with the specified value. If an attribute with this value does not yet exist, it will be created.
Get instance
Returns the instance, if it exists, of this DoubleAttributeType with the specified value.
Set Supertype
attributeType.asRemote(Transaction tx).setSupertype(AttributeType.Double doubleAttributeType);
Sets the supplied DoubleAttributeType as the supertype of the current DoubleAttributeType
StringAttributeType methods
StringAttributeType
has all the AttributeType
methods plus what follows.
Put instance
Puts and returns a new instance of this StringAttributeType with the specified value. If an attribute with this value does not yet exist, it will be created.
Get instance
Returns the instance, if it exists, of this StringAttributeType with the specified value.
Set regex
Set Supertype
attributeType.asRemote(Transaction tx).setSupertype(AttributeType.String stringAttributeType);
Sets the supplied StringAttributeType as the supertype of the current StringAttributeType
DateTimeAttributeType methods
DateTimeAttributeType
has all the AttributeType
methods plus what follows.
Put instance
Puts and returns a new instance of this DateTimeAttributeType with the specified value. If an attribute with this value does not yet exist, it will be created.
Get instance
Returns the instance, if it exists, of this DateTimeAttributeType with the specified value.
Set Supertype
attributeType.asRemote(Transaction tx).setSupertype(AttributeType.DateTime datetimeAttributeType);
Sets the supplied DateTimeAttributeType as the supertype of the current DateTimeAttributeType
Rule methods
Get a Remote version of the rule.
Rename label
LogicManager
Retrieve a Rule
Create or retrieve a Rule
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
label |
The label of the Rule to create or retrive. |
String |
True |
N/A |
when |
The pattern describing the when body of the rule. |
[ |
True |
N/A |
then |
The pattern describing the then body of the rule. |
[ |
True |
N/A |
Data methods
Data
has all the Concept
methods plus what follows.
Get a Remote version of the Thing.
Assign attribute
Unassign attribute
Retrieve attributes
Description
Retrieves the Attributes that this Thing owns.
Optionally, filtered by annotations provided.
Retrieve attributes
Description
Retrieves the Attributes that this Thing owns, optionally filtered by one or more AttributeTypes.
Retrieve attributes
Description
Retrieves the Attributes that this Thing owns, optionally filtered by one or more AttributeTypes.
Retrieve relations
Description
Retrieves all the Relations which this Thing plays a role in, optionally filtered by one or more given roles.
Entity methods
Entity
has all the Data
methods plus what follows.
Relation methods
Relation
has all the Data
methods plus what follows.
Retrieve role players
Description
Retrieves all roleplayers of this this Relation, optionally filtered by given role types.
Add roleplayer
Remove a roleplayer
Description
Removes the association of the given instance that plays the given role in this Relation.
Attribute methods
Attribute
has all the Data
methods plus what follows.
Retrieve owners
Input parameters
Name | Description | Type | Required | Default Value |
---|---|---|---|---|
ownerType |
If specified, filter results for only owners of the given type |
Type |
False |
N/A |
Returns
-
Stream<
Thing
>