rigatoni-destinations 0.2.0

Destination implementations for Rigatoni CDC/Data Replication: S3 with multiple formats and compression
Documentation
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# Rigatoni S3 Destination

Production-ready AWS S3 destination for the Rigatoni ETL framework. Supports multiple serialization formats, compression options, and flexible partitioning strategies.

## Features

- **Multiple Formats**: JSON, CSV, Parquet, Avro
-**Compression**: Gzip, Zstandard
-**Flexible Partitioning**: Hive-style, date-based, collection-based
-**Automatic Retry**: Exponential backoff with configurable retries
-**S3-Compatible**: Works with AWS S3, MinIO, LocalStack
-**Production Ready**: Comprehensive error handling and logging

## Quick Start

### Basic Usage

```rust
use rigatoni_core::Destination;
use rigatoni_destinations::s3::{S3Config, S3Destination};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = S3Config::builder()
        .bucket("my-data-lake")
        .region("us-east-1")
        .prefix("mongodb/events")
        .build()?;

    let mut destination = S3Destination::new(config).await?;

    // Write events
    // destination.write_batch(events).await?;

    destination.close().await?;
    Ok(())
}
```

### With Compression and Partitioning

```rust
use rigatoni_destinations::s3::{
    S3Config, S3Destination, Compression, KeyGenerationStrategy
};

let config = S3Config::builder()
    .bucket("analytics-data")
    .region("us-west-2")
    .prefix("events")
    .compression(Compression::Zstd)
    .key_strategy(KeyGenerationStrategy::HivePartitioned)
    .build()?;

let destination = S3Destination::new(config).await?;
```

## Serialization Formats

### JSON (Default)

Newline-delimited JSON (JSONL) - one JSON object per line.

**Best for**:
- Human readability
- S3 Select queries
- Mixed schemas

**Enable**: `--features json` (included in default)

```rust
use rigatoni_destinations::s3::SerializationFormat;

S3Config::builder()
    .format(SerializationFormat::Json)
    // ...
```

### CSV

Comma-separated values with JSON-encoded nested documents.

**Best for**:
- Excel compatibility
- Simple data
- Quick analysis

**Enable**: `--features csv`

```rust
S3Config::builder()
    .format(SerializationFormat::Csv)
    // ...
```

### Parquet

Apache Parquet columnar format.

**Best for**:
- Analytics workloads
- Excellent compression
- Fast queries

**Enable**: `--features parquet`

```rust
S3Config::builder()
    .format(SerializationFormat::Parquet)
    // ...
```

### Avro

Apache Avro binary format with schema evolution support.

**Best for**:
- Schema evolution
- Streaming
- Kafka integration

**Enable**: `--features avro`

```rust
S3Config::builder()
    .format(SerializationFormat::Avro)
    // ...
```

## Compression

### Gzip

Standard compression with wide compatibility.

**Stats**: ~70-80% compression ratio, moderate speed

**Enable**: `--features gzip`

```rust
use rigatoni_destinations::s3::Compression;

S3Config::builder()
    .compression(Compression::Gzip)
    // ...
```

### Zstandard

Modern compression with better ratio and speed than gzip.

**Stats**: ~75-85% compression ratio, faster than gzip

**Enable**: `--features zstandard`

```rust
S3Config::builder()
    .compression(Compression::Zstd)
    // ...
```

## Key Generation Strategies

### Hive Partitioned

Hive-style partitioning for analytics platforms.

**Pattern**: `{prefix}/collection={name}/year={YYYY}/month={MM}/day={DD}/hour={HH}/{timestamp}.{ext}`

**Example**: `events/collection=users/year=2025/month=01/day=15/hour=10/1705318800000.jsonl`

```rust
use rigatoni_destinations::s3::KeyGenerationStrategy;

S3Config::builder()
    .key_strategy(KeyGenerationStrategy::HivePartitioned)
    // ...
```

**Best for**: Athena, Presto, Spark (automatic partition discovery)

### Date Hour Partitioned (Default)

Simple time-based partitioning with hour granularity.

**Pattern**: `{prefix}/{collection}/{YYYY}/{MM}/{DD}/{HH}/{timestamp}.{ext}`

**Example**: `events/users/2025/01/15/10/1705318800000.jsonl`

```rust
S3Config::builder()
    .key_strategy(KeyGenerationStrategy::DateHourPartitioned)
    // ...
```

**Best for**: Time-range queries, lifecycle policies, human-readable structure

### Date Partitioned

Daily partitioning without hour granularity.

**Pattern**: `{prefix}/{collection}/{YYYY}/{MM}/{DD}/{timestamp}.{ext}`

**Best for**: Daily aggregations, lower partition count

### Collection Based

Simple grouping by collection.

**Pattern**: `{prefix}/{collection}/{timestamp}.{ext}`

**Best for**: Collection-specific access, minimal partitioning

### Flat

Flat structure with timestamp.

**Pattern**: `{prefix}/{collection}_{timestamp}.{ext}`

**Best for**: Simple backups, testing

## Configuration Options

```rust
S3Config::builder()
    .bucket("my-bucket")              // Required
    .region("us-east-1")              // Required
    .prefix("path/to/data")           // Optional prefix
    .format(SerializationFormat::Json) // Default: JSON
    .compression(Compression::Zstd)    // Default: None
    .key_strategy(KeyGenerationStrategy::HivePartitioned)
    .max_retries(5)                   // Default: 3
    .endpoint_url("http://localhost:4566") // For LocalStack/MinIO
    .force_path_style(true)           // For LocalStack/MinIO
    .build()?
```

## Examples

Run the examples to see the S3 destination in action:

### Basic Example

```bash
export S3_BUCKET="your-bucket-name"
cargo run --example s3_basic --features s3,json
```

### With Compression

```bash
# Gzip compression
cargo run --example s3_with_compression --features s3,json,gzip

# Zstd compression (better)
cargo run --example s3_with_compression --features s3,json,zstandard
```

### Advanced Features

```bash
# All features
cargo run --example s3_advanced --all-features

# Specific formats
cargo run --example s3_advanced --features s3,csv,gzip
cargo run --example s3_advanced --features s3,parquet,zstandard
cargo run --example s3_advanced --features s3,avro
```

## Testing

### Unit Tests

```bash
cargo test --package rigatoni-destinations --features s3,json,gzip,zstandard --lib
```

### Integration Tests with LocalStack

Start LocalStack:

```bash
cd rigatoni-destinations
docker-compose up -d
```

Wait for LocalStack to be healthy:

```bash
docker-compose logs -f
```

Run integration tests:

```bash
# All integration tests
cargo test --package rigatoni-destinations --test s3_integration_test --all-features -- --ignored

# Specific tests
cargo test --test s3_integration_test test_s3_basic_write --features s3,json -- --ignored
cargo test --test s3_integration_test test_s3_with_gzip_compression --features s3,json,gzip -- --ignored
```

Stop LocalStack:

```bash
docker-compose down
```

## Analytics Integration

### AWS Athena

```sql
-- Create external table with Hive partitioning
CREATE EXTERNAL TABLE mongodb_events (
  operation STRING,
  database STRING,
  collection STRING,
  cluster_time TIMESTAMP,
  full_document STRING
)
PARTITIONED BY (
  collection_name STRING,
  year INT,
  month INT,
  day INT,
  hour INT
)
STORED AS PARQUET
LOCATION 's3://your-bucket/analytics/mongodb-cdc/';

-- Discover partitions
MSCK REPAIR TABLE mongodb_events;

-- Query with partition pruning
SELECT * FROM mongodb_events
WHERE collection_name = 'users'
  AND year = 2025
  AND month = 1
  AND day = 15;
```

### Apache Spark

```python
# Read with partition discovery
df = spark.read.parquet("s3://your-bucket/analytics/mongodb-cdc/")

# Query with partition pruning
users_df = df.filter(
    (df.collection_name == "users") &
    (df.year == 2025) &
    (df.month == 1)
)
```

## Performance Tips

1. **Batching**: Buffer events and write in larger batches (1000-10000 events)
2. **Compression**: Use Zstd for best compression ratio and speed
3. **Partitioning**: Use Hive partitioning for analytics workloads
4. **Format**:
   - JSON for flexibility and debugging
   - Parquet for analytics and compression
   - CSV for Excel/simple analysis
   - Avro for schema evolution
5. **Retries**: Increase `max_retries` for production (5-10)

## Troubleshooting

### Access Denied

Ensure your AWS credentials have S3 write permissions:

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:PutObject",
        "s3:PutObjectAcl"
      ],
      "Resource": "arn:aws:s3:::your-bucket/*"
    }
  ]
}
```

### LocalStack Connection Refused

```bash
# Check LocalStack is running
docker-compose ps

# View LocalStack logs
docker-compose logs -f localstack

# Restart LocalStack
docker-compose restart localstack
```

### Path Style Addressing

For LocalStack and MinIO, you must enable path-style addressing:

```rust
S3Config::builder()
    .force_path_style(true)
    // ...
```

## License

Licensed under the Apache License 2.0