-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpfc_export_questdb.py
More file actions
299 lines (247 loc) · 10.8 KB
/
pfc_export_questdb.py
File metadata and controls
299 lines (247 loc) · 10.8 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
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
#!/usr/bin/env python3
"""
pfc-export-questdb — Export QuestDB tables to PFC cold-storage archives.
Streams rows from a QuestDB table directly into a compressed .pfc archive
with block-level timestamp index — ready for time-range queries via DuckDB
or pfc-gateway without loading the full archive.
Note: QuestDB has no schema concept — tables are referenced by name only.
Usage:
pfc-export-questdb --host quest.example.com --table trades --output trades.pfc
pfc-export-questdb --host quest.example.com --table sensor_data \\
--ts-column timestamp \\
--from-ts "2024-01-01T00:00:00" \\
--to-ts "2024-02-01T00:00:00" \\
--output sensor_jan2024.pfc --verbose
Requires: pip install psycopg2-binary
"""
__version__ = "0.1.0"
import argparse
import json
import os
import shutil
import subprocess
import sys
import tempfile
from datetime import datetime
from pathlib import Path
# ---------------------------------------------------------------------------
# Binary detection
# ---------------------------------------------------------------------------
def find_pfc_binary(override=None):
if override:
if os.path.isfile(override) and os.access(override, os.X_OK):
return override
raise FileNotFoundError(f"pfc_jsonl binary not found at: {override}")
env = os.environ.get("PFC_JSONL_BINARY")
if env and os.path.isfile(env) and os.access(env, os.X_OK):
return env
default = "/usr/local/bin/pfc_jsonl"
if os.path.isfile(default) and os.access(default, os.X_OK):
return default
found = shutil.which("pfc_jsonl")
if found:
return found
return None
# ---------------------------------------------------------------------------
# Core export
# ---------------------------------------------------------------------------
def export_to_pfc(
host: str,
port: int,
user: str,
password: str,
dbname: str,
table: str,
output_path: Path,
pfc_binary: str,
ts_column: str = None,
from_ts: str = None,
to_ts: str = None,
batch_size: int = 10_000,
verbose: bool = False,
) -> dict:
"""
Stream rows from QuestDB into a PFC archive.
Flow: QuestDB → fetchmany() loop → JSONL temp file → pfc_jsonl compress → .pfc + .pfc.bidx
Returns dict: rows, jsonl_mb, output_mb, ratio_pct, output
"""
try:
import psycopg2
except ImportError:
raise ImportError(
"psycopg2 is required.\n"
"Install: pip install psycopg2-binary"
)
output_path = Path(output_path)
output_path.parent.mkdir(parents=True, exist_ok=True)
# QuestDB has no schema — table referenced directly
full_table = f'"{table}"'
conditions, params = [], []
if ts_column and from_ts:
conditions.append(f'"{ts_column}" >= %s')
params.append(from_ts)
if ts_column and to_ts:
conditions.append(f'"{ts_column}" < %s')
params.append(to_ts)
where_clause = ("WHERE " + " AND ".join(conditions)) if conditions else ""
order_clause = (f'ORDER BY "{ts_column}"') if ts_column else ""
query = f"SELECT * FROM {full_table} {where_clause} {order_clause}".strip()
if verbose:
print(f" → Connecting to QuestDB at {host}:{port} (db: {dbname}) ...")
print(f" → Query: {query[:120]}{'...' if len(query) > 120 else ''}")
conn = psycopg2.connect(
host=host, port=port, user=user, password=password,
dbname=dbname, connect_timeout=30,
)
conn.autocommit = True
tmp_fd, tmp_jsonl = tempfile.mkstemp(suffix=".jsonl", prefix="pfc_questdb_")
os.close(tmp_fd)
row_count = jsonl_bytes = 0
try:
cur = conn.cursor()
cur.execute(query, params or None)
col_names = [desc[0] for desc in cur.description]
if verbose:
print(f" → Columns ({len(col_names)}): {', '.join(col_names[:8])}"
f"{'...' if len(col_names) > 8 else ''}")
print(f" → Streaming rows (batch: {batch_size:,}) ...")
with open(tmp_jsonl, "w", encoding="utf-8") as fout:
while True:
batch = cur.fetchmany(batch_size)
if not batch:
break
for raw_row in batch:
row = {}
for col, val in zip(col_names, raw_row):
if isinstance(val, datetime):
val = val.isoformat()
elif isinstance(val, bytes):
val = val.hex()
row[col] = val
# Ensure pfc_jsonl timestamp index works regardless of column name
if ts_column and ts_column in row and "timestamp" not in row:
row["timestamp"] = row[ts_column]
line = json.dumps(row, ensure_ascii=False) + "\n"
fout.write(line)
jsonl_bytes += len(line.encode("utf-8"))
row_count += 1
if verbose and row_count % 100_000 == 0:
print(f" {row_count:,} rows ({jsonl_bytes / 1_048_576:.1f} MiB) ...")
cur.close()
if verbose:
print(f" → Exported {row_count:,} rows ({jsonl_bytes / 1_048_576:.1f} MiB JSONL)")
if row_count == 0:
if verbose:
print(" → 0 rows in result — no .pfc written.")
return {"rows": 0, "jsonl_mb": 0, "output_mb": 0, "ratio_pct": 0, "output": str(output_path)}
if verbose:
print(f" → Compressing ...")
proc = subprocess.run(
[pfc_binary, "compress", tmp_jsonl, str(output_path)],
capture_output=True, text=True,
)
if proc.returncode != 0:
raise RuntimeError(
f"pfc_jsonl compress failed (exit {proc.returncode}):\n{proc.stderr.strip()}"
)
jsonl_mb = jsonl_bytes / 1_048_576
output_mb = output_path.stat().st_size / 1_048_576
ratio_pct = (output_mb / jsonl_mb * 100) if jsonl_mb > 0 else 0.0
if verbose:
print(
f" ✓ {row_count:,} rows | "
f"JSONL {jsonl_mb:.1f} MiB → PFC {output_mb:.1f} MiB "
f"({ratio_pct:.1f}%) → {output_path.name}"
)
return {
"rows": row_count,
"jsonl_mb": jsonl_mb,
"output_mb": output_mb,
"ratio_pct": ratio_pct,
"output": str(output_path),
}
except Exception:
conn.close()
raise
finally:
if os.path.exists(tmp_jsonl):
os.unlink(tmp_jsonl)
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def build_parser():
parser = argparse.ArgumentParser(
prog="pfc-export-questdb",
description="Export QuestDB tables to PFC cold-storage archives.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
examples:
pfc-export-questdb --host quest.example.com --table trades --output trades.pfc
pfc-export-questdb --host quest.example.com --table sensor_data \\
--ts-column timestamp --from-ts "2024-01-01T00:00:00" --to-ts "2024-02-01T00:00:00" \\
--output sensor_jan2024.pfc --verbose
Install pfc_jsonl binary first:
curl -L https://github.com/ImpossibleForge/pfc-jsonl/releases/latest/download/pfc_jsonl-linux-x64 \\
-o /usr/local/bin/pfc_jsonl && chmod +x /usr/local/bin/pfc_jsonl
""",
)
parser.add_argument("--version", action="version", version=f"pfc-export-questdb {__version__}")
parser.add_argument("--host", required=True, help="QuestDB hostname or IP")
parser.add_argument("--port", type=int, default=8812, help="PostgreSQL wire port (default: 8812)")
parser.add_argument("--user", default="admin", help="Username (default: admin)")
parser.add_argument("--password", default="quest", help="Password (default: quest)")
parser.add_argument("--dbname", default="qdb", help="Database name (default: qdb)")
parser.add_argument("--table", required=True, help="Table name to export")
parser.add_argument("--ts-column", default=None, metavar="COL",
help="Timestamp column for time-range filtering and ORDER BY")
parser.add_argument("--from-ts", default=None, metavar="ISO_DATETIME",
help="Start of time range (ISO 8601, inclusive)")
parser.add_argument("--to-ts", default=None, metavar="ISO_DATETIME",
help="End of time range (ISO 8601, exclusive)")
parser.add_argument("--output", default=None, metavar="FILE",
help="Output .pfc file (default: {table}.pfc or {table}_{from}_{to}.pfc)")
parser.add_argument("--batch-size", type=int, default=10_000, metavar="N",
help="Rows per fetch batch (default: 10,000)")
parser.add_argument("--verbose", "-v", action="store_true")
parser.add_argument("--pfc-binary", default=None, metavar="PATH",
help="Path to pfc_jsonl binary (default: auto-detect)")
return parser
def main():
parser = build_parser()
args = parser.parse_args()
try:
pfc_binary = find_pfc_binary(args.pfc_binary)
except FileNotFoundError as exc:
print(f"ERROR: {exc}", file=sys.stderr)
sys.exit(1)
if not pfc_binary:
print("ERROR: pfc_jsonl binary not found. Install from "
"https://github.com/ImpossibleForge/pfc-jsonl/releases", file=sys.stderr)
sys.exit(1)
if args.output:
output_path = Path(args.output)
else:
parts = [args.table]
if args.from_ts:
parts.append(args.from_ts.replace(":", "").replace("-", "").replace(" ", "T")[:15])
if args.to_ts:
parts.append(args.to_ts.replace(":", "").replace("-", "").replace(" ", "T")[:15])
output_path = Path("_".join(parts) + ".pfc")
try:
stats = export_to_pfc(
host=args.host, port=args.port, user=args.user,
password=args.password, dbname=args.dbname,
table=args.table, output_path=output_path, pfc_binary=pfc_binary,
ts_column=args.ts_column, from_ts=args.from_ts, to_ts=args.to_ts,
batch_size=args.batch_size, verbose=args.verbose,
)
if not args.verbose:
if stats['rows'] == 0:
print(f"Done: 0 rows in result — no .pfc written.")
else:
print(f"Done: {stats['rows']:,} rows → {stats['output']} ({stats['ratio_pct']:.1f}% of JSONL)")
except Exception as exc:
print(f"ERROR: {exc}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()