Skip to content

Commit edefbd3

Browse files
authored
Update README.md
1 parent 7fd464f commit edefbd3

File tree

1 file changed

+76
-32
lines changed

1 file changed

+76
-32
lines changed

README.md

Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# QuestDB Python Library
22

3-
A robust, strongly-typed Python library for interacting with QuestDB, providing an easy-to-use interface for querying, writing data, and managing logs.
3+
A robust, strongly-typed Python library for interacting with QuestDB, providing an easy-to-use interface for querying and writing data, with additional features such as easy integration with the built in python logging library automatically saving all logs, including tracebacks and stack traces to DB. Easy set-up with docker-compose and a grafana dashboard is included.
44

5-
## Features
5+
## Key Features
66

77
- Asynchronous and synchronous query support
8-
- Strongly-typed data structures
8+
- Strongly-typed data structures using `msgspec`
99
- Easy data insertion with automatic flushing
1010
- Pandas DataFrame integration
11-
- Built-in logging handler for QuestDB
11+
- Seamless logging integration with Python's built-in logging system
12+
- Docker Compose setup for QuestDB with Prometheus and Grafana monitoring
1213
- Comprehensive test coverage
14+
- Optional error handling callback
1315

1416
## Installation
1517

@@ -23,6 +25,7 @@ pip install py-questdb
2325

2426
```python
2527
from py_questdb import QuestDB
28+
from datetime import datetime
2629

2730
# Initialize QuestDB client
2831
db = QuestDB(host="localhost", port=9000)
@@ -62,11 +65,43 @@ db = QuestDB()
6265
db = QuestDB(host="questdb.example.com", port=9000, username="user", password="pass")
6366
```
6467

68+
### Querying Data
69+
70+
```python
71+
# Synchronous query
72+
for row in db.query_sync("SELECT * FROM sensors WHERE location = 'NYC'"):
73+
print(f"Temperature: {row['temperature']}, Humidity: {row['humidity']}")
74+
75+
# Asynchronous query
76+
async for row in db.query("SELECT * FROM sensors WHERE location = 'LA'"):
77+
print(f"Temperature: {row['temperature']}, Humidity: {row['humidity']}")
78+
79+
# Query into Pandas DataFrame
80+
df = db.query_df_sync("SELECT * FROM sensors")
81+
print(df.describe())
82+
83+
# Query with type hinting
84+
class SensorReading(msgspec.Struct):
85+
location: str
86+
temperature: float
87+
humidity: int
88+
89+
async for reading in db.query("SELECT * FROM sensors", into_type=SensorReading):
90+
print(f"Location: {reading.location}, Temp: {reading.temperature}°C, Humidity: {reading.humidity}%")
91+
92+
# Query with error handling
93+
def error_handler(response: bytes):
94+
print(f"Error in query: {response.decode()}")
95+
96+
results = db.query_sync("SELECT * FROM non_existent_table", error_handler=error_handler)
97+
```
98+
6599
### Writing Data
66100

67101
```python
68102
from datetime import datetime
69103
from py_questdb.db_types import QuestDBFields
104+
import msgspec
70105

71106
# Single write with auto-flush
72107
db.write({
@@ -106,51 +141,60 @@ sensor_data = SensorData(location="SF", temperature=22.5, humidity=65, timestamp
106141
db.write(sensor_data)
107142
```
108143

109-
### Querying Data
110-
111-
```python
112-
# Synchronous query
113-
for row in db.query_sync("SELECT * FROM sensors WHERE location = 'NYC'"):
114-
print(f"Temperature: {row['temperature']}, Humidity: {row['humidity']}")
115-
116-
# Asynchronous query
117-
async for row in db.query("SELECT * FROM sensors WHERE location = 'LA'"):
118-
print(f"Temperature: {row['temperature']}, Humidity: {row['humidity']}")
144+
### Logging Integration
119145

120-
# Query into Pandas DataFrame
121-
df = db.query_df_sync("SELECT * FROM sensors")
122-
print(df.describe())
123-
124-
# Query with type hinting
125-
class SensorReading(msgspec.Struct):
126-
location: str
127-
temperature: float
128-
humidity: int
129-
130-
async for reading in db.query("SELECT * FROM sensors", into_type=SensorReading):
131-
print(f"Location: {reading.location}, Temp: {reading.temperature}°C, Humidity: {reading.humidity}%")
132-
```
133-
134-
### Logging
146+
The library provides seamless integration with Python's built-in logging system, allowing you to easily write logs directly to QuestDB:
135147

136148
```python
137149
import logging
138150
from py_questdb import QuestDB
139151
from py_questdb.log_handler import QuestDBLogHandler
140152

141-
# Set up QuestDB logging
153+
# Initialize QuestDB client
142154
questdb_client = QuestDB()
155+
156+
# Create QuestDB log handler
143157
handler = QuestDBLogHandler(questdb_client=questdb_client)
158+
159+
# Get logger and add handler
144160
logger = logging.getLogger()
145161
logger.addHandler(handler)
146162
logger.setLevel(logging.INFO)
147163

148-
# Log messages
164+
# Now all your logs will be written to QuestDB
149165
logger.info("System started")
150166
logger.warning("Low disk space")
151167
logger.error("Connection failed", exc_info=True)
168+
169+
# You can log exceptions with full stack traces
170+
try:
171+
1 / 0
172+
except Exception as e:
173+
logger.exception("An error occurred")
174+
175+
# The logs are written to QuestDB in real-time, allowing for immediate analysis and monitoring
176+
```
177+
178+
This integration allows you to leverage QuestDB's powerful querying capabilities for log analysis, making it easy to search, filter, and analyze your application logs.
179+
180+
## Docker Compose Setup
181+
182+
The project includes a Docker Compose configuration to run QuestDB along with Prometheus for metrics collection and Grafana for visualization. To use this setup:
183+
184+
1. Navigate to the `/etc` directory in the project.
185+
2. Run the following command:
186+
187+
```bash
188+
docker-compose up -d
152189
```
153190

191+
This will start QuestDB, Prometheus, and Grafana containers. You can access:
192+
- QuestDB Web Console: http://localhost:9000
193+
- Prometheus: http://localhost:9090
194+
- Grafana: http://localhost:3000 (default credentials: admin/admin)
195+
196+
> A pre-configured Grafana dashboard for QuestDB metrics is included in the `questdb_dashboard.json` file, allowing for immediate visualization of your QuestDB instance's performance.
197+
154198
## Testing
155199

156200
Run the test suite using pytest:
@@ -165,4 +209,4 @@ Contributions are welcome! Please feel free to submit a Pull Request.
165209

166210
## License
167211

168-
This project is licensed under the [MIT License](LICENSE).
212+
This project is licensed under the [MIT License](LICENSE).

0 commit comments

Comments
 (0)