![Timbr logo](https://timbr.ai/wp-content/uploads/2023/06/timbr-ai-l-5-226x60-1.png)
# timbr Python connector sample file
This is a sample repository for how to connect to timbr using SQLAlchemy and Python.
## Dependencies
- Access to a timbr-server
- Python from 3.7.13 or newer
- Support SQLAlchemy 1.4.36 or newer but not version 2.x yet.
## Installation
- Install 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.txt`
- 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`
For Python 3.9:
`pip install https://download.lfd.uci.edu/pythonlibs/archived/sasl-0.3.1-cp39-cp39-win_amd64.whl`
## Sample usage
- For an example of how to use the Python SQLAlchemy connector for timbr, follow this [example file](examples/example.py)
- For an example of how to use the Python SQLAlchemy connector with 'PyHive' as async query for timbr, follow this [example file](examples/pyhive_async_example.py)
- For an example of how to use the Python SQLAlchemy connector with 'PyHive' as sync query for timbr, follow this [example file](examples/pyhive_sync_example.py)
## Connection parameters
### General example
```python
hostname = '<TIMBR_IP/HOST>'
port = '<TIMBR_PORT>'
ontology = '<ONTOLOGY_NAME>'
protocol = '<http/https>'
username = '<TIMBR_USER/token>'
password = '<TIMBR_PASSWORD/TOKEN_VALUE>'
# hostname - The IP / Hostname of the Timbr server (not necessarily the hostname of the Timbr platform).
# port - The port to connect to in the Timbr server. Timbr's default port with enabled_ssl is 443 without SSL is 11000.
# ontology = The name of the ontology (knowledge graph) to connect.
# protocol - Connection protocol can be 'http' or 'https'.
# username - Use 'token' as the username when connecting using a Timbr token, otherwise use the user name.
# password - If using a token as a username then the pass is the token value, otherwise its the user's password.
```
### HTTP example with dummy data
#### Username and password
```python
hostname = 'mytimbrenv.com'
port = '11000'
ontology = 'my_ontology'
protocol = 'http'
username = 'timbr'
password = 'StrongPassword'
```
#### Timbr token
```python
hostname = 'mytimbrenv.com'
port = '11000'
ontology = 'my_ontology'
protocol = 'http'
username = 'token'
password = '<TOKEN_VALUE>'
```
### HTTPS example with dummy data
#### Username and password
```python
hostname = 'mytimbrenv.com'
port = '443'
ontology = 'my_ontology'
protocol = 'https'
username = 'timbr'
password = 'StrongPassword'
```
#### Timbr token
```python
hostname = 'mytimbrenv.com'
port = '443'
ontology = 'my_ontology'
protocol = 'https'
username = 'token'
password = '<TOKEN_VALUE>'
```
## Connect options
### Connect using 'pytimbr_sqla' and 'SQLAlchemy' packages
```python
from sqlalchemy import create_engine
# Declare the connection variables
# General example
hostname = '<TIMBR_IP/HOST>'
port = '<TIMBR_PORT>'
ontology = '<ONTOLOGY_NAME>'
protocol = '<http/https>'
username = '<TIMBR_USER/token>'
password = '<TIMBR_PASSWORD/TOKEN_VALUE>'
# hostname - The IP / Hostname of the Timbr server (not necessarily the hostname of the Timbr platform).
# port - The port to connect to in the Timbr server. Timbr's default port with enabled_ssl is 443 without SSL is 11000.
# ontology = The name of the ontology (knowledge graph) to connect.
# protocol - Connection protocol can be 'http' or 'https'.
# username - Use 'token' as the username when connecting using a Timbr token, otherwise use the user name.
# password - If using a token as a username then the pass is the token value, otherwise its the user's password.
# Create new sqlalchemy connection
engine = create_engine(f"timbr+{protocol}://{username}@{ontology}:{password}@{hostname}:{port}")
# Connect to the created engine
conn = engine.connect()
# Execute a query
query = "SHOW CONCEPTS"
concepts = conn.execute(query).fetchall()
# Display the results of the execution
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' and 'SQLAlchemy' packages
#### Connect using PyHive Async Query
```python
from sqlalchemy import create_engine
from TCLIService.ttypes import TOperationState
# Declare the connection variables
# General example
hostname = '<TIMBR_IP/HOST>'
port = '<TIMBR_PORT>'
ontology = '<ONTOLOGY_NAME>'
protocol = '<http/https>'
username = '<TIMBR_USER/token>'
password = '<TIMBR_PASSWORD/TOKEN_VALUE>'
# hostname - The IP / Hostname of the Timbr server (not necessarily the hostname of the Timbr platform).
# port - The port to connect to in the Timbr server. Timbr's default port with enabled_ssl is 443 without SSL is 11000.
# ontology = The name of the ontology (knowledge graph) to connect.
# protocol - Connection protocol can be 'http' or 'https'.
# username - Use 'token' as the username when connecting using a Timbr token, otherwise use the user name.
# password - If using a token as a username then the pass is the token value, otherwise its the user's password.
# Create new sqlalchemy connection
engine = create_engine(f"hive+{protocol}://{username}@{ontology}:{password}@{hostname}:{port}", connect_args={'configuration': {'set:hiveconf:hiveMetadata': 'true'}})
# Connect to the created engine
conn = engine.connect()
dbapi_conn = engine.raw_connection()
cursor = dbapi_conn.cursor()
# 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
# Display the results of the execution
results = cursor.fetchall()
print(results)
```
#### Connect using PyHive Sync Query
```python
from sqlalchemy import create_engine
from TCLIService.ttypes import TOperationState
# Declare the connection variables
# General example
hostname = '<TIMBR_IP/HOST>'
port = '<TIMBR_PORT>'
ontology = '<ONTOLOGY_NAME>'
protocol = '<http/https>'
username = '<TIMBR_USER/token>'
password = '<TIMBR_PASSWORD/TOKEN_VALUE>'
# hostname - The IP / Hostname of the Timbr server (not necessarily the hostname of the Timbr platform).
# port - The port to connect to in the Timbr server. Timbr's default port with enabled_ssl is 443 without SSL is 11000.
# ontology = The name of the ontology (knowledge graph) to connect.
# protocol - Connection protocol can be 'http' or 'https'.
# username - Use 'token' as the username when connecting using a Timbr token, otherwise use the user name.
# password - If using a token as a username then the pass is the token value, otherwise its the user's password.
# Create new sqlalchemy connection
engine = create_engine(f"hive+{protocol}://{username}@{ontology}:{password}@{hostname}:{port}", connect_args={'configuration': {'set:hiveconf:async': 'false', 'set:hiveconf:hiveMetadata': 'true'}})
# Connect to the created engine
conn = engine.connect()
# Use the connection to execute a query
query = "SHOW CONCEPTS"
results = conn.execute(query).fetchall()
# Display the results of the execution
print(results)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/WPSemantix/timbr_python_SQLAlchemy",
"name": "pytimbr-sqla",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "timbr, timbr-python, timbr-connector, python-connector, PyTimbr, pytimbr, py-timbr, Py-Timbr, pytimbr_sqla, pytimbr_Sqla, PyTimbr_Sqla, pytimbr_SQla, PyTimbr_SQla, pytimbr_SQLa, PyTimbr_SQLa, pytimbr_SQlA, PyTimbr_SQLA, pytimbrsqlalchemy, PyTimbrSqlalchemy, PyTimbrSQlalchemy, PyTimbrSQLalchemy, PyTimbrSQLAlchemy, Py-TimbrSQLAlchemy, py-timbrsqlalchemy, Py-Timbr-SQLAlchemy, py-timbr-sqlalchemy",
"author": "timbr",
"author_email": "contact@timbr.ai",
"download_url": "https://files.pythonhosted.org/packages/0a/56/952205ee3bef80e02839d62158de563e0b10e255e10ef3a30a1f4aa7e859/pytimbr_sqla-1.0.4.tar.gz",
"platform": null,
"description": "![Timbr logo](https://timbr.ai/wp-content/uploads/2023/06/timbr-ai-l-5-226x60-1.png)\r\n\r\n# timbr Python connector sample file\r\nThis is a sample repository for how to connect to timbr using SQLAlchemy and Python.\r\n\r\n## Dependencies\r\n- Access to a timbr-server\r\n- Python from 3.7.13 or newer\r\n- Support SQLAlchemy 1.4.36 or newer but not version 2.x yet.\r\n\r\n## Installation\r\n- Install as clone repository:\r\n - Install Python: https://www.python.org/downloads/release/python-3713/\r\n - Run the following command to install the Python dependencies: `pip install -r requirements.txt`\r\n\r\n- Install using pip and git:\r\n - `pip install git+https://github.com/WPSemantix/timbr_python_SQLAlchemy`\r\n\r\n- Install using pip:\r\n - `pip install pytimbr-sqla`\r\n\r\n## Known issues\r\nIf you encounter a problem installing `PyHive` with sasl dependencies on windows, install the following wheel (for 64bit Windows) by running:\r\n\r\n`pip install https://download.lfd.uci.edu/pythonlibs/archived/cp37/sasl-0.3.1-cp37-cp37m-win_amd64.whl`\r\n\r\nFor Python 3.9:\r\n\r\n`pip install https://download.lfd.uci.edu/pythonlibs/archived/sasl-0.3.1-cp39-cp39-win_amd64.whl`\r\n\r\n## Sample usage\r\n- For an example of how to use the Python SQLAlchemy connector for timbr, follow this [example file](examples/example.py)\r\n- For an example of how to use the Python SQLAlchemy connector with 'PyHive' as async query for timbr, follow this [example file](examples/pyhive_async_example.py)\r\n- For an example of how to use the Python SQLAlchemy connector with 'PyHive' as sync query for timbr, follow this [example file](examples/pyhive_sync_example.py)\r\n\r\n## Connection parameters\r\n\r\n### General example\r\n```python\r\n hostname = '<TIMBR_IP/HOST>'\r\n port = '<TIMBR_PORT>'\r\n ontology = '<ONTOLOGY_NAME>'\r\n protocol = '<http/https>'\r\n username = '<TIMBR_USER/token>'\r\n password = '<TIMBR_PASSWORD/TOKEN_VALUE>'\r\n\r\n # hostname - The IP / Hostname of the Timbr server (not necessarily the hostname of the Timbr platform).\r\n # port - The port to connect to in the Timbr server. Timbr's default port with enabled_ssl is 443 without SSL is 11000.\r\n # ontology = The name of the ontology (knowledge graph) to connect.\r\n # protocol - Connection protocol can be 'http' or 'https'.\r\n # username - Use 'token' as the username when connecting using a Timbr token, otherwise use the user name.\r\n # password - If using a token as a username then the pass is the token value, otherwise its the user's password.\r\n```\r\n\r\n### HTTP example with dummy data\r\n\r\n#### Username and password\r\n```python\r\n hostname = 'mytimbrenv.com'\r\n port = '11000'\r\n ontology = 'my_ontology'\r\n protocol = 'http'\r\n username = 'timbr'\r\n password = 'StrongPassword'\r\n```\r\n\r\n#### Timbr token\r\n```python\r\n hostname = 'mytimbrenv.com'\r\n port = '11000'\r\n ontology = 'my_ontology'\r\n protocol = 'http'\r\n username = 'token'\r\n password = '<TOKEN_VALUE>'\r\n```\r\n\r\n### HTTPS example with dummy data\r\n\r\n#### Username and password\r\n```python\r\n hostname = 'mytimbrenv.com'\r\n port = '443'\r\n ontology = 'my_ontology'\r\n protocol = 'https'\r\n username = 'timbr'\r\n password = 'StrongPassword'\r\n```\r\n\r\n#### Timbr token\r\n```python\r\n hostname = 'mytimbrenv.com'\r\n port = '443'\r\n ontology = 'my_ontology'\r\n protocol = 'https'\r\n username = 'token'\r\n password = '<TOKEN_VALUE>'\r\n```\r\n## Connect options\r\n\r\n### Connect using 'pytimbr_sqla' and 'SQLAlchemy' packages\r\n```python\r\n from sqlalchemy import create_engine\r\n\r\n # Declare the connection variables\r\n # General example\r\n hostname = '<TIMBR_IP/HOST>'\r\n port = '<TIMBR_PORT>'\r\n ontology = '<ONTOLOGY_NAME>'\r\n protocol = '<http/https>'\r\n username = '<TIMBR_USER/token>'\r\n password = '<TIMBR_PASSWORD/TOKEN_VALUE>'\r\n\r\n # hostname - The IP / Hostname of the Timbr server (not necessarily the hostname of the Timbr platform).\r\n # port - The port to connect to in the Timbr server. Timbr's default port with enabled_ssl is 443 without SSL is 11000.\r\n # ontology = The name of the ontology (knowledge graph) to connect.\r\n # protocol - Connection protocol can be 'http' or 'https'.\r\n # username - Use 'token' as the username when connecting using a Timbr token, otherwise use the user name.\r\n # password - If using a token as a username then the pass is the token value, otherwise its the user's password.\r\n \r\n # Create new sqlalchemy connection\r\n engine = create_engine(f\"timbr+{protocol}://{username}@{ontology}:{password}@{hostname}:{port}\")\r\n\r\n # Connect to the created engine\r\n conn = engine.connect()\r\n\r\n # Execute a query\r\n query = \"SHOW CONCEPTS\"\r\n concepts = conn.execute(query).fetchall()\r\n \r\n # Display the results of the execution\r\n for concept in concepts:\r\n print(concept)\r\n```\r\n### Attention:\r\n### timbr works only as async when running a query, if you want to use standard PyHive you have two options\r\n\r\n### Connect using 'PyHive' and 'SQLAlchemy' packages\r\n\r\n#### Connect using PyHive Async Query\r\n```python\r\n from sqlalchemy import create_engine\r\n from TCLIService.ttypes import TOperationState\r\n\r\n # Declare the connection variables\r\n # General example\r\n hostname = '<TIMBR_IP/HOST>'\r\n port = '<TIMBR_PORT>'\r\n ontology = '<ONTOLOGY_NAME>'\r\n protocol = '<http/https>'\r\n username = '<TIMBR_USER/token>'\r\n password = '<TIMBR_PASSWORD/TOKEN_VALUE>'\r\n\r\n # hostname - The IP / Hostname of the Timbr server (not necessarily the hostname of the Timbr platform).\r\n # port - The port to connect to in the Timbr server. Timbr's default port with enabled_ssl is 443 without SSL is 11000.\r\n # ontology = The name of the ontology (knowledge graph) to connect.\r\n # protocol - Connection protocol can be 'http' or 'https'.\r\n # username - Use 'token' as the username when connecting using a Timbr token, otherwise use the user name.\r\n # password - If using a token as a username then the pass is the token value, otherwise its the user's password.\r\n \r\n # Create new sqlalchemy connection\r\n engine = create_engine(f\"hive+{protocol}://{username}@{ontology}:{password}@{hostname}:{port}\", connect_args={'configuration': {'set:hiveconf:hiveMetadata': 'true'}})\r\n\r\n # Connect to the created engine\r\n conn = engine.connect()\r\n dbapi_conn = engine.raw_connection()\r\n cursor = dbapi_conn.cursor()\r\n\r\n # Execute a query\r\n query = \"SHOW CONCEPTS\"\r\n cursor.execute(query)\r\n\r\n # Check the status of this execution\r\n status = cursor.poll().operationState\r\n while status in (TOperationState.INITIALIZED_STATE, TOperationState.RUNNING_STATE):\r\n status = cursor.poll().operationState\r\n\r\n # Display the results of the execution\r\n results = cursor.fetchall()\r\n print(results)\r\n```\r\n\r\n#### Connect using PyHive Sync Query\r\n```python\r\n from sqlalchemy import create_engine\r\n from TCLIService.ttypes import TOperationState\r\n\r\n # Declare the connection variables\r\n # General example\r\n hostname = '<TIMBR_IP/HOST>'\r\n port = '<TIMBR_PORT>'\r\n ontology = '<ONTOLOGY_NAME>'\r\n protocol = '<http/https>'\r\n username = '<TIMBR_USER/token>'\r\n password = '<TIMBR_PASSWORD/TOKEN_VALUE>'\r\n\r\n # hostname - The IP / Hostname of the Timbr server (not necessarily the hostname of the Timbr platform).\r\n # port - The port to connect to in the Timbr server. Timbr's default port with enabled_ssl is 443 without SSL is 11000.\r\n # ontology = The name of the ontology (knowledge graph) to connect.\r\n # protocol - Connection protocol can be 'http' or 'https'.\r\n # username - Use 'token' as the username when connecting using a Timbr token, otherwise use the user name.\r\n # password - If using a token as a username then the pass is the token value, otherwise its the user's password.\r\n\r\n # Create new sqlalchemy connection\r\n engine = create_engine(f\"hive+{protocol}://{username}@{ontology}:{password}@{hostname}:{port}\", connect_args={'configuration': {'set:hiveconf:async': 'false', 'set:hiveconf:hiveMetadata': 'true'}})\r\n\r\n # Connect to the created engine\r\n conn = engine.connect()\r\n\r\n # Use the connection to execute a query\r\n query = \"SHOW CONCEPTS\"\r\n results = conn.execute(query).fetchall()\r\n\r\n # Display the results of the execution\r\n print(results)\r\n```\r\n\r\n\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Timbr Python SQLAlchemy connector",
"version": "1.0.4",
"project_urls": {
"Bug Tracker": "https://github.com/WPSemantix/timbr_python_SQLAlchemy/issues",
"Download": "https://github.com/WPSemantix/timbr_python_SQLAlchemy/archive/refs/tags/v1.0.4.tar.gz",
"Homepage": "https://github.com/WPSemantix/timbr_python_SQLAlchemy"
},
"split_keywords": [
"timbr",
" timbr-python",
" timbr-connector",
" python-connector",
" pytimbr",
" pytimbr",
" py-timbr",
" py-timbr",
" pytimbr_sqla",
" pytimbr_sqla",
" pytimbr_sqla",
" pytimbr_sqla",
" pytimbr_sqla",
" pytimbr_sqla",
" pytimbr_sqla",
" pytimbr_sqla",
" pytimbr_sqla",
" pytimbrsqlalchemy",
" pytimbrsqlalchemy",
" pytimbrsqlalchemy",
" pytimbrsqlalchemy",
" pytimbrsqlalchemy",
" py-timbrsqlalchemy",
" py-timbrsqlalchemy",
" py-timbr-sqlalchemy",
" py-timbr-sqlalchemy"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0a56952205ee3bef80e02839d62158de563e0b10e255e10ef3a30a1f4aa7e859",
"md5": "03cdf83f296c6c2b88bae747ba21e542",
"sha256": "a136ddf0e2a7bb06d0f209b6fd068c4fc03571555da14a7c60e6b6fadd6c5696"
},
"downloads": -1,
"filename": "pytimbr_sqla-1.0.4.tar.gz",
"has_sig": false,
"md5_digest": "03cdf83f296c6c2b88bae747ba21e542",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 41179,
"upload_time": "2024-07-07T12:06:32",
"upload_time_iso_8601": "2024-07-07T12:06:32.083321Z",
"url": "https://files.pythonhosted.org/packages/0a/56/952205ee3bef80e02839d62158de563e0b10e255e10ef3a30a1f4aa7e859/pytimbr_sqla-1.0.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-07 12:06:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "WPSemantix",
"github_project": "timbr_python_SQLAlchemy",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "pytimbr-sqla"
}