Query a public dataset with the BigQuery Node.js client library

Query a public dataset with the BigQuery Node.js client library

Learn how to:
  1. Activate Cloud Shell in a Google Cloud project.
  2. Open the Cloud Shell Editor.
  3. Prepare files for queries.
  4. Query a public dataset in BigQuery.
  5. Clean up.

Estimated time to complete:

Click Startto begin.

Activate Cloud Shell in a Google Cloud project

  1. If you do not enable billing for a project, you automatically work in the BigQuery sandbox. The BigQuery sandbox lets you learn BigQuery with a limited set of BigQuery features at no charge. If you do not plan to use your project beyond this document, we recommend that you use the BigQuery sandbox.

  2. Click Activate Cloud Shell. Show me

To learn how to open the Cloud Shell Editor, click Next.

Open the Cloud Shell Editor

  1. In Cloud Shell, create a new Node.js project and file:

     mkdir  
    bigquery-node-quickstart  
     \ 
     && 
    touch  
     \ 
      
    bigquery-node-quickstart/app.js 
    

    This command creates a Node.js project that's named bigquery-node-quickstart and a file that's named app.js .

  2. Open the Cloud Shell Editor:

     cloudshell  
    workspace  
    bigquery-node-quickstart 
    

To learn how to prepare files for queries, click Next.

Prepare files for queries

  1. To open a terminal in the Cloud Shell Editor, click Open Terminal.

  2. Open your project directory:

      cd 
      
    bigquery-node-quickstart 
    
  3. Install the BigQuery client library for Node.js:

     npm  
    install  
    @google-cloud/bigquery 
    

    The output is similar to the following:

     added 63 packages in 2s 
    

To learn how to query a public dataset in BigQuery, click Next.

Query a public dataset in BigQuery

  1. Click Open Editor.

  2. In the Explorerpane, locate your BIGQUERY-NODE-QUICKSTART project.

  3. Click the app.js file to open it.

  4. To create a query against the bigquery-public-data.stackoverflow dataset that returns the top 10 most viewed Stack Overflow pages and their view counts, copy the following code into the app.js file:

      // Import the Google Cloud client library 
     const 
      
     { 
     BigQuery 
     } 
      
     = 
      
     require 
     ( 
     ' @google-cloud/bigquery 
    ' 
     ); 
     async 
      
     function 
      
     queryStackOverflow 
     () 
      
     { 
      
     // Queries a public Stack Overflow dataset. 
      
     // Create a client 
      
     const 
      
     bigqueryClient 
      
     = 
      
     new 
      
      BigQuery 
     
     (); 
      
     // The SQL query to run 
      
     const 
      
     sqlQuery 
      
     = 
      
     `SELECT 
     CONCAT( 
     'https://stackoverflow.com/questions/', 
     CAST(id as STRING)) as url, 
     view_count 
     FROM \`bigquery-public-data.stackoverflow.posts_questions\` 
     WHERE tags like '%google-bigquery%' 
     ORDER BY view_count DESC 
     LIMIT 10` 
     ; 
      
     const 
      
     options 
      
     = 
      
     { 
      
     query 
     : 
      
     sqlQuery 
     , 
      
     // Location must match that of the dataset(s) referenced in the query. 
      
     location 
     : 
      
     'US' 
     , 
      
     }; 
      
     // Run the query 
      
     const 
      
     [ 
     rows 
     ] 
      
     = 
      
     await 
      
     bigqueryClient 
     . 
     query 
     ( 
     options 
     ); 
      
     console 
     . 
     log 
     ( 
     'Query Results:' 
     ); 
      
     rows 
     . 
     forEach 
     ( 
     row 
      
     = 
    >  
     { 
      
     const 
      
     url 
      
     = 
      
     row 
     [ 
     'url' 
     ]; 
      
     const 
      
     viewCount 
      
     = 
      
     row 
     [ 
     'view_count' 
     ]; 
      
     console 
     . 
     log 
     ( 
     `url: 
     ${ 
     url 
     } 
     , 
     ${ 
     viewCount 
     } 
     views` 
     ); 
      
     }); 
     } 
     queryStackOverflow 
     (); 
     
    
  • Click Open Terminal.

  • In the terminal, run the app.js script. If you are prompted to authorize Cloud Shell and agree to the terms, click Authorize.

     node  
    app.js 
    

    The result is similar to the following:

     Query Results:
    url: https://stackoverflow.com/questions/35159967, 170023 views
    url: https://stackoverflow.com/questions/22879669, 142581 views
    url: https://stackoverflow.com/questions/10604135, 132406 views
    url: https://stackoverflow.com/questions/44564887, 128781 views
    url: https://stackoverflow.com/questions/27060396, 127008 views
    url: https://stackoverflow.com/questions/12482637, 120766 views
    url: https://stackoverflow.com/questions/20673986, 115720 views
    url: https://stackoverflow.com/questions/39109817, 108368 views
    url: https://stackoverflow.com/questions/11057219, 105175 views
    url: https://stackoverflow.com/questions/43195143, 101878 views 
    
  • You have successfully queried a public dataset with the BigQuery Node.js client library.

    To avoid incurring charges to your account and learn about next steps, click Next.

    Next steps

    Keep the resources that you created and do more with BigQuery, or clean up to avoid billing charges.

    Do more with BigQuery

    Clean up

    To avoid incurring charges to your Google Cloud account, either delete your Google Cloud project, or delete the resources that you created in this walkthrough.

    Delete the project

    If you created a new project to learn about BigQuery and you no longer need the project, delete it . Be aware that deleting a project deletes everything in the project and custom project IDs are lost.

    Delete the resources

    If you used an existing project, delete the bigquery-node-quickstart folder that you created:

    1. In Cloud Shell, move up a directory:

        cd 
        
      .. 
      
    2. Delete the resources that you created:

       rm  
      -R  
      bigquery-node-quickstart 
      

      The -R flag deletes all assets in a folder.

    Create a Mobile Website
    View Site in Mobile | Classic
    Share by: