-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexample.py
More file actions
62 lines (51 loc) · 2.1 KB
/
Copy pathexample.py
File metadata and controls
62 lines (51 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from sqlalchemy.engine import create_engine
if __name__ == '__main__':
# 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.
# HTTP example
hostname = 'mytimbrenv.com'
port = '11000'
ontology = 'my_ontology'
protocol = 'http'
username = 'timbr'
password = 'StrongPassword'
# HTTPS example
hostname = 'mytimbrenv.com'
port = '443'
ontology = 'my_ontology'
protocol = 'https'
username = 'timbr'
password = 'StrongPassword'
# 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"
res_obj = conn.execute(query)
results_headers = [(desc[0], desc[1]) for desc in res_obj.cursor.description]
# Optional Performance Tuning:
# By default, Timbr fetches results in batches of 10,000 rows at a time.
# You can increase this batch size for better performance with large datasets.
# Note: The maximum batch size is controlled by your Timbr server's
# "TIMBR_RESULTSET_MAX_FETCH_SIZE" configuration setting.
res_obj.cursor._arraysize = 20000
results = res_obj.fetchall()
# Print the columns name
for name, col_type in results_headers:
print(f"{name} - {col_type}")
# Print the results
for result in results:
print(result)