|
14 | 14 |
|
15 | 15 | """IPython Magics |
16 | 16 |
|
17 | | -To use these magics, you must first register them. Run the ``%load_ext`` magic |
18 | | -in a Jupyter notebook cell. |
19 | | -
|
20 | | -.. code:: |
21 | | -
|
22 | | - %load_ext google.cloud.bigquery |
23 | | -
|
24 | | -This makes the ``%%bigquery`` magic available. |
25 | | -
|
26 | 17 | .. function:: %%bigquery |
27 | 18 |
|
28 | 19 | IPython cell magic to run a query and display the result as a DataFrame |
|
85 | 76 | .. note:: |
86 | 77 | All queries run using this magic will run using the context |
87 | 78 | :attr:`~google.cloud.bigquery.magics.Context.credentials`. |
88 | | -
|
89 | | - Examples: |
90 | | - The following examples can be run in an IPython notebook after loading |
91 | | - the bigquery IPython extension (see ``In[1]``) and setting up |
92 | | - Application Default Credentials. |
93 | | -
|
94 | | - .. code-block:: none |
95 | | -
|
96 | | - In [1]: %load_ext google.cloud.bigquery |
97 | | -
|
98 | | - In [2]: %%bigquery |
99 | | - ...: SELECT name, SUM(number) as count |
100 | | - ...: FROM `bigquery-public-data.usa_names.usa_1910_current` |
101 | | - ...: GROUP BY name |
102 | | - ...: ORDER BY count DESC |
103 | | - ...: LIMIT 3 |
104 | | -
|
105 | | - Out[2]: name count |
106 | | - ...: ------------------- |
107 | | - ...: 0 James 4987296 |
108 | | - ...: 1 John 4866302 |
109 | | - ...: 2 Robert 4738204 |
110 | | -
|
111 | | - In [3]: %%bigquery df --project my-alternate-project --verbose |
112 | | - ...: SELECT name, SUM(number) as count |
113 | | - ...: FROM `bigquery-public-data.usa_names.usa_1910_current` |
114 | | - ...: WHERE gender = 'F' |
115 | | - ...: GROUP BY name |
116 | | - ...: ORDER BY count DESC |
117 | | - ...: LIMIT 3 |
118 | | - Executing query with job ID: bf633912-af2c-4780-b568-5d868058632b |
119 | | - Query executing: 2.61s |
120 | | - Query complete after 2.92s |
121 | | -
|
122 | | - In [4]: df |
123 | | -
|
124 | | - Out[4]: name count |
125 | | - ...: ---------------------- |
126 | | - ...: 0 Mary 3736239 |
127 | | - ...: 1 Patricia 1568495 |
128 | | - ...: 2 Elizabeth 1519946 |
129 | | -
|
130 | | - In [5]: %%bigquery --params {"num": 17} |
131 | | - ...: SELECT @num AS num |
132 | | -
|
133 | | - Out[5]: num |
134 | | - ...: ------- |
135 | | - ...: 0 17 |
136 | | -
|
137 | | - In [6]: params = {"num": 17} |
138 | | -
|
139 | | - In [7]: %%bigquery --params $params |
140 | | - ...: SELECT @num AS num |
141 | | -
|
142 | | - Out[7]: num |
143 | | - ...: ------- |
144 | | - ...: 0 17 |
145 | 79 | """ |
146 | 80 |
|
147 | 81 | from __future__ import print_function |
|
0 commit comments