Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting illegal BigQuery characters in table and field names #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions target_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import http.client
import urllib
import pkg_resources
import re

from jsonschema import validate
import singer
Expand Down Expand Up @@ -51,8 +52,11 @@ def emit_state(state):
def clear_dict_hook(items):
return {k: v if v is not None else '' for k, v in items}

def fix_name(name):
return re.sub('[^a-zA-Z0-9_]', '_', name)

def define_schema(field, name):
schema_name = name
schema_name = fix_name(name)
schema_type = "STRING"
schema_mode = "NULLABLE"
schema_description = None
Expand Down Expand Up @@ -139,7 +143,10 @@ def persist_lines_job(project_id, dataset_id, lines=None, truncate=False, valida
validate(msg.record, schema)

# NEWLINE_DELIMITED_JSON expects literal JSON formatted data, with a newline character splitting each row.
dat = bytes(json.dumps(msg.record) + '\n', 'UTF-8')
new_record = {}
for key, value in msg.record.items():
new_record[fix_name(key)] = value
dat = bytes(json.dumps(new_record) + '\n', 'UTF-8')

rows[msg.stream].write(dat)
#rows[msg.stream].write(bytes(str(msg.record) + '\n', 'UTF-8'))
Expand Down Expand Up @@ -170,7 +177,7 @@ def persist_lines_job(project_id, dataset_id, lines=None, truncate=False, valida
raise Exception("Unrecognized message {}".format(msg))

for table in rows.keys():
table_ref = bigquery_client.dataset(dataset_id).table(table)
table_ref = bigquery_client.dataset(dataset_id).table(fix_name(table))
SCHEMA = build_schema(schemas[table])
load_config = LoadJobConfig()
load_config.schema = SCHEMA
Expand Down