1- # Write GarfReport
1+ # Writing GarfReport
22
3- ` garf-io ` handles reading queries and writing ` GarfReport ` to various local/remote storages.
3+ [ ![ PyPI] ( https://img.shields.io/pypi/v/garf-io?logo=pypi&logoColor=white&style=flat-square )] ( https://pypi.org/project/garf-io )
4+ [ ![ Downloads PyPI] ( https://img.shields.io/pypi/dw/garf-io?logo=pypi )] ( https://pypi.org/project/garf-io/ )
5+
6+ ` garf-io ` library is reponsible for writing [ ` GarfReport ` ] ( reports.md ) to various local/remote storages.
47
5- Currently it supports writing data to the following destination:
68
79| CLI identifier | Writer Class | Options |
810| ------------| ---------------- | -------- |
@@ -13,10 +15,6 @@ Currently it supports writing data to the following destination:
1315| ` sqldb ` | SqlAlchemyWriter | ` connection-string ` , `if-exists=fail| replace| append` |
1416| ` sheets ` | SheetsWriter | ` share-with ` , ` credentials-file ` , ` spreadsheet-url ` , `is_append=True| False`|
1517
16- Each of writer also support two options for dealing with arrays:
17-
18- * ` WRITER.array-handling ` - arrays handling method: "strings" (default) - store arrays as strings (items combined via a separator, e.g. "item1|item2"), "arrays" - store arrays as arrays.
19- * ` WRITER.array-separator ` - a separator symbol for joining arrays as strings, by default '|'.
2018
2119## Installation
2220
@@ -97,6 +95,60 @@ writer.write(sample_report, 'query')
9795```
9896///
9997
98+ #### Format
99+
100+ For ` console ` writer you can specify the output format:
101+
102+ * ` table ` - rich table (default).
103+ * ` json ` - JSON.
104+ * ` jsonl ` - JSON lines
105+
106+ /// tab | cli
107+ ``` bash hl_lines="3"
108+ garf query.sql --source API_SOURCE \
109+ --output console \
110+ --console.format=json
111+ ```
112+ ///
113+
114+ /// tab | python
115+ ``` python hl_lines="7"
116+ from garf_core import report
117+ from garf_io.writers import console_writer
118+
119+ # Create example report
120+ sample_report = report.GarfReport(results = [[1 ]], column_names = [' one' ])
121+
122+ writer = console_writer.ConsoleWriter(format = ' json' )
123+ writer.write(sample_report, ' query' )
124+ ```
125+ ///
126+
127+ #### Page size
128+
129+ If you're using ` console ` writer with ` table ` format option, you can specify
130+ ` page_size ` parameter to print N rows to the console.
131+
132+ /// tab | cli
133+ ``` bash hl_lines="3"
134+ garf query.sql --source API_SOURCE \
135+ --output console \
136+ --console.page-size=100
137+ ```
138+ ///
139+
140+ /// tab | python
141+ ``` python hl_lines="7"
142+ from garf_core import report
143+ from garf_io.writers import console_writer
144+
145+ # Create example report
146+ sample_report = report.GarfReport(results = [[1 ]], column_names = [' one' ])
147+
148+ writer = console_writer.ConsoleWriter(page_size = 100 )
149+ writer.write(sample_report, ' query' )
150+ ```
151+ ///
100152
101153### CSV
102154
@@ -122,6 +174,34 @@ writer.write(sample_report, 'query')
122174```
123175///
124176
177+ #### Destination folder
178+
179+ For ` csv ` writer you can specify the local or remote folder to store results.
180+ I.e. if you want to write results to Google Cloud Storage bucket ` gs://PROJECT_ID/bucket ` ,
181+ you need to provide ` destination_folder ` parameter.
182+
183+
184+ /// tab | cli
185+ ``` bash hl_lines="3"
186+ garf query.sql --source API_SOURCE \
187+ --output csv \
188+ --csv.destination-folder=gs://PROJECT_ID/bucket
189+ ```
190+ ///
191+
192+ /// tab | python
193+ ``` python
194+ from garf_core import report
195+ from garf_io.writers import csv_writer
196+
197+ # Create example report
198+ sample_report = report.GarfReport(results = [[1 ]], column_names = [' one' ])
199+
200+ writer = csv_writer.CsvWriter(destination_folder = ' gs://PROJECT_ID/bucket/' )
201+ writer.write(sample_report, ' query' )
202+ ```
203+ ///
204+
125205### JSON
126206
127207` json ` writer allows you to save ` GarfReport ` as JSON or JSONL file to local or remote storage.
@@ -146,6 +226,62 @@ writer.write(sample_report, 'query')
146226```
147227///
148228
229+ #### Destination folder
230+
231+ You can specify the local or remote folder to store results.
232+ I.e. if you want to write results to Google Cloud Storage bucket ` gs://PROJECT_ID/bucket ` ,
233+ you need to provide ` destination_folder ` parameter.
234+
235+
236+ /// tab | cli
237+ ``` bash hl_lines="3"
238+ garf query.sql --source API_SOURCE \
239+ --output json \
240+ --json.destination-folder=gs://PROJECT_ID/bucket
241+ ```
242+ ///
243+
244+ /// tab | python
245+ ``` python
246+ from garf_core import report
247+ from garf_io.writers import json_writer
248+
249+ # Create example report
250+ sample_report = report.GarfReport(results = [[1 ]], column_names = [' one' ])
251+
252+ writer = json_writer.JsonWriter(destination_folder = ' gs://PROJECT_ID/bucket/' )
253+ writer.write(sample_report, ' query' )
254+ ```
255+ ///
256+
257+ #### Format
258+
259+ You can specify the output format:
260+
261+ * ` json ` - JSON (default)
262+ * ` jsonl ` - JSON lines
263+
264+ /// tab | cli
265+ ``` bash hl_lines="3"
266+ garf query.sql --source API_SOURCE \
267+ --output json \
268+ --json.format=jsonl
269+ ```
270+ ///
271+
272+ /// tab | python
273+ ``` python hl_lines="7"
274+ from garf_core import report
275+ from garf_io.writers import json_writer
276+
277+ # Create example report
278+ sample_report = report.GarfReport(results = [[1 ]], column_names = [' one' ])
279+
280+ writer = json_writer.JsonWriter(format = ' jsonl' )
281+ writer.write(sample_report, ' query' )
282+ ```
283+ ///
284+
149285### BigQuery
150286
151287!!! important
@@ -178,6 +314,110 @@ writer.write(sample_report, 'query')
178314```
179315///
180316
317+ #### Project
318+
319+ By default reports are saved to ` GOOGLE_CLOUD_PROJECT ` .
320+ You can overwrite it with ` project ` parameter.
321+
322+ /// tab | cli
323+ ``` bash hl_lines="3"
324+ garf query.sql --source API_SOURCE \
325+ --output bq \
326+ --bq.project=PROJECT_ID
327+ ```
328+ ///
329+
330+ /// tab | python
331+ ``` python hl_lines="7"
332+ from garf_core import report
333+ from garf_io.writers import bigquery_writer
334+
335+ # Create example report
336+ sample_report = report.GarfReport(results = [[1 ]], column_names = [' one' ])
337+
338+ writer = bigquery_writer.BigQueryWriter(project = " PROJECT_ID" )
339+ writer.write(sample_report, ' query' )
340+ ```
341+ ///
342+
343+ #### Dataset
344+
345+ By default reports are saved to ` garf ` dataset.
346+ You can overwrite it with ` dataset ` parameter.
347+
348+ /// tab | cli
349+ ``` bash hl_lines="3"
350+ garf query.sql --source API_SOURCE \
351+ --output bq \
352+ --bq.dataset=DATASET
353+ ```
354+ ///
355+
356+ /// tab | python
357+ ``` python hl_lines="7"
358+ from garf_core import report
359+ from garf_io.writers import bigquery_writer
360+
361+ # Create example report
362+ sample_report = report.GarfReport(results = [[1 ]], column_names = [' one' ])
363+
364+ writer = bigquery_writer.BigQueryWriter(dataset = " DATASET" )
365+ writer.write(sample_report, ' query' )
366+ ```
367+ ///
368+
369+ #### Location
370+
371+ By default reports are saved to ` US ` location.
372+ You can overwrite it with ` location ` parameter.
373+
374+ /// tab | cli
375+ ``` bash hl_lines="3"
376+ garf query.sql --source API_SOURCE \
377+ --output bq \
378+ --bq.location=LOCATION
379+ ```
380+ ///
381+
382+ /// tab | python
383+ ``` python hl_lines="7"
384+ from garf_core import report
385+ from garf_io.writers import bigquery_writer
386+
387+ # Create example report
388+ sample_report = report.GarfReport(results = [[1 ]], column_names = [' one' ])
389+
390+ writer = bigquery_writer.BigQueryWriter(location = " LOCATION" )
391+ writer.write(sample_report, ' query' )
392+ ```
393+ ///
394+
395+ #### Write disposition
396+
397+ By default reports overwrite any existing data.
398+ You can overwrite it with [ ` write_disposition ` ] ( https://cloud.google.com/bigquery/docs/reference/auditlogs/rest/Shared.Types/BigQueryAuditMetadata.WriteDisposition ) parameter.
399+
400+ /// tab | cli
401+ ``` bash hl_lines="3"
402+ garf query.sql --source API_SOURCE \
403+ --output bq \
404+ --bq.write_disposition=DISPOSITION
405+ ```
406+ ///
407+
408+ /// tab | python
409+ ``` python hl_lines="7"
410+ from garf_core import report
411+ from garf_io.writers import bigquery_writer
412+
413+ # Create example report
414+ sample_report = report.GarfReport(results = [[1 ]], column_names = [' one' ])
415+
416+ writer = bigquery_writer.BigQueryWriter(write_disposition = " DISPOSITION" )
417+ writer.write(sample_report, ' query' )
418+ ```
419+ ///
420+
181421### Google Sheets
182422
183423!!! important
@@ -243,3 +483,10 @@ writer = sqldb_writer.SqlAlchemyWriter(
243483writer.write(sample_report, ' query' )
244484```
245485///
486+
487+ ## Configuration
488+
489+ Each of writer also support two options for dealing with arrays:
490+
491+ * ` WRITER.array-handling ` - arrays handling method: "strings" (default) - store arrays as strings (items combined via a separator, e.g. "item1|item2"), "arrays" - store arrays as arrays.
492+ * ` WRITER.array-separator ` - a separator symbol for joining arrays as strings, by default '|'.
0 commit comments