|
1 | 1 | { |
2 | 2 | "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "91edcf7b", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# %%bqsql cell magics\n", |
| 9 | + "\n", |
| 10 | + "The BigQuery DataFrames (aka BigFrames) package provides a `%%bqsql` cell magics for Jupyter environments.\n", |
| 11 | + "\n", |
| 12 | + "To use it, first activate the extension:" |
| 13 | + ] |
| 14 | + }, |
3 | 15 | { |
4 | 16 | "cell_type": "code", |
5 | | - "execution_count": 3, |
| 17 | + "execution_count": 1, |
6 | 18 | "id": "98cd0489", |
7 | 19 | "metadata": {}, |
8 | | - "outputs": [ |
9 | | - { |
10 | | - "name": "stdout", |
11 | | - "output_type": "stream", |
12 | | - "text": [ |
13 | | - "The bigframes extension is already loaded. To reload it, use:\n", |
14 | | - " %reload_ext bigframes\n" |
15 | | - ] |
16 | | - } |
17 | | - ], |
| 20 | + "outputs": [], |
18 | 21 | "source": [ |
19 | 22 | "%load_ext bigframes" |
20 | 23 | ] |
21 | 24 | }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "id": "f18fdc63", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "Now, use the magics by including SQL in the body." |
| 31 | + ] |
| 32 | + }, |
22 | 33 | { |
23 | 34 | "cell_type": "code", |
24 | | - "execution_count": 4, |
| 35 | + "execution_count": 2, |
25 | 36 | "id": "269c5862", |
26 | 37 | "metadata": {}, |
27 | 38 | "outputs": [ |
28 | 39 | { |
29 | 40 | "data": { |
30 | 41 | "text/html": [ |
31 | 42 | "\n", |
32 | | - " Query processed 0 Bytes. [<a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:job_HiiqKoJVBj9_EYqkrjVsmzkVZCb5&page=queryresults\">Job bigframes-dev:US.job_HiiqKoJVBj9_EYqkrjVsmzkVZCb5 details</a>]\n", |
| 43 | + " Query processed 0 Bytes. [<a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:job_UVe7FsupxF3CbYuLcLT7fpw9dozg&page=queryresults\">Job bigframes-dev:US.job_UVe7FsupxF3CbYuLcLT7fpw9dozg details</a>]\n", |
33 | 44 | " " |
34 | 45 | ], |
35 | 46 | "text/plain": [ |
|
42 | 53 | { |
43 | 54 | "data": { |
44 | 55 | "application/vnd.jupyter.widget-view+json": { |
45 | | - "model_id": "c6fbeec6762f4a3fa0c9d3e71ca4c00d", |
| 56 | + "model_id": "1e2fb7b019754d31b11323a054f97f47", |
46 | 57 | "version_major": 2, |
47 | 58 | "version_minor": 1 |
48 | 59 | }, |
|
184 | 195 | "SELECT * FROM `bigquery-public-data.usa_names.usa_1910_2013`" |
185 | 196 | ] |
186 | 197 | }, |
| 198 | + { |
| 199 | + "cell_type": "markdown", |
| 200 | + "id": "8771e10f", |
| 201 | + "metadata": {}, |
| 202 | + "source": [ |
| 203 | + "The output DataFrame can be saved to a variable." |
| 204 | + ] |
| 205 | + }, |
187 | 206 | { |
188 | 207 | "cell_type": "code", |
189 | | - "execution_count": null, |
| 208 | + "execution_count": 3, |
190 | 209 | "id": "30bb6327", |
191 | 210 | "metadata": {}, |
| 211 | + "outputs": [ |
| 212 | + { |
| 213 | + "data": { |
| 214 | + "text/html": [ |
| 215 | + "\n", |
| 216 | + " Query processed 0 Bytes. [<a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:c142adf3-cd95-42da-bbdc-c176b36b934f&page=queryresults\">Job bigframes-dev:US.c142adf3-cd95-42da-bbdc-c176b36b934f details</a>]\n", |
| 217 | + " " |
| 218 | + ], |
| 219 | + "text/plain": [ |
| 220 | + "<IPython.core.display.HTML object>" |
| 221 | + ] |
| 222 | + }, |
| 223 | + "metadata": {}, |
| 224 | + "output_type": "display_data" |
| 225 | + } |
| 226 | + ], |
| 227 | + "source": [ |
| 228 | + "%%bqsql mydf\n", |
| 229 | + "SELECT * FROM `bigquery-public-data.usa_names.usa_1910_2013`" |
| 230 | + ] |
| 231 | + }, |
| 232 | + { |
| 233 | + "cell_type": "markdown", |
| 234 | + "id": "533e2e9e", |
| 235 | + "metadata": {}, |
| 236 | + "source": [ |
| 237 | + "You can chain cells together using format strings. DataFrame objects are automatically turned into table expressions." |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "code", |
| 242 | + "execution_count": 4, |
| 243 | + "id": "6a8a8123", |
| 244 | + "metadata": {}, |
| 245 | + "outputs": [ |
| 246 | + { |
| 247 | + "data": { |
| 248 | + "text/html": [ |
| 249 | + "\n", |
| 250 | + " Query processed 88.1 MB in a moment of slot time.\n", |
| 251 | + " " |
| 252 | + ], |
| 253 | + "text/plain": [ |
| 254 | + "<IPython.core.display.HTML object>" |
| 255 | + ] |
| 256 | + }, |
| 257 | + "metadata": {}, |
| 258 | + "output_type": "display_data" |
| 259 | + }, |
| 260 | + { |
| 261 | + "data": { |
| 262 | + "application/vnd.jupyter.widget-view+json": { |
| 263 | + "model_id": "c4889de9296440428de90defb5c58070", |
| 264 | + "version_major": 2, |
| 265 | + "version_minor": 1 |
| 266 | + }, |
| 267 | + "text/html": [ |
| 268 | + "<div>\n", |
| 269 | + "<style scoped>\n", |
| 270 | + " .dataframe tbody tr th:only-of-type {\n", |
| 271 | + " vertical-align: middle;\n", |
| 272 | + " }\n", |
| 273 | + "\n", |
| 274 | + " .dataframe tbody tr th {\n", |
| 275 | + " vertical-align: top;\n", |
| 276 | + " }\n", |
| 277 | + "\n", |
| 278 | + " .dataframe thead th {\n", |
| 279 | + " text-align: right;\n", |
| 280 | + " }\n", |
| 281 | + "</style>\n", |
| 282 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 283 | + " <thead>\n", |
| 284 | + " <tr style=\"text-align: right;\">\n", |
| 285 | + " <th></th>\n", |
| 286 | + " <th>total_count</th>\n", |
| 287 | + " <th>name</th>\n", |
| 288 | + " </tr>\n", |
| 289 | + " </thead>\n", |
| 290 | + " <tbody>\n", |
| 291 | + " <tr>\n", |
| 292 | + " <th>0</th>\n", |
| 293 | + " <td>304036</td>\n", |
| 294 | + " <td>Tracy</td>\n", |
| 295 | + " </tr>\n", |
| 296 | + " <tr>\n", |
| 297 | + " <th>1</th>\n", |
| 298 | + " <td>293876</td>\n", |
| 299 | + " <td>Travis</td>\n", |
| 300 | + " </tr>\n", |
| 301 | + " <tr>\n", |
| 302 | + " <th>2</th>\n", |
| 303 | + " <td>203784</td>\n", |
| 304 | + " <td>Troy</td>\n", |
| 305 | + " </tr>\n", |
| 306 | + " <tr>\n", |
| 307 | + " <th>3</th>\n", |
| 308 | + " <td>150127</td>\n", |
| 309 | + " <td>Trevor</td>\n", |
| 310 | + " </tr>\n", |
| 311 | + " <tr>\n", |
| 312 | + " <th>4</th>\n", |
| 313 | + " <td>96397</td>\n", |
| 314 | + " <td>Tristan</td>\n", |
| 315 | + " </tr>\n", |
| 316 | + " <tr>\n", |
| 317 | + " <th>5</th>\n", |
| 318 | + " <td>89996</td>\n", |
| 319 | + " <td>Tracey</td>\n", |
| 320 | + " </tr>\n", |
| 321 | + " <tr>\n", |
| 322 | + " <th>6</th>\n", |
| 323 | + " <td>65546</td>\n", |
| 324 | + " <td>Trinity</td>\n", |
| 325 | + " </tr>\n", |
| 326 | + " <tr>\n", |
| 327 | + " <th>7</th>\n", |
| 328 | + " <td>50112</td>\n", |
| 329 | + " <td>Traci</td>\n", |
| 330 | + " </tr>\n", |
| 331 | + " <tr>\n", |
| 332 | + " <th>8</th>\n", |
| 333 | + " <td>49657</td>\n", |
| 334 | + " <td>Trenton</td>\n", |
| 335 | + " </tr>\n", |
| 336 | + " <tr>\n", |
| 337 | + " <th>9</th>\n", |
| 338 | + " <td>45692</td>\n", |
| 339 | + " <td>Trent</td>\n", |
| 340 | + " </tr>\n", |
| 341 | + " </tbody>\n", |
| 342 | + "</table>\n", |
| 343 | + "<p>10 rows × 2 columns</p>\n", |
| 344 | + "</div>[238 rows x 2 columns in total]" |
| 345 | + ], |
| 346 | + "text/plain": [ |
| 347 | + " total_count name\n", |
| 348 | + "0 304036 Tracy\n", |
| 349 | + "1 293876 Travis\n", |
| 350 | + "2 203784 Troy\n", |
| 351 | + "3 150127 Trevor\n", |
| 352 | + "4 96397 Tristan\n", |
| 353 | + "5 89996 Tracey\n", |
| 354 | + "6 65546 Trinity\n", |
| 355 | + "7 50112 Traci\n", |
| 356 | + "8 49657 Trenton\n", |
| 357 | + "9 45692 Trent\n", |
| 358 | + "...\n", |
| 359 | + "\n", |
| 360 | + "[238 rows x 2 columns]" |
| 361 | + ] |
| 362 | + }, |
| 363 | + "metadata": {}, |
| 364 | + "output_type": "display_data" |
| 365 | + } |
| 366 | + ], |
| 367 | + "source": [ |
| 368 | + "%%bqsql\n", |
| 369 | + "SELECT sum(number) as total_count, name\n", |
| 370 | + "FROM {mydf}\n", |
| 371 | + "WHERE name LIKE 'Tr%'\n", |
| 372 | + "GROUP BY name\n", |
| 373 | + "ORDER BY total_count DESC" |
| 374 | + ] |
| 375 | + }, |
| 376 | + { |
| 377 | + "cell_type": "code", |
| 378 | + "execution_count": null, |
| 379 | + "id": "d2a17078", |
| 380 | + "metadata": {}, |
192 | 381 | "outputs": [], |
193 | 382 | "source": [] |
194 | 383 | } |
|
0 commit comments