Skip to content

Commit feab922

Browse files
committed
update pydata_google_auth snippet
1 parent bc39e47 commit feab922

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

2020/bigquery-python/user-credentials-magics.ipynb

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"metadata": {},
77
"outputs": [],
88
"source": [
9+
"# Install the google-cloud-bigquery and pydata-google-auth packages\n",
10+
"# to use this code snippet.\n",
11+
"import google.cloud.bigquery\n",
912
"import google.cloud.bigquery.magics\n",
1013
"import pydata_google_auth"
1114
]
@@ -16,7 +19,10 @@
1619
"metadata": {},
1720
"outputs": [],
1821
"source": [
19-
"credentials = pydata_google_auth.get_user_credentials([\"https://www.googleapis.com/auth/cloud-platform\"])"
22+
"# In the cell output, open the link to authenticate with your\n",
23+
"# Google credentials.\n",
24+
"credentials = pydata_google_auth.get_user_credentials([\"https://www.googleapis.com/auth/cloud-platform\"])\n",
25+
"project_id = \"my-project-id\""
2026
]
2127
},
2228
{
@@ -25,7 +31,8 @@
2531
"metadata": {},
2632
"outputs": [],
2733
"source": [
28-
"google.cloud.bigquery.magics.context.project = \"my-project-id\"\n",
34+
"# Configure the magics to use your credentials and project.\n",
35+
"google.cloud.bigquery.magics.context.project = project_id\n",
2936
"google.cloud.bigquery.magics.context.credentials = credentials"
3037
]
3138
},
@@ -52,6 +59,41 @@
5259
"LIMIT 3"
5360
]
5461
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": null,
65+
"metadata": {},
66+
"outputs": [],
67+
"source": [
68+
"# Configure a BigQuery client to use your credentials and project.\n",
69+
"bqclient = google.cloud.bigquery.Client()"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": null,
75+
"metadata": {},
76+
"outputs": [],
77+
"source": [
78+
"query_job = bqclient.query(\n",
79+
"\"\"\"SELECT name, SUM(number) as count\n",
80+
"FROM `bigquery-public-data.usa_names.usa_1910_current`\n",
81+
"GROUP BY name\n",
82+
"ORDER BY count DESC\n",
83+
"LIMIT 3\n",
84+
"\"\"\")\n",
85+
"query_job.result()"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"metadata": {},
92+
"outputs": [],
93+
"source": [
94+
"query_job.to_dataframe()"
95+
]
96+
},
5597
{
5698
"cell_type": "code",
5799
"execution_count": null,
@@ -62,7 +104,7 @@
62104
],
63105
"metadata": {
64106
"kernelspec": {
65-
"display_name": "Python 3",
107+
"display_name": "Python 3 (ipykernel)",
66108
"language": "python",
67109
"name": "python3"
68110
},
@@ -76,7 +118,7 @@
76118
"name": "python",
77119
"nbconvert_exporter": "python",
78120
"pygments_lexer": "ipython3",
79-
"version": "3.7.3"
121+
"version": "3.9.5"
80122
}
81123
},
82124
"nbformat": 4,

0 commit comments

Comments
 (0)