Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (66 loc) · 2.65 KB
/
Copy pathmain.py
File metadata and controls
86 lines (66 loc) · 2.65 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
import logging
import os
import traceback
import re
from google.cloud import bigquery
from google.cloud import storage
import yaml
with open("./schemas.yaml") as schema_file:
config = yaml.load(schema_file, Loader=yaml.Loader)
PROJECT_ID = os.getenv('cloudquicklab')
BQ_DATASET = 'staging'
CS = storage.Client()
BQ = bigquery.Client()
job_config = bigquery.LoadJobConfig()
def streaming(data):
bucketname = data['bucket']
print("Bucket name",bucketname)
filename = data['name']
print("File name",filename)
timeCreated = data['timeCreated']
print("Time Created",timeCreated)
try:
for table in config:
tableName = table.get('name')
if re.search(tableName.replace('_', '-'), filename) or re.search(tableName, filename):
tableSchema = table.get('schema')
_check_if_table_exists(tableName,tableSchema)
tableFormat = table.get('format')
if tableFormat == 'NEWLINE_DELIMITED_JSON':
_load_table_from_uri(data['bucket'], data['name'], tableSchema, tableName)
except Exception:
print('Error streaming file. Cause: %s' % (traceback.format_exc()))
def _check_if_table_exists(tableName,tableSchema):
table_id = BQ.dataset(BQ_DATASET).table(tableName)
try:
BQ.get_table(table_id)
except Exception:
logging.warn('Creating table: %s' % (tableName))
schema = create_schema_from_yaml(tableSchema)
table = bigquery.Table(table_id, schema=schema)
table = BQ.create_table(table)
print("Created table {}.{}.{}".format(table.project, table.dataset_id, table.table_id))
def _load_table_from_uri(bucket_name, file_name, tableSchema, tableName):
uri = 'gs://%s/%s' % (bucket_name, file_name)
table_id = BQ.dataset(BQ_DATASET).table(tableName)
schema = create_schema_from_yaml(tableSchema)
print(schema)
job_config.schema = schema
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON,
job_config.write_disposition = 'WRITE_APPEND',
load_job = BQ.load_table_from_uri(
uri,
table_id,
job_config=job_config,
)
load_job.result()
print("Job finished.")
def create_schema_from_yaml(table_schema):
schema = []
for column in table_schema:
schemaField = bigquery.SchemaField(column['name'], column['type'], column['mode'])
schema.append(schemaField)
if column['type'] == 'RECORD':
schemaField._fields = create_schema_from_yaml(column['fields'])
return schema
streaming(data)