Checkmark

BigQuery Tutorial Code Snip 1

Jul 2nd, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. // Replace the following with your Google Developer Console project number
  2. var projectNumber = 'xxxxxxxxxxxx';
  3.  
  4. var ss = SpreadsheetApp.getActiveSpreadsheet();
  5.  
  6. function onOpen() {
  7. var menuEntries = [ {name: 'Run Query', functionName: 'runQuery'} ];
  8. ss.addMenu('BigQuery', menuEntries);
  9. }
  10.  
  11. function runQuery() {
  12. var sheet = SpreadsheetApp.getActiveSheet();
  13.  
  14. var sql = 'select TOP(word, 10), COUNT(*) as word_count from publicdata:samples.shakespeare WHERE LENGTH(word) > 12;';
  15.  
  16. var queryResults;
  17.  
  18. // Inserts a Query Job with an optional timeoutMs parameter.
  19. try {
  20. queryResults = BigQuery.Jobs.query(projectNumber, sql, {'timeoutMs':10000});
  21. }
  22. catch (err) {
  23. Logger.log(err);
  24. Browser.msgBox(err);
  25. return;
  26. }
  27.  
  28. // If the query results are not complete when the optional timeout is reached,
  29. // periodically poll the BigQuery API for the results manually using the query job id.
  30. while (queryResults.getJobComplete() == false) {
  31. try {
  32. queryResults = BigQuery.Jobs.getQueryResults(projectNumber, queryResults.getJobReference().getJobId());
  33. // If the Job is still not complete, sleep script for
  34. // 3 seconds before checking result status again
  35. if (queryResults.getJobComplete() == false) {
  36. Utilities.sleep(3000);
  37. }
  38. }
  39. catch (err) {
  40. Logger.log(err);
  41. Browser.msgBox(err);
  42. return;
  43. }
  44. }
  45.  
  46. // Update the amount of results
  47. var resultCount = queryResults.getTotalRows();
  48. var resultSchema = queryResults.getSchema();
  49. }
Advertisement
Add Comment
Please, Sign In to add comment