-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathbigquery_avro_tools.py
More file actions
143 lines (120 loc) · 4.37 KB
/
bigquery_avro_tools.py
File metadata and controls
143 lines (120 loc) · 4.37 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Tools used tool work with Avro files in the context of BigQuery.
Classes, constants and functions in this file are experimental and have no
backwards compatibility guarantees.
NOTHING IN THIS FILE HAS BACKWARDS COMPATIBILITY GUARANTEES.
"""
from typing import Any
from typing import Dict
# BigQuery types as listed in
# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types
# with aliases (RECORD, BOOLEAN, FLOAT, INTEGER) as defined in
# https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/java/latest/com/google/api/services/bigquery/model/TableFieldSchema.html#setType-java.lang.String-
BIG_QUERY_TO_AVRO_TYPES = {
"STRUCT": "record",
"RECORD": "record",
"STRING": "string",
"BOOL": "boolean",
"BOOLEAN": "boolean",
"BYTES": "bytes",
"FLOAT64": "double",
"FLOAT": "double",
"INT64": "long",
"INTEGER": "long",
"TIME": {
"type": "long",
"logicalType": "time-micros",
},
"TIMESTAMP": {
"type": "long",
"logicalType": "timestamp-micros",
},
"DATE": {
"type": "int",
"logicalType": "date",
},
"DATETIME": "string",
"NUMERIC": {
"type": "bytes",
"logicalType": "decimal",
# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#numeric-type
"precision": 38,
"scale": 9,
},
"GEOGRAPHY": "string",
}
def get_record_schema_from_dict_table_schema(
schema_name: str,
table_schema: Dict[str, Any],
namespace: str = "apache_beam.io.gcp.bigquery") -> Dict[str, Any]:
# noqa: F821
"""Convert a table schema into an Avro schema.
Args:
schema_name (str): The name of the record.
table_schema (Dict[str, Any]): A BigQuery table schema in dict form.
namespace (str): The namespace of the Avro schema.
Returns:
Dict[str, Any]: The schema as an Avro RecordSchema.
"""
avro_fields = [
table_field_to_avro_field(field, ".".join((namespace, schema_name)))
for field in table_schema["fields"]
]
return {
"type": "record",
"name": schema_name,
"fields": avro_fields,
"doc": "Translated Avro Schema for {}".format(schema_name),
"namespace": namespace,
}
def table_field_to_avro_field(table_field: Dict[str, Any],
namespace: str) -> Dict[str, Any]:
# noqa: F821
"""Convert a BigQuery field to an avro field.
Args:
table_field (Dict[str, Any]): A BigQuery field in dict form.
Returns:
Dict[str, Any]: An equivalent Avro field in dict form.
"""
assert "type" in table_field, \
"Unable to get type for table field {}".format(table_field)
assert table_field["type"] in BIG_QUERY_TO_AVRO_TYPES, \
"Unable to map BigQuery field type {} to avro type".format(
table_field["type"])
avro_type = BIG_QUERY_TO_AVRO_TYPES[table_field["type"]]
if avro_type == "record":
element_type = get_record_schema_from_dict_table_schema(
table_field["name"],
table_field,
namespace=".".join((namespace, table_field["name"])))
else:
element_type = avro_type
field_mode = table_field.get("mode", "NULLABLE")
if field_mode in (None, "NULLABLE"):
field_type = ["null", element_type]
elif field_mode == "REQUIRED":
field_type = element_type
elif field_mode == "REPEATED":
field_type = {"type": "array", "items": element_type}
else:
raise ValueError("Unknown BigQuery field mode: {}".format(field_mode))
avro_field = {"type": field_type, "name": table_field["name"]}
doc = table_field.get("description")
if doc:
avro_field["doc"] = doc
return avro_field