bqseine 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of bqseine might be problematic. Click here for more details.

bqseine/.polyp.py.swp ADDED
Binary file
bqseine/__init__.py ADDED
File without changes
bqseine/polyp.py ADDED
@@ -0,0 +1,381 @@
1
+ from gcp_secrets.secrets import *
2
+ from google.cloud import bigquery
3
+ from google.cloud.exceptions import NotFound
4
+ from datetime import date, datetime
5
+ from db_lib import *
6
+ import json
7
+
8
+
9
+ lastSeineId = {}
10
+ tableSchema = {}
11
+ tableCurrentSchema = {}
12
+ tableCurrentSchemaType = {}
13
+ tableReset = {}
14
+ #client = bigquery.Client()
15
+
16
+ # * EXCEPT(is_generated, generation_expression, is_stored, is_updatable)
17
+ tableColumnsQuery = """
18
+ SELECT
19
+ column_name
20
+ FROM
21
+ `__dataset__`.INFORMATION_SCHEMA.COLUMNS
22
+ WHERE
23
+ table_name = '__table__';
24
+ """
25
+
26
+
27
+ def resolveType(value):
28
+ isDatetime = False
29
+ tempDatetime = None
30
+ try:
31
+ timeDatetime = datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
32
+ value = timeDatetime
33
+ isDatetime = True
34
+ except:
35
+ pass
36
+
37
+ if isinstance(value, bool):
38
+ return "BOOL"
39
+ elif isinstance(value, int):
40
+ return "INT64"
41
+ elif isinstance(value, float):
42
+ return "FLOAT64"
43
+ elif type(value) == datetime:
44
+ return "DATETIME"
45
+ else:
46
+ return "STRING"
47
+
48
+
49
+ def testValue(value, tableKey, fieldKey):
50
+ global tableCurrentSchemaType
51
+ try:
52
+ timeDatetime = datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
53
+ return timeDatetime.strftime('%Y-%m-%dT%H:%M:%S')
54
+ except:
55
+ pass
56
+
57
+ fieldType = None
58
+ if fieldKey in tableCurrentSchemaType[tableKey].keys():
59
+ fieldType = tableCurrentSchemaType[tableKey][fieldKey]
60
+ else:
61
+ fieldType = resolveType(value)
62
+ tableCurrentSchemaType[tableKey][fieldKey] = fieldType
63
+
64
+ if fieldType == "BOOL":
65
+ if isinstance(value, bool):
66
+ return value
67
+ else:
68
+ return False
69
+ elif fieldType == "INT64":
70
+ if isinstance(value, int):
71
+ return value
72
+ else:
73
+ return 0
74
+ elif fieldType == "FLOAT64":
75
+ if isinstance(value, float):
76
+ return value
77
+ else:
78
+ return 0
79
+ elif fieldType == "STRING":
80
+ if isinstance(value, str):
81
+ return value
82
+ else:
83
+ return ""
84
+ else:
85
+ if isinstance(value, str):
86
+ return value
87
+ else:
88
+ return json.dumps(value)
89
+
90
+ return ""
91
+
92
+
93
+ def incrementId(curKey):
94
+ global lastSeineId
95
+ if curKey in lastSeineId.keys():
96
+ lastSeineId[curKey] += 1
97
+ else:
98
+ lastSeineId[curKey] = 1
99
+
100
+
101
+ def sync(myGoogleProject, blob, curKey, bqRegion = 'US', firstReset = False, idField = None):
102
+ global lastSeineId
103
+ global tableSchema
104
+ global tableReset
105
+ global tableCurrentSchema
106
+ global tableCurrentSchemaType
107
+
108
+ if len(curKey) < 1:
109
+ raise ExceptionType("A default current key (second arg) should be provided")
110
+
111
+ stack = []
112
+ if isinstance(blob, list):
113
+ for part in blob:
114
+ stack.insert(0, (curKey, part, curKey, 0))
115
+ else:
116
+ stack = [(curKey, blob, curKey, 0)]
117
+
118
+ tableChecked = False
119
+
120
+ client = None
121
+ try:
122
+ client = bigquery.Client(project=myGoogleProject)
123
+ except:
124
+ raise ExceptionType(f"Could not connect to {myGoogleProject}")
125
+
126
+ if firstReset:
127
+ lastSeineId = {}
128
+ tableSchema = {}
129
+ tableReset = {}
130
+ tableCurrentSchema = {}
131
+ tableCurrentSchemaType = {}
132
+
133
+ keyNotInSchema = {}
134
+ dataToLoad = {}
135
+ seineDataset = False
136
+ dataset = ""
137
+ datasetName = "seine_" + curKey
138
+ try:
139
+ seineDataset = client.get_dataset(myGoogleProject + f".{datasetName}")
140
+ except NotFound:
141
+ seineDataset = bigquery.Dataset(myGoogleProject + f".{datasetName}")
142
+ seineDataset.location = bqRegion
143
+ seineDataset = client.create_dataset(seineDataset, timeout=30)
144
+ print("Created dataset {}".format(seineDataset.dataset_id))
145
+
146
+ valuesArray = []
147
+ currentDepth = 0
148
+ while stack:
149
+ curKey, curDict, lastKey, parentId = stack.pop()
150
+ currentDepth += 1
151
+ fieldTypes = {}
152
+ fields = []
153
+ fieldsJson = {}
154
+ values = []
155
+ fieldType = {}
156
+ valuePlaceholders = {}
157
+ counter = 0
158
+
159
+ #seineDataset = client.get_dataset(myGoogleProject + f".{datasetName}")
160
+ print("-------------------------")
161
+ print(curDict)
162
+ noUpdateNeeded = False
163
+
164
+ if curKey not in lastSeineId.keys():
165
+ lastSeineId[curKey] = 1
166
+
167
+ if parentId == 0 and curKey == idField and isinstance(curDict, int):
168
+ try:
169
+ curTableName = myGoogleProject + f".{datasetName}." + curKey
170
+ queryJob = client.query(f"select {idField} from {curTableName} where {idField} = {curDict}")
171
+ returned = queryJob.result()
172
+ foundId = False
173
+ for row in returned:
174
+ foundId = True
175
+ if foundId:
176
+ print(f"ID exists {idField} = {curDict}")
177
+ print(returned)
178
+ continue
179
+ except:
180
+ print("================== QUERY FAILED =======================")
181
+ pass
182
+
183
+ #if curKey not in keyNotInSchema.keys():
184
+ keyNotInSchema[curKey] = []
185
+ if curKey not in tableCurrentSchema.keys():
186
+ try:
187
+ curTableName = myGoogleProject + f".{datasetName}." + curKey
188
+ queryJob = client.query(f"select max(seine_id) as max_id from {curTableName}")
189
+ returned = queryJob.result()
190
+ print(returned)
191
+ for row in returned:
192
+ if row.max_id is not None and isinstance(row.max_id, int):
193
+ lastSeineId[curKey] = int(row.max_id) + 1
194
+ except:
195
+ pass
196
+ tableCurrentSchema[curKey] = []
197
+ tableCurrentSchemaType[curKey] = {}
198
+
199
+ if isinstance(curDict, list):
200
+ if not isinstance(curDict[0], dict):
201
+ fields.append(curKey)
202
+ fieldTypes[curKey] = resolveType(json.dumps(curDict))
203
+ valuePlaceholders[curKey] = testValue(json.dumps(curDict), curKey, curKey)
204
+ if curKey not in tableCurrentSchema[curKey]:
205
+ tableCurrentSchema[curKey].append(curKey)
206
+ keyNotInSchema[curKey].append(curKey)
207
+ #tableCurrentSchemaType[curKey][curKey] = resolveType(json.dumps(curDict))
208
+ else:
209
+ noUpdateNeeded = True
210
+ for tempDict in curDict:
211
+ stack.insert(0, (curKey, tempDict, lastKey, lastSeineId[lastKey]))
212
+ continue
213
+
214
+ elif isinstance(curDict, dict):
215
+ for key, value in curDict.items():
216
+ if isinstance(value, list):
217
+ if len(value) > 0:
218
+ if not isinstance(value[0], dict) and key not in ["edges"]:
219
+ fields.append(key)
220
+ fieldTypes[key] = resolveType(json.dumps(value))
221
+ valuePlaceholders[key] = testValue(json.dumps(value), curKey, key)
222
+ if key not in tableCurrentSchema[curKey]:
223
+ tableCurrentSchema[curKey].append(key)
224
+ keyNotInSchema[curKey].append(key)
225
+ #tableCurrentSchemaType[curKey][key] = resolveType(json.dumps(value))
226
+ elif key in ["edges"]:
227
+ noUpdateNeeded = True
228
+ for part in value:
229
+ stack.insert(0, (curKey, part, lastKey, lastSeineId[lastKey]))
230
+ else:
231
+ for part in value:
232
+ stack.insert(0, (curKey + "_" + key, part, curKey, lastSeineId[lastKey]))
233
+ #fields.append(key)
234
+ #fieldTypes[key] = resolveType(json.dumps(value))
235
+ #valuePlaceholders[key] = testValue(json.dumps(value), curKey, key)
236
+ #if key not in tableCurrentSchema[curKey]:
237
+ # tableCurrentSchema[curKey].append(key)
238
+ # keyNotInSchema[curKey].append(key)
239
+ else:
240
+ fields.append(key)
241
+ fieldTypes[key] = resolveType(json.dumps(value))
242
+ valuePlaceholders[key] = testValue(json.dumps(value), curKey, key)
243
+ if key not in tableCurrentSchema[curKey]:
244
+ tableCurrentSchema[curKey].append(key)
245
+ keyNotInSchema[curKey].append(key)
246
+ #tableCurrentSchemaType[curKey][key] = resolveType(json.dumps(value))
247
+ elif isinstance(value, dict):
248
+ if key in ["node"]:
249
+ noUpdateNeeded = True
250
+ stack.insert(0, (curKey, value, lastKey, lastSeineId[lastKey]))
251
+ else:
252
+ stack.insert(0, (curKey + "_" + key, value, curKey, lastSeineId[lastKey]))
253
+ fields.append(key)
254
+ fieldTypes[key] = resolveType(lastSeineId[curKey])
255
+ valuePlaceholders[key] = testValue(lastSeineId[curKey], curKey, key)
256
+ if key not in tableCurrentSchema[curKey]:
257
+ tableCurrentSchema[curKey].append(key)
258
+ keyNotInSchema[curKey].append(key)
259
+ #tableCurrentSchemaType[curKey][key] = resolveType(json.dumps(value))
260
+ else:
261
+ # Add schema
262
+ if key not in fields:
263
+ fields.append(key)
264
+ fieldTypes[key] = resolveType(value)
265
+ valuePlaceholders[key] = testValue(value, curKey, key)
266
+ if key not in tableCurrentSchema[curKey]:
267
+ tableCurrentSchema[curKey].append(key)
268
+ keyNotInSchema[curKey].append(key)
269
+ #tableCurrentSchemaType[curKey][key] = resolveType(value)
270
+
271
+ elif isinstance(curDict, str) or isinstance(curDict, int) or isinstance(curDict, float):
272
+ fields.append(curKey)
273
+ fieldTypes[curKey] = resolveType(curDict)
274
+ valuePlaceholders[curKey] = testValue(curDict, curKey, key)
275
+ if curKey not in tableCurrentSchema[curKey]:
276
+ tableCurrentSchema[curKey].append(curKey)
277
+ keyNotInSchema[curKey].append(curKey)
278
+ #tableCurrentSchemaType[curKey][curKey] = resolveType(curDict)
279
+
280
+ else:
281
+ fields.append(curKey)
282
+ fieldTypes[curKey] = resolveType(curDict)
283
+ valuePlaceholders[curKey] = testValue(curDict, curKey, key)
284
+ if curKey not in tableCurrentSchema[curKey]:
285
+ tableCurrentSchema[curKey].append(curKey)
286
+ keyNotInSchema[curKey].append(curKey)
287
+ #tableCurrentSchemaType[curKey][curKey] = resolveType(curDict)
288
+
289
+ #if len(fields) < 1 and parentId is None:
290
+ # continue
291
+
292
+ if noUpdateNeeded:
293
+ continue
294
+
295
+ if len(keyNotInSchema[curKey]) > 0 or curKey not in tableReset.keys():
296
+ tableReset[curKey] = True
297
+ curTableName = myGoogleProject + f".{datasetName}." + curKey
298
+ curTable = False
299
+ tableSchema[curKey] = []
300
+ tableSchema[curKey].append(bigquery.SchemaField("seine_id", "INT64"))
301
+ tableSchema[curKey].append(bigquery.SchemaField("seine_parent_id", "INT64"))
302
+ tableSchema[curKey].append(bigquery.SchemaField("injected", "DATETIME"))
303
+ tableCurrentSchema[curKey].append("seine_id");
304
+ tableCurrentSchema[curKey].append("seine_parent_id");
305
+ tableCurrentSchema[curKey].append("injected");
306
+ try:
307
+ curTable = client.get_table(curTableName)
308
+ #colQuery = tableColumnsQuery.replace("__dataset__", seineDataset).replace("__table__", key)
309
+ #queryJob = client.query(colQuery)
310
+ #returned = queryJob.result()
311
+ existingSchema = curTable.schema
312
+ tableSchema[curKey] = existingSchema
313
+ existingColumns = []
314
+ jobCconfig = bigquery.QueryJobConfig(
315
+ destination=curTableName,
316
+ schema_update_options=[bigquery.SchemaUpdateOption.ALLOW_FIELD_ADDITION],
317
+ write_disposition=bigquery.WriteDisposition.WRITE_APPEND,
318
+ )
319
+ for schemaElement in existingSchema:
320
+ existingColumns.append(schemaElement.name)
321
+ schemaAdjusted = False
322
+ for fidx, field in enumerate(fields):
323
+ if field not in existingColumns:
324
+ if field not in tableCurrentSchema[curKey]:
325
+ tableCurrentSchema[curKey].append(field);
326
+ tableSchema[curKey].append(bigquery.SchemaField(field, fieldTypes[field]))
327
+ existingSchema.append(bigquery.SchemaField(field, fieldTypes[field]))
328
+ if not schemaAdjusted:
329
+ schemaAdjusted = True
330
+ if schemaAdjusted:
331
+ curTable.schema = existingSchema
332
+ try:
333
+ curTable = client.update_table(curTable, ["schema"])
334
+ except:
335
+ pass
336
+
337
+ #if returned.total_rows > 0:
338
+ # counter = 0
339
+ # for row in returned:
340
+ # print(row)
341
+ # counter += 1
342
+ # if counter < 2:
343
+ # continue
344
+ # if row.max_id + 1 > lastSeineId[curKey]:
345
+ # lastSeineId[curKey] = int(row.max_id) + 1
346
+ # print(f"{curKey}" + str(lastSeineId[curKey]))
347
+ except NotFound:
348
+ for fidx, field in enumerate(fields):
349
+ tableSchema[curKey].append(bigquery.SchemaField(field, fieldTypes[field]))
350
+ if field not in tableCurrentSchema[curKey]:
351
+ tableCurrentSchema[curKey].append(field);
352
+ curTable = bigquery.Table(curTableName, schema=tableSchema[curKey])
353
+ curTable = client.create_table(curTable)
354
+ lastSeineId[curKey] = 1
355
+ keyNotInSchema[curKey] = []
356
+
357
+ if curKey not in dataToLoad.keys():
358
+ dataToLoad[curKey] = []
359
+ tempRow = {
360
+ "seine_id": lastSeineId[curKey],
361
+ "seine_parent_id": parentId,
362
+ "injected": datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')
363
+ }
364
+ for idx, field in enumerate(fields):
365
+ tempRow[field] = valuePlaceholders[field]
366
+ print(tempRow)
367
+ dataToLoad[curKey].append(tempRow)
368
+ lastSeineId[curKey] += 1
369
+
370
+ conn, cur = dbConnect()
371
+ for tableName in dataToLoad.keys():
372
+ curTable = client.get_table(myGoogleProject + f".{datasetName}." + tableName)
373
+ errors = client.insert_rows_json(
374
+ curTable, dataToLoad[tableName], row_ids=[None] * len(dataToLoad[tableName])
375
+ )
376
+ if errors == []:
377
+ print("Loaded " + str(len(dataToLoad[tableName])) + " rows into " + tableName)
378
+ else:
379
+ print("FAILED: loading " + str(len(dataToLoad[tableName])) + " rows into " + tableName)
380
+ print(errors)
381
+
@@ -0,0 +1,65 @@
1
+ Metadata-Version: 2.4
2
+ Name: bqseine
3
+ Version: 0.0.1
4
+ Summary: ETL for BigQuery
5
+ Project-URL: Homepage, https://github.com/shaafiee/seine
6
+ Project-URL: Issues, https://github.com/shaafiee/siene/issues
7
+ Author-email: Shaafiee <shaafiee@gmail.com>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Python: >=3.9
13
+ Description-Content-Type: text/markdown
14
+
15
+ # BQSeine
16
+ ## Python dict to BigQuery data loader
17
+ Seine is a data loader that pushes data in a dictionary to BigQuery in relational normalized form.
18
+
19
+ ## Usage
20
+ ```
21
+ from bqseine import sync
22
+ sourceData = [
23
+ {
24
+ 'item': 'Juice',
25
+ 'price': 20.0,
26
+ 'stock': [
27
+ {
28
+ 'batch': '2025-01-20',
29
+ 'qty': 300
30
+ },
31
+ {
32
+ 'batch': '2025-02-02',
33
+ 'qty': 50
34
+ }
35
+ ]
36
+ },
37
+ {
38
+ 'item': 'Burger',
39
+ 'price': 30.0,
40
+ 'stock': [
41
+ {
42
+ 'batch': '2025-02-10',
43
+ 'qty': 200
44
+ }
45
+ ]
46
+ }
47
+ ]
48
+ sync('someGoogleProject', sourceData, 'catalog', 'US')
49
+ ### The arguments above are:
50
+ ### sync(<Google project name>, <dict>, <main table name>, <BigQuery region>)*
51
+ ```
52
+
53
+ The above example will generate the following tables in BigQuery:
54
+ ### catalog
55
+ | seine_id | seine_parent_id | item | price | injected |
56
+ | --- | --- | --- | --- | --- |
57
+ | 1 | 0 | 'Juice' | 20.0 | now() |
58
+ | 2 | 0 | 'Burger' | 30.0 | now() |
59
+
60
+ ### catalog_stock
61
+ | seine_id | seine_parent_id | batch | qty | injected |
62
+ | --- | --- | --- | --- | --- |
63
+ | 1 | 1 | '2025-01-20' | 300 | now() |
64
+ | 2 | 1 | '2025-02-02' | 50 | now() |
65
+ | 3 | 2 | '2025-02-10' | 200 | now() |
@@ -0,0 +1,7 @@
1
+ bqseine/.polyp.py.swp,sha256=MMw6cZr20moge7_M81xWAvTiKQiNtceG3I2PUAykSzw,16384
2
+ bqseine/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ bqseine/polyp.py,sha256=Pk-_J7WTPxU8Tq9yIwn7GEO1xw1CeKHQp0g2eV_SEiQ,12178
4
+ bqseine-0.0.1.dist-info/METADATA,sha256=8rxfZb6WbaI1tjgB5yhuQ4mlnybf7KDahiNdyNNVZwE,1554
5
+ bqseine-0.0.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
+ bqseine-0.0.1.dist-info/licenses/LICENSE,sha256=ACwmltkrXIz5VsEQcrqljq-fat6ZXAMepjXGoe40KtE,1069
7
+ bqseine-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) [year] [fullname]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.