-
Notifications
You must be signed in to change notification settings - Fork 5
/
pyhive_sync_example.py
70 lines (61 loc) · 2.36 KB
/
pyhive_sync_example.py
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
63
64
65
66
67
68
69
70
from sqlalchemy.engine import create_engine
from TCLIService.ttypes import TOperationState
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>'
connect_args = {
'configuration': {
'set:hiveconf:async': 'false',
'set:hiveconf:hiveMetadata': 'true',
'set:hiveconf:active_datasource': '<datasource_name>',
'set:hiveconf:queryTimeout': '<TIMEOUT_IN_SECONDS>',
},
}
# 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.
# connect_args - The connection special arguments for extra customization. The only 2 arguments you must have are the first and the second one (set:hiveconf:async, set:hiveconf:hiveMetadata) the others are optional.
# HTTP example
hostname = 'mytimbrenv.com'
port = '11000'
ontology = 'my_ontology'
protocol = 'http'
username = 'timbr'
password = 'StrongPassword'
connect_args = {
'configuration': {
'set:hiveconf:async': 'false',
'set:hiveconf:hiveMetadata': 'true',
},
}
# HTTPS example
hostname = 'mytimbrenv.com'
port = '443'
ontology = 'my_ontology'
protocol = 'https'
username = 'timbr'
password = 'StrongPassword'
connect_args = {
'configuration': {
'set:hiveconf:async': 'false',
'set:hiveconf:hiveMetadata': 'true',
},
}
# Create new sqlalchemy connection
engine = create_engine(f"hive+{protocol}://{username}@{ontology}:{password}@{hostname}:{port}", connect_args = connect_args)
# 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)