-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathtrino_example.py
More file actions
66 lines (52 loc) · 1.94 KB
/
Copy pathtrino_example.py
File metadata and controls
66 lines (52 loc) · 1.94 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
63
64
65
66
import trino
from trino.exceptions import TrinoQueryError
from testcontainers.community.trino import TrinoContainer
def basic_example():
with TrinoContainer() as trino_container:
# Get connection parameters
host = trino_container.get_container_host_ip()
port = trino_container.get_exposed_port(trino_container.port)
# Create Trino client
conn = trino.dbapi.connect(host=host, port=port, user="test", catalog="memory", schema="default")
cur = conn.cursor()
# Create a test table
try:
cur.execute("""
CREATE TABLE memory.default.test_table (
id BIGINT,
name VARCHAR,
value DOUBLE
)
""")
print("Created test table")
except TrinoQueryError as e:
print(f"Table might already exist: {e}")
# Insert test data
test_data = [(1, "test1", 100.0), (2, "test2", 200.0), (3, "test3", 300.0)]
for row in test_data:
cur.execute("INSERT INTO memory.default.test_table VALUES (%s, %s, %s)", row)
print("Inserted test data")
# Query data
cur.execute("SELECT * FROM memory.default.test_table ORDER BY id")
rows = cur.fetchall()
print("\nQuery results:")
for row in rows:
print(f"ID: {row[0]}, Name: {row[1]}, Value: {row[2]}")
# Execute a more complex query
cur.execute("""
SELECT
name,
AVG(value) as avg_value,
COUNT(*) as count
FROM memory.default.test_table
GROUP BY name
ORDER BY avg_value DESC
""")
print("\nAggregation results:")
for row in cur.fetchall():
print(f"Name: {row[0]}, Average Value: {row[1]}, Count: {row[2]}")
# Clean up
cur.close()
conn.close()
if __name__ == "__main__":
basic_example()