Skip to main content

Timbr SQLAlchemy and Python connector

This is a sample for connecting to Timbr using SQLAlchemy and Python.


Dependencies

  • Access to a timbr-server
  • Python from 3.7.13 or newer

Installation

  • Install as clone repository:

  • Install using pip and git:

    • pip install git+https://github.com/WPSemantix/timbr_python_SQLAlchemy
  • Install using pip:

    • pip install pytimbr_sqla

Known issues

If you encounter a problem installing PyHive with sasl dependencies on windows, install the following wheel (for 64bit Windows) by running:

pip install https://download.lfd.uci.edu/pythonlibs/archived/cp37/sasl-0.3.1-cp37-cp37m-win_amd64.whl


Sample usage

  • For an example of how to use the Python connector for timbr, follow this Example File

Connecting using 'pytimbr_sqla' and 'SQLAlchemy' packages

  from sqlalchemy import create_engine

# Connection protocol can be 'http' or 'https'
protocol = 'http'
# Use 'token' as the username when connecting using a Timbr token, otherwise use the user name
user_name = 'token'
# If using a token as a username then the pass is the token value, otherwise its the user's password.
user_pass = '<token_value_or_user_password>'
# The IP / Hostname of the Timbr server (not necessarily the hostname of the Timbr platform).
hostname = '<timbr_server_host>'
# The port to connect to in the Timbr server. Timbr's default port is 11000
port = '<timbr_server_port>'
# The name of the ontology (knowledge graph) to connect
ontology = '<ontology_name>'

# Create new sqlalchemy connection
engine = create_engine(f"timbr+{protocol}://{user_name}@{ontology}:{user_pass}@{hostname}:{port}")
conn = engine.connect()

# Use the connection to execute a query
query = "SHOW CONCEPTS"
concepts = conn.execute(query).fetchall()
for concept in concepts:
print(concept)
Attention

Timbr works only as async when running a query, if you want to use standard PyHive you have two options:

  • Connect using 'PyHive' package
  • Connect using'SQLAlchemy' package

Connecting using PyHive Async Query

  from sqlalchemy import create_engine
from TCLIService.ttypes import TOperationState

# Connection protocol can be 'http' or 'https'
protocol = 'http'
# Use 'token' as the username when connecting using a Timbr token, otherwise use the user name
user_name = 'token'
# If using a token as a username then the pass is the token value, otherwise its the user's password.
user_pass = '<token_value_or_user_password>'
# The IP / Hostname of the Timbr server (not necessarily the hostname of the Timbr platform).
hostname = '<timbr_server_host>'
# The port to connect to in the Timbr server. Timbr's default port is 11000
port = '<timbr_server_port>'
# The name of the ontology (knowledge graph) to connect
ontology = '<ontology_name>'

# Create new sqlalchemy connection
engine = create_engine(f"hive+{protocol}://{user_name}@{ontology}:{user_pass}@{hostname}:{port}", connect_args={'configuration': {'set:hiveconf:hiveMetadata': 'true'}})
conn = engine.connect()
dbapi_conn = engine.raw_connection()
cursor = dbapi_conn.cursor()

# Use the connection to execute a query
query = "SHOW CONCEPTS"
cursor.execute(query)

# Check the status of this execution
status = cursor.poll().operationState
while status in (TOperationState.INITIALIZED_STATE, TOperationState.RUNNING_STATE):
status = cursor.poll().operationState

results = cursor.fetchall()
print(results)

Connecting using PyHive Sync Query

  from sqlalchemy import create_engine
from TCLIService.ttypes import TOperationState

# Connection protocol can be 'http' or 'https'
protocol = 'http'
# Use 'token' as the username when connecting using a Timbr token, otherwise use the user name
user_name = 'token'
# If using a token as a username then the pass is the token value, otherwise its the user's password.
user_pass = '<token_value_or_user_password>'
# The IP / Hostname of the Timbr server (not necessarily the hostname of the Timbr platform).
hostname = '<timbr_server_host>'
# The port to connect to in the Timbr server. Timbr's default port is 11000
port = '<timbr_server_port>'
# The name of the ontology (knowledge graph) to connect
ontology = '<ontology_name>'

# Create new sqlalchemy connection
engine = create_engine(f"hive+{protocol}://{user_name}@{ontology}:{user_pass}@{hostname}:{port}", connect_args={'configuration': {'set:hiveconf:async': 'false', 'set:hiveconf:hiveMetadata': 'true'}})
conn = engine.connect()

# Use the connection to execute a query
query = "SHOW CONCEPTS"
results = conn.execute(query).fetchall()
print(results)