Package: pytimbr-sqla
timbr Python connector sample fileThis is a sample repository for how to connect to timbr using SQLAlchemy and Python.DependenciesAccess to a timbr-serverPython from 3.7.13 or newerSupport SQLAlchemy 1.4.36 or newer but not version 2.x yet.InstallationInstall as clone repository:Install Python:https://www.python.org/downloads/release/python-3713/Run the following command to install the Python dependencies:pip install -r requirements.txtInstall using pip and git:pip install git+https://github.com/WPSemantix/timbr_python_SQLAlchemyInstall using pip:pip install pytimbr-sqlaKnown issuesIf you encounter a problem installingPyHivewith 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.whlFor Python 3.9:pip install https://download.lfd.uci.edu/pythonlibs/archived/sasl-0.3.1-cp39-cp39-win_amd64.whlSample usageFor an example of how to use the Python connector for timbr, follow thisexample fileConnect optionsConnect using 'pytimbr_sqla' and 'SQLAlchemy' packagesfromsqlalchemyimportcreate_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 nameuser_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 11000port='<timbr_server_port>'# The name of the ontology (knowledge graph) to connectontology='<ontology_name>'# Create new sqlalchemy connectionengine=create_engine(f"timbr+{protocol}://{user_name}@{ontology}:{user_pass}@{hostname}:{port}")conn=engine.connect()# Use the connection to execute a queryquery="SHOW CONCEPTS"concepts=conn.execute(query).fetchall()forconceptinconcepts:print(concept)Attention:timbr works only as async when running a query, if you want to use standard PyHive you have two optionsConnect using 'PyHive' and 'SQLAlchemy' packagesConnect using PyHive Async Queryfromsqlalchemyimportcreate_enginefromTCLIService.ttypesimportTOperationState# Connection protocol can be 'http' or 'https'protocol='http'# Use 'token' as the username when connecting using a Timbr token, otherwise use the user nameuser_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 11000port='<timbr_server_port>'# The name of the ontology (knowledge graph) to connectontology='<ontology_name>'# Create new sqlalchemy connectionengine=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 queryquery="SHOW CONCEPTS"cursor.execute(query)# Check the status of this executionstatus=cursor.poll().operationStatewhilestatusin(TOperationState.INITIALIZED_STATE,TOperationState.RUNNING_STATE):status=cursor.poll().operationStateresults=cursor.fetchall()print(results)Connect using PyHive Sync Queryfromsqlalchemyimportcreate_enginefromTCLIService.ttypesimportTOperationState# Connection protocol can be 'http' or 'https'protocol='http'# Use 'token' as the username when connecting using a Timbr token, otherwise use the user nameuser_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 11000port='<timbr_server_port>'# The name of the ontology (knowledge graph) to connectontology='<ontology_name>'# Create new sqlalchemy connectionengine=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 queryquery="SHOW CONCEPTS"results=conn.execute(query).fetchall()print(results)