Skip to content

Commit 75ea79c

Browse files
authored
Examples (#3)
1 parent 1c4a51b commit 75ea79c

16 files changed

+2474
-294
lines changed

Makefile

Lines changed: 246 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,50 @@ We welcome contributions! Please see:
287287
- [FastAPI Integration](examples/fastapi_app/README.md) - Complete REST API example
288288
- [More Examples](examples/) - Additional usage patterns
289289

290+
## 🎯 Running the Examples
291+
292+
The project includes comprehensive examples demonstrating various features and use cases. Each example can be run using the provided Makefile, which automatically handles Cassandra setup if needed.
293+
294+
### Available Examples
295+
296+
Run any example with: `make example-<name>`
297+
298+
- **`make example-basic`** - Basic connection and query execution
299+
- **`make example-streaming`** - Memory-efficient streaming of large result sets with True Async Paging
300+
- **`make example-context-safety`** - Demonstrates proper context manager usage and resource isolation
301+
- **`make example-export-large-table`** - Export large tables to CSV with progress tracking
302+
- **`make example-export-parquet`** - Export data to Parquet format with complex data types
303+
- **`make example-metrics`** - Comprehensive metrics collection and performance monitoring
304+
- **`make example-metrics-simple`** - Basic metrics collection example
305+
- **`make example-realtime`** - Real-time data processing with sliding window analytics
306+
- **`make example-streaming-demo`** - Visual demonstration that streaming doesn't block the event loop
307+
308+
### Running with External Cassandra
309+
310+
If you have Cassandra running elsewhere:
311+
312+
```bash
313+
# Single node
314+
CASSANDRA_CONTACT_POINTS=10.0.0.1 make example-streaming
315+
316+
# Multiple nodes
317+
CASSANDRA_CONTACT_POINTS=10.0.0.1,10.0.0.2,10.0.0.3 make example-streaming
318+
319+
# With custom port
320+
CASSANDRA_CONTACT_POINTS=cassandra.example.com CASSANDRA_PORT=9043 make example-basic
321+
```
322+
323+
### Example Descriptions
324+
325+
- **Basic Example**: Shows fundamental operations like connecting, executing queries, and using prepared statements
326+
- **Streaming Examples**: Demonstrate True Async Paging for processing millions of rows without memory issues
327+
- **Export Examples**: Show how to export Cassandra data to various formats (CSV, Parquet) with progress tracking
328+
- **Metrics Examples**: Illustrate performance monitoring, query tracking, and connection health checking
329+
- **Real-time Processing**: Demonstrates processing time-series IoT data with concurrent operations
330+
- **Context Safety Demo**: Proves that errors in one operation don't affect others when using context managers
331+
332+
Each example includes detailed comments explaining the concepts and best practices. Start with `example-basic` if you're new to the library.
333+
290334
## ⚡ Performance
291335

292336
async-cassandra enables your async Python application to work with Cassandra without blocking the event loop. While it doesn't eliminate the underlying driver's thread pool, it prevents those blocking operations from freezing your entire application. This is crucial for web servers where a blocked event loop means no requests can be processed.

examples/README.md

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,48 @@
22

33
This directory contains working examples demonstrating various features and use cases of async-cassandra.
44

5+
## Quick Start
6+
7+
### Running Examples with Make
8+
9+
The easiest way to run examples is using the provided Make targets:
10+
11+
```bash
12+
# Run a specific example (automatically starts Cassandra if needed)
13+
make example-streaming
14+
make example-export-csv
15+
make example-export-parquet
16+
make example-realtime
17+
make example-metrics
18+
make example-non-blocking
19+
make example-context
20+
21+
# Run all examples in sequence
22+
make examples-all
23+
24+
# Use external Cassandra cluster
25+
CASSANDRA_CONTACT_POINTS=node1.example.com,node2.example.com make example-streaming
26+
```
27+
28+
### Installing Example Dependencies
29+
30+
Some examples require additional dependencies:
31+
32+
```bash
33+
# Install all example dependencies (including pyarrow for Parquet export)
34+
make install-examples
35+
36+
# Or manually
37+
pip install -r examples/requirements.txt
38+
```
39+
40+
### Environment Variables
41+
42+
All examples support these environment variables:
43+
- `CASSANDRA_CONTACT_POINTS`: Comma-separated list of contact points (default: localhost)
44+
- `CASSANDRA_PORT`: Port number (default: 9042)
45+
- `EXAMPLE_OUTPUT_DIR`: Directory for output files like CSV and Parquet exports (default: examples/exampleoutput)
46+
547
## Available Examples
648

749
### 1. [FastAPI Integration](fastapi_app/)
@@ -50,10 +92,38 @@ Shows how to export large Cassandra tables to CSV:
5092
**Run:**
5193
```bash
5294
python export_large_table.py
53-
# Exports will be saved in ./exports/ directory
95+
# Exports will be saved in examples/exampleoutput/ directory (default)
96+
97+
# Or with custom output directory:
98+
EXAMPLE_OUTPUT_DIR=/tmp/my-exports python export_large_table.py
5499
```
55100

56-
### 4. [Real-time Data Processing](realtime_processing.py)
101+
### 4. [Export to Parquet Format](export_to_parquet.py)
102+
103+
Advanced example of exporting large Cassandra tables to Parquet format:
104+
- Memory-efficient streaming with page-by-page processing
105+
- Automatic schema inference from Cassandra data types
106+
- Multiple compression options (snappy, gzip, lz4)
107+
- Progress tracking during export
108+
- Handles all Cassandra data types including collections
109+
- Configurable row group sizes for optimization
110+
- Export statistics and performance metrics
111+
112+
**Run:**
113+
```bash
114+
python export_to_parquet.py
115+
# Exports will be saved in examples/exampleoutput/ directory (default)
116+
117+
# Or with custom output directory:
118+
EXAMPLE_OUTPUT_DIR=/tmp/my-parquet-exports python export_to_parquet.py
119+
```
120+
121+
**Note:** Requires PyArrow to be installed:
122+
```bash
123+
pip install pyarrow
124+
```
125+
126+
### 5. [Real-time Data Processing](realtime_processing.py)
57127

58128
Example of processing time-series data in real-time:
59129
- Sliding window analytics
@@ -67,7 +137,7 @@ Example of processing time-series data in real-time:
67137
python realtime_processing.py
68138
```
69139

70-
### 5. [Metrics Collection](metrics_simple.py)
140+
### 6. [Metrics Collection](metrics_simple.py)
71141

72142
Simple example of metrics collection:
73143
- Query performance tracking
@@ -80,7 +150,7 @@ Simple example of metrics collection:
80150
python metrics_simple.py
81151
```
82152

83-
### 6. [Advanced Metrics](metrics_example.py)
153+
### 7. [Advanced Metrics](metrics_example.py)
84154

85155
Comprehensive metrics and observability example:
86156
- Multiple metrics collectors setup
@@ -94,7 +164,21 @@ Comprehensive metrics and observability example:
94164
python metrics_example.py
95165
```
96166

97-
### 7. [Context Manager Safety](context_manager_safety_demo.py)
167+
### 8. [Non-Blocking Streaming Demo](streaming_non_blocking_demo.py)
168+
169+
Visual demonstration that streaming doesn't block the event loop:
170+
- Heartbeat monitoring to detect event loop blocking
171+
- Concurrent queries during streaming
172+
- Visual feedback showing event loop responsiveness
173+
- Performance analysis of concurrent operations
174+
- Proves the async wrapper truly keeps the event loop free
175+
176+
**Run:**
177+
```bash
178+
python streaming_non_blocking_demo.py
179+
```
180+
181+
### 9. [Context Manager Safety](context_manager_safety_demo.py)
98182

99183
Demonstrates proper context manager usage:
100184
- Context manager isolation
@@ -119,6 +203,19 @@ Production-ready monitoring configurations:
119203
- Connection health status
120204
- Error rates and trends
121205

206+
## Output Files
207+
208+
Examples that generate output files (CSV exports, Parquet exports, etc.) save them to a configurable directory:
209+
210+
- **Default location**: `examples/exampleoutput/`
211+
- **Configure via environment variable**: `EXAMPLE_OUTPUT_DIR=/path/to/output`
212+
- **Git ignored**: All files in the default output directory are ignored by Git (except README.md and .gitignore)
213+
- **Cleanup**: Files are not automatically deleted; clean up manually when needed:
214+
```bash
215+
rm -f examples/exampleoutput/*.csv
216+
rm -f examples/exampleoutput/*.parquet
217+
```
218+
122219
## Prerequisites
123220

124221
All examples require:

0 commit comments

Comments
 (0)