Created
September 15, 2016 16:52
-
-
Save daspecster/e26346af9fea67aa6efc0716c11c5956 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@snippet | |
def udf_inline(client, _): | |
"""Inline UDF.""" | |
from google.cloud.bigquery.job import UDFResource | |
UDF_QUERY = ('SELECT upper_name FROM upperCaseName(' | |
'[bigquery-public-data:usa_names.usa_1910_2013]) ' | |
'WHERE state = "TX"') | |
INLINE_UDF_CODE = """ | |
function upperCaseName(r, emit) { | |
emit({upper_name: r.name.toUpperCase(), | |
state: r.state}); | |
} | |
bigquery.defineFunction( | |
'upperCaseName', | |
['name', 'state'], | |
[{'name': 'upper_name', 'type': 'string'}, | |
{'name': 'state', 'type': 'string'}], | |
upperCaseName | |
); | |
""" | |
LIMIT = 100 | |
LIMITED = '%s LIMIT %d' % (UDF_QUERY, LIMIT) | |
TIMEOUT_MS = 1000 | |
query = client.run_sync_query(LIMITED) | |
query.timeout_ms = TIMEOUT_MS | |
query.udf_resources = [UDFResource("inlineCode", INLINE_UDF_CODE)] | |
query.run() # API Query | |
assert query.complete | |
assert len(query.rows) == LIMIT | |
for row in query.rows: | |
assert row['name'].isUpper() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment