SlideShare a Scribd company logo
1 of 12
Download to read offline
David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
Google Big Query
UDFs
User Defined Functions
@dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
What is it
Google Big Query’s UDF[1] are:
● Javascript based functions
● Extend the query to include “Map” like functionality (Mapreduce[2])
● Can return a different schema to the input schema
● Runs the function against every row in the input dataset.
See presentation[3] by Thomas Park[4] & Fillipe Hoffa[5]
[1] https://cloud.google.com/bigquery/user-defined-functions
[2] https://en.wikipedia.org/wiki/MapReduce
[3] http://www.slideshare.net/BigDataSpain/thomas-park-hands-on-with-big-query-javascript-udfs-bigdataspain-2014
[4] no link found
[5] https://twitter.com/felipehoffa
David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
Where can I find this UDF?
Click here for
UDFs
David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
Parts of a UDF
A UDF has 3 parts:
1. Emitter Function
○ A function that accepts a row and function (emit) object and runs the function on parts of the
row
2. Helper Function
○ This defines the a working function that can be called in the emitter function. Requires robust
error handling.
3. Registration Function
○ Links the emitter function, input / output schemas and labels together
David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
Emitter Function
function urlDecode(row, emit) {
emit({title: functionHelper(row.title), requests: row.num_requests});
}
Function Name row: defined by the input column schema
row: defined by the output schema
Helper Function(s) Input to helper function Element from row
David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
function functionHelper(s [,...]){
try {
[...]
return decodeURI(s);
} catch (ex) {
return s;
}
}
Helper Function
Function Name
Must include one term
Can add additional parameters if needed.
Error Handling
Very Important
Can add additional statements if needed.
Return a single term.
Return a term on failure too !
David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
How do I test my code...
Google have provided a beautiful page to test out your functions:
http://storage.googleapis.com/bigquery-udf-test-tool/testtool.html
This allows you to enter data (JSON), functions (emitter and helper) and see its
output.
David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
Silly example...
Input Data:
[{a: "Enter your query data", b:"a", c:"_"},
{a: "here, as JSON,", b:",", c:"*"},
{a: "and click 'Evaluate' to run your function over the data rows!", b:"!", c:"^"}]
User Defined Function:
function(r, emit) {
emit({len: r.a.length, fc: replaceHelper(r.a, r.b, r.c)});
}
function replaceHelper(s, t, r){
try {
return s.replace(t, r);
} catch (ex) {
return s;
}
}
David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
What about the Registration Function?
Once the Emitter and Helper Functions have been tested, it is now time to apply them to an actual query...
bigquery.defineFunction(
'urlDecode',
['title', 'num_requests'],
[{name: 'title', type: 'string'},
{name: 'requests', type: 'integer'}],
urlDecode
);
Input column names
JSON Output
Schema
Internal Function Reference
Verb used to
call function
from SQL
David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
So how can I use it?
Enter the 3 UDF parts in the UDF editor, and click the Query Editor
SELECT requests, title
FROM
urlDecode(
SELECT
title, sum(requests) AS num_requests
FROM
[fh-bigquery:wikipedia.pagecounts_201504]
WHERE language = 'fr'
GROUP EACH BY title
)
WHERE title LIKE '%ç%'
ORDER BY requests DESC
LIMIT 100
David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
Break this down for me...
SELECT requests, title
FROM
urlDecode(
SELECT
title, sum(requests) AS num_requests
FROM
[fh-bigquery:wikipedia.pagecounts_201504]
WHERE language = 'fr'
GROUP EACH BY title
)
WHERE title LIKE '%ç%'
ORDER BY requests DESC
LIMIT 100
Field names defined in the
output schema
SQL Label for the
UDF
Input columns
SQL select statement each
row of this query will have the
UDF applied to them
David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
Questions:
Any questions, please tweet me or +dreffed
Thank You

More Related Content

What's hot

What's hot (20)

BigQuery implementation
BigQuery implementationBigQuery implementation
BigQuery implementation
 
Google Cloud Platform at Vente-Exclusive.com
Google Cloud Platform at Vente-Exclusive.comGoogle Cloud Platform at Vente-Exclusive.com
Google Cloud Platform at Vente-Exclusive.com
 
Google BigQuery for Everyday Developer
Google BigQuery for Everyday DeveloperGoogle BigQuery for Everyday Developer
Google BigQuery for Everyday Developer
 
Big Query Basics
Big Query BasicsBig Query Basics
Big Query Basics
 
TDC2016SP - Trilha BigData
TDC2016SP - Trilha BigDataTDC2016SP - Trilha BigData
TDC2016SP - Trilha BigData
 
30 days of google cloud event
30 days of google cloud event30 days of google cloud event
30 days of google cloud event
 
Redshift VS BigQuery
Redshift VS BigQueryRedshift VS BigQuery
Redshift VS BigQuery
 
GDD Brazil 2010 - Google Storage, Bigquery and Prediction APIs
GDD Brazil 2010 - Google Storage, Bigquery and Prediction APIsGDD Brazil 2010 - Google Storage, Bigquery and Prediction APIs
GDD Brazil 2010 - Google Storage, Bigquery and Prediction APIs
 
Big query
Big queryBig query
Big query
 
Workshop 20140522 BigQuery Implementation
Workshop 20140522   BigQuery ImplementationWorkshop 20140522   BigQuery Implementation
Workshop 20140522 BigQuery Implementation
 
BigQuery for the Big Data win
BigQuery for the Big Data winBigQuery for the Big Data win
BigQuery for the Big Data win
 
VoxxedDays Bucharest 2017 - Powering interactive data analysis with Google Bi...
VoxxedDays Bucharest 2017 - Powering interactive data analysis with Google Bi...VoxxedDays Bucharest 2017 - Powering interactive data analysis with Google Bi...
VoxxedDays Bucharest 2017 - Powering interactive data analysis with Google Bi...
 
Google BigQuery Best Practices
Google BigQuery Best PracticesGoogle BigQuery Best Practices
Google BigQuery Best Practices
 
Augmenting Mongo DB with treasure data
Augmenting Mongo DB with treasure dataAugmenting Mongo DB with treasure data
Augmenting Mongo DB with treasure data
 
Google App Engine 7 9-14
Google App Engine 7 9-14Google App Engine 7 9-14
Google App Engine 7 9-14
 
Unifying Events and Logs into the Cloud
Unifying Events and Logs into the CloudUnifying Events and Logs into the Cloud
Unifying Events and Logs into the Cloud
 
Google Developer Group - Cloud Singapore BigQuery Webinar
Google Developer Group - Cloud Singapore BigQuery WebinarGoogle Developer Group - Cloud Singapore BigQuery Webinar
Google Developer Group - Cloud Singapore BigQuery Webinar
 
An Intro to Elasticsearch and Kibana
An Intro to Elasticsearch and KibanaAn Intro to Elasticsearch and Kibana
An Intro to Elasticsearch and Kibana
 
Budapest Data Forum 2017 - BigQuery, Looker And Big Data Analytics At Petabyt...
Budapest Data Forum 2017 - BigQuery, Looker And Big Data Analytics At Petabyt...Budapest Data Forum 2017 - BigQuery, Looker And Big Data Analytics At Petabyt...
Budapest Data Forum 2017 - BigQuery, Looker And Big Data Analytics At Petabyt...
 
Scaling to Infinity - Open Source meets Big Data
Scaling to Infinity - Open Source meets Big DataScaling to Infinity - Open Source meets Big Data
Scaling to Infinity - Open Source meets Big Data
 

Viewers also liked

Viewers also liked (20)

Bio movie chapter 37
Bio movie  chapter 37Bio movie  chapter 37
Bio movie chapter 37
 
Cover Letter and Resume
Cover Letter and ResumeCover Letter and Resume
Cover Letter and Resume
 
El 9 de febrero
El 9 de febreroEl 9 de febrero
El 9 de febrero
 
Abc
AbcAbc
Abc
 
Review for chapter 10
Review for chapter 10Review for chapter 10
Review for chapter 10
 
Gourmet Coffee Service Product Menu
Gourmet Coffee Service Product MenuGourmet Coffee Service Product Menu
Gourmet Coffee Service Product Menu
 
Spark with Elasticsearch - umd version 2014
Spark with Elasticsearch - umd version 2014Spark with Elasticsearch - umd version 2014
Spark with Elasticsearch - umd version 2014
 
Cover Letter and Resume
Cover Letter and ResumeCover Letter and Resume
Cover Letter and Resume
 
El 18 de enero
El 18 de eneroEl 18 de enero
El 18 de enero
 
Applying technology standards for adult basic education
Applying technology standards for adult basic educationApplying technology standards for adult basic education
Applying technology standards for adult basic education
 
El dos de marzo1
El dos de marzo1El dos de marzo1
El dos de marzo1
 
Play & Learn Toys Holiday 2011 Gift Guide
Play & Learn Toys Holiday 2011 Gift GuidePlay & Learn Toys Holiday 2011 Gift Guide
Play & Learn Toys Holiday 2011 Gift Guide
 
El 14 de diciembre
El 14 de diciembreEl 14 de diciembre
El 14 de diciembre
 
Grammar book Part 2
Grammar book Part 2Grammar book Part 2
Grammar book Part 2
 
Virtual child (infant & toddler)
Virtual child (infant & toddler)Virtual child (infant & toddler)
Virtual child (infant & toddler)
 
O voceiro do piñeiro manso
O voceiro do piñeiro mansoO voceiro do piñeiro manso
O voceiro do piñeiro manso
 
Presentation1
Presentation1Presentation1
Presentation1
 
Presentation 1
Presentation 1Presentation 1
Presentation 1
 
The 2012 Project: A Year of Opportunity for Women -- March Webinar
The 2012 Project: A Year of Opportunity for Women -- March WebinarThe 2012 Project: A Year of Opportunity for Women -- March Webinar
The 2012 Project: A Year of Opportunity for Women -- March Webinar
 
El 14 de febrero
El 14 de febreroEl 14 de febrero
El 14 de febrero
 

Similar to Google Big Query UDFs

How Google Does Big Data - DevNexus 2014
How Google Does Big Data - DevNexus 2014How Google Does Big Data - DevNexus 2014
How Google Does Big Data - DevNexus 2014
James Chittenden
 

Similar to Google Big Query UDFs (20)

Microsoft Graph: Connect to essential data every app needs
Microsoft Graph: Connect to essential data every app needsMicrosoft Graph: Connect to essential data every app needs
Microsoft Graph: Connect to essential data every app needs
 
Microsoft Graph: Connect to essential data every app needs
Microsoft Graph: Connect to essential data every app needsMicrosoft Graph: Connect to essential data every app needs
Microsoft Graph: Connect to essential data every app needs
 
The Big Picture and How to Get Started
The Big Picture and How to Get StartedThe Big Picture and How to Get Started
The Big Picture and How to Get Started
 
Google Apps Script: Accessing G Suite & other Google services with JavaScript
Google Apps Script: Accessing G Suite & other Google services with JavaScriptGoogle Apps Script: Accessing G Suite & other Google services with JavaScript
Google Apps Script: Accessing G Suite & other Google services with JavaScript
 
Exploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScriptExploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScript
 
Exploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScriptExploring Google (Cloud) APIs with Python & JavaScript
Exploring Google (Cloud) APIs with Python & JavaScript
 
Exploring Google (Cloud) APIs & Cloud Computing overview
Exploring Google (Cloud) APIs & Cloud Computing overviewExploring Google (Cloud) APIs & Cloud Computing overview
Exploring Google (Cloud) APIs & Cloud Computing overview
 
Google... more than just a cloud
Google... more than just a cloudGoogle... more than just a cloud
Google... more than just a cloud
 
Getting Started with DrupalGap
Getting Started with DrupalGapGetting Started with DrupalGap
Getting Started with DrupalGap
 
Image archive, analysis & report generation with Google Cloud
Image archive, analysis & report generation with Google CloudImage archive, analysis & report generation with Google Cloud
Image archive, analysis & report generation with Google Cloud
 
From Knowledge Graphs to AI-powered SEO: Using taxonomies, schemas and knowle...
From Knowledge Graphs to AI-powered SEO: Using taxonomies, schemas and knowle...From Knowledge Graphs to AI-powered SEO: Using taxonomies, schemas and knowle...
From Knowledge Graphs to AI-powered SEO: Using taxonomies, schemas and knowle...
 
Google Apps Script: The authentic{ated} playground [2015 Ed.]
Google Apps Script: The authentic{ated} playground [2015 Ed.]Google Apps Script: The authentic{ated} playground [2015 Ed.]
Google Apps Script: The authentic{ated} playground [2015 Ed.]
 
Google Devfest 2009 Argentina - Google and the Social Web
Google Devfest 2009 Argentina - Google and the Social WebGoogle Devfest 2009 Argentina - Google and the Social Web
Google Devfest 2009 Argentina - Google and the Social Web
 
Google Cloud lightning talk @MHacks
Google Cloud lightning talk @MHacksGoogle Cloud lightning talk @MHacks
Google Cloud lightning talk @MHacks
 
Google Developer Days Brazil 2009 - Google Social Web
Google Developer Days Brazil 2009 -  Google Social WebGoogle Developer Days Brazil 2009 -  Google Social Web
Google Developer Days Brazil 2009 - Google Social Web
 
How Google Does Big Data - DevNexus 2014
How Google Does Big Data - DevNexus 2014How Google Does Big Data - DevNexus 2014
How Google Does Big Data - DevNexus 2014
 
Interview with Developer Jose Luis Arenas regarding Google App Engine & Geosp...
Interview with Developer Jose Luis Arenas regarding Google App Engine & Geosp...Interview with Developer Jose Luis Arenas regarding Google App Engine & Geosp...
Interview with Developer Jose Luis Arenas regarding Google App Engine & Geosp...
 
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSTek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJS
 
Reactive Type safe Webcomponents with skateJS
Reactive Type safe Webcomponents with skateJSReactive Type safe Webcomponents with skateJS
Reactive Type safe Webcomponents with skateJS
 
Introduction to Cloud Computing with Google Cloud
Introduction to Cloud Computing with Google CloudIntroduction to Cloud Computing with Google Cloud
Introduction to Cloud Computing with Google Cloud
 

Recently uploaded

Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
amitlee9823
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
JoseMangaJr1
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
amitlee9823
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
amitlee9823
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
amitlee9823
 

Recently uploaded (20)

Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptx
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 

Google Big Query UDFs

  • 1. David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10 Google Big Query UDFs User Defined Functions @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10
  • 2. David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10 What is it Google Big Query’s UDF[1] are: ● Javascript based functions ● Extend the query to include “Map” like functionality (Mapreduce[2]) ● Can return a different schema to the input schema ● Runs the function against every row in the input dataset. See presentation[3] by Thomas Park[4] & Fillipe Hoffa[5] [1] https://cloud.google.com/bigquery/user-defined-functions [2] https://en.wikipedia.org/wiki/MapReduce [3] http://www.slideshare.net/BigDataSpain/thomas-park-hands-on-with-big-query-javascript-udfs-bigdataspain-2014 [4] no link found [5] https://twitter.com/felipehoffa
  • 3. David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10 Where can I find this UDF? Click here for UDFs
  • 4. David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10 Parts of a UDF A UDF has 3 parts: 1. Emitter Function ○ A function that accepts a row and function (emit) object and runs the function on parts of the row 2. Helper Function ○ This defines the a working function that can be called in the emitter function. Requires robust error handling. 3. Registration Function ○ Links the emitter function, input / output schemas and labels together
  • 5. David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10 Emitter Function function urlDecode(row, emit) { emit({title: functionHelper(row.title), requests: row.num_requests}); } Function Name row: defined by the input column schema row: defined by the output schema Helper Function(s) Input to helper function Element from row
  • 6. David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10 function functionHelper(s [,...]){ try { [...] return decodeURI(s); } catch (ex) { return s; } } Helper Function Function Name Must include one term Can add additional parameters if needed. Error Handling Very Important Can add additional statements if needed. Return a single term. Return a term on failure too !
  • 7. David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10 How do I test my code... Google have provided a beautiful page to test out your functions: http://storage.googleapis.com/bigquery-udf-test-tool/testtool.html This allows you to enter data (JSON), functions (emitter and helper) and see its output.
  • 8. David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10 Silly example... Input Data: [{a: "Enter your query data", b:"a", c:"_"}, {a: "here, as JSON,", b:",", c:"*"}, {a: "and click 'Evaluate' to run your function over the data rows!", b:"!", c:"^"}] User Defined Function: function(r, emit) { emit({len: r.a.length, fc: replaceHelper(r.a, r.b, r.c)}); } function replaceHelper(s, t, r){ try { return s.replace(t, r); } catch (ex) { return s; } }
  • 9. David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10 What about the Registration Function? Once the Emitter and Helper Functions have been tested, it is now time to apply them to an actual query... bigquery.defineFunction( 'urlDecode', ['title', 'num_requests'], [{name: 'title', type: 'string'}, {name: 'requests', type: 'integer'}], urlDecode ); Input column names JSON Output Schema Internal Function Reference Verb used to call function from SQL
  • 10. David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10 So how can I use it? Enter the 3 UDF parts in the UDF editor, and click the Query Editor SELECT requests, title FROM urlDecode( SELECT title, sum(requests) AS num_requests FROM [fh-bigquery:wikipedia.pagecounts_201504] WHERE language = 'fr' GROUP EACH BY title ) WHERE title LIKE '%ç%' ORDER BY requests DESC LIMIT 100
  • 11. David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10 Break this down for me... SELECT requests, title FROM urlDecode( SELECT title, sum(requests) AS num_requests FROM [fh-bigquery:wikipedia.pagecounts_201504] WHERE language = 'fr' GROUP EACH BY title ) WHERE title LIKE '%ç%' ORDER BY requests DESC LIMIT 100 Field names defined in the output schema SQL Label for the UDF Input columns SQL select statement each row of this query will have the UDF applied to them
  • 12. David Gloyn-Cox : @dreffed : https://ca.linkedin.com/in/dreffededited date: 2015-09-10 Questions: Any questions, please tweet me or +dreffed Thank You