Advertisement
makispaiktis

Python-based SQL queries - Exercise 2

Jun 8th, 2023 (edited)
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. # ************************************************************************
  2. # ************************************************************************
  3. # Step 1
  4. # ************************************************************************
  5. # ************************************************************************
  6.  
  7. from google.cloud import bigquery
  8.  
  9. # Create a "Client" object
  10. client = bigquery.Client()
  11.  
  12. # Construct a reference to the "openaq" dataset
  13. dataset_ref = client.dataset("openaq", project="bigquery-public-data")
  14.  
  15. # API request - fetch the dataset
  16. dataset = client.get_dataset(dataset_ref)
  17.  
  18. # Construct a reference to the "global_air_quality" table
  19. table_ref = dataset_ref.table("global_air_quality")
  20.  
  21. # API request - fetch the table
  22. table = client.get_table(table_ref)
  23.  
  24. # Preview the first five lines of the "global_air_quality" table
  25. client.list_rows(table, max_results=5).to_dataframe()
  26.  
  27.  
  28.  
  29. # ************************************************************************
  30. # ************************************************************************
  31. # Step 2
  32. # ************************************************************************
  33. # ************************************************************************
  34.  
  35. # Query to select countries with units of "ppm"
  36. first_query = """
  37.                SELECT DISTINCT country
  38.                from `bigquery-public-data.openaq.global_air_quality`
  39.                WHERE unit = 'ppm'
  40.            """ # Your code goes here
  41.  
  42. # Set up the query (cancel the query if it would use too much of
  43. # your quota, with the limit set to 10 GB)
  44. safe_config = bigquery.QueryJobConfig(maximum_bytes_billed=10**10)
  45. first_query_job = client.query(first_query, job_config=safe_config)
  46.  
  47. # API request - run the query, and return a pandas DataFrame
  48. first_results = first_query_job.to_dataframe()
  49. # View top few rows of results
  50. print(first_results.head())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement