-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno1_fetch_data.py
65 lines (46 loc) · 1.99 KB
/
no1_fetch_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# import libraries
from google.cloud import bigquery
# create a client object
client = bigquery.Client()
### --------------------------------------------------------------------------
### Fetch data from bigquery-public-data
### --------------------------------------------------------------------------
# Construct a reference to the "hacker_news" dataset
dataset_ref = client.dataset("hacker_news", project="bigquery-public-data")
# API request - fetch the dataset
dataset = client.get_dataset(dataset_ref)
# List all the tables in the "hacker_news" dataset
tables = list(client.list_tables(dataset))
# print list of tables
for table in tables:
print(table.table_id)
# construct a reference to the "full" table
table_ref = dataset_ref.table("full")
# API request - fetch the table
table = client.get_table(table_ref)
# Print information on all the columns in the "full" table in the "hacker_news" dataset
table.schema
# Preview the first five lines of the "full" table
client.list_rows(table, max_results = 5).to_dataframe()
### --------------------------------------------------------------------------
### SELECT FROM
### --------------------------------------------------------------------------
# Construct a reference to the "openaq" dataset
dataset_ref = client.dataset("openaq", project="bigquery-public-data")
# https://openaq.org/#/?_k=esd1sb
# API request - fetch the dataset
dataset = client.get_dataset(dataset_ref)
# construct a reference to the "global_air_quality" table
table_ref = dataset_ref.table("global_air_quality")
# API request - fetch the table
table = client.get_table(table_ref)
# Print information on all the columns in the "global_air_quality" table in the "openaq" dataset
table.schema
# Print information on all the columns in the "global_air_quality" table in the "openaq" dataset
client.list_rows(table, max_results = 5).to_dataframe()
# query
query = """
SELECT city
FROM `bigquery-public-data.openaq.global_air_quality`
WHERE country = 'US'
"""