schema_builder

bigquery.schema_builder.bigquery_type(o, timestamp_parser=<function default_timestamp_parser>)

Given a value, return the matching BigQuery type of that value. Must be one of str/unicode/int/float/datetime/record, where record is a dict containing value which have matching BigQuery types.

Parameters:

o : object

A Python object

time_stamp_parser : function, optional

Unary function taking a str and returning and bool that is True if the string represents a date

Returns:

Union[str, None]

Name of the corresponding BigQuery type for o, or None if no type could be found

Examples

>>> bigquery_type("abc")
"string"
>>> bigquery_type(123)
"integer"
bigquery.schema_builder.describe_field(k, v, timestamp_parser=<function default_timestamp_parser>)

Given a key representing a column name and value representing the value stored in the column, return a representation of the BigQuery schema element describing that field. Raise errors if invalid value types are provided.

Parameters:

k : Union[str, unicode]

Key representing the column

v : Union[str, unicode, int, float, datetime, object]

Value mapped to by k

Returns:

object

Describing the field

Raises:

Exception

If invalid value types are provided.

Examples

>>> describe_field("username", "Bob")
{"name": "username", "type": "string", "mode": "nullable"}
>>> describe_field("users", [{"username": "Bob"}])
{"name": "users", "type": "record", "mode": "repeated",
 "fields": [{"name":"username","type":"string","mode":"nullable"}]}
bigquery.schema_builder.schema_from_record(record, timestamp_parser=<function default_timestamp_parser>)

Generate a BigQuery schema given an example of a record that is to be inserted into BigQuery.

Parameters:

record : dict

Example of a record that is to be inserted into BigQuery

timestamp_parser : function, optional

Unary function taking a str and returning and bool that is True if the string represents a date

Returns:

Schema: list