-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathFakeExportJSONLResults.py
More file actions
41 lines (33 loc) · 1.85 KB
/
Copy pathFakeExportJSONLResults.py
File metadata and controls
41 lines (33 loc) · 1.85 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
# Copyright 2026 by Teradata Corporation. All rights reserved.
# TERADATA CORPORATION CONFIDENTIAL AND TRADE SECRET
# This sample program demonstrates fake multi-statement results written to JSONL files.
import json
import os
import teradatasql
def readJSONL (sFileName):
with open (sFileName, "rt", encoding="UTF8") as f:
return [json.loads (sLine) for sLine in f]
with teradatasql.connect (host="whomooz", user="guest", password="please") as con:
with con.cursor () as cur:
cur.execute ("create volatile table voltab (c1 integer, c2 varchar(100)) on commit preserve rows")
print ("Inserting data")
cur.execute ("insert into voltab values (?, ?)", [[1, "abc"], [2, None], [3, "xyz"]])
asFileNames = ["dataPy.jsonl", "dataPy_1.jsonl", "dataPy_2.jsonl", "dataPy_3.jsonl", "dataPy_4.jsonl", "dataPy_5.jsonl"]
print ("Exporting fake multi-statement results to files", asFileNames)
cur.execute ("{fn teradata_write_jsonl(" + asFileNames [0] + ")}{fn teradata_fake_result_sets}select * from voltab where c1 < 3 order by 1;select * from voltab where c1 >= 3 order by 1;select 123 as col1, 'abc' as col2")
try:
for sFileName in asFileNames:
print ("Reading file", sFileName)
rows = readJSONL (sFileName)
for row in rows:
for sColumnName in ("ColumnMetadata", "ParameterMetadata"):
if isinstance (row.get (sColumnName), str):
try:
row [sColumnName] = json.loads (row [sColumnName])
except ValueError:
pass
print (f"{sFileName}:")
print (json.dumps (rows, indent=2))
finally:
for sFileName in asFileNames:
os.remove (sFileName)