Skip to main content

Unofficial IPython magic command for bigquery

Project description

another-bigquery-magic

Unofficial bigquery magic Command for IPython notebook

Installation

# from pypi
$ pip install another-bigquery-magic

# alternatively, from github
$ git clone https://github.com/kota7/another-bigquery-magic.git --depth 1
$ pip install -U ./another-bigquery-magic

Usage

# Set the project ID
project_id = "<google-cloud-project-id>"
!gcloud config set project {project_id}
# If you are authenticated to the google cloud already, skip this cell.
# Otherwise, authenticate with your choice of method.

# Example 1. Authentication on colab
from google.colab import auth
auth.authenticate_user()

# Example 2. Authentication by user log-in
# Note: to access external table with google drive, we also need "https://www.googleapis.com/auth/drive" in the scope
!gcloud auth application-default login --scopes="https://www.googleapis.com/auth/bigquery"

# Example 3. Authentication with a local json file
jsonfile = "<json-file-path>"
%config BigqueryMagic.localjson = jsonfile
# Load the bq magic command
%load_ext bq

# %bq magic command runs the query and returns the pandas data frame
%bq SELECT 1 AS test
Start query at 2024-01-12 15:31:07.286991
End query at 2024-01-12 15:31:10.047083 (Execution time: 0:00:02.760092, Processed: 0.0 GB)
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
test
0 1
%%bq
/* Cell magic is also defined */
SELECT
  1 AS x,
  2 AS y
Start query at 2024-01-12 15:31:10.848227
End query at 2024-01-12 15:31:13.636390 (Execution time: 0:00:02.788163, Processed: 0.0 GB)
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
x y
0 1 2
# Change the limit to the number of rows to get (default is 50000 rows)
%config BigqueryMagic.autolimit = 2  # at most two rows to return

q = """
SELECT 1 AS x
UNION ALL
SELECT 2 AS x
UNION ALL
SELECT 3 AS x
"""
%bq {q}

# Reset to a reasonable number
%config BigqueryMagic.autolimit = 10000
Start query at 2024-01-12 15:31:13.948559
End query at 2024-01-12 15:31:16.773933 (Execution time: 0:00:02.825374, Processed: 0.0 GB)
Result is truncated at the row 2 of 3
# Control the amount of messages
%config BigqueryMagic.showbytes = False
x = %bq SELECT false AS showbytes
display(x)

%config BigqueryMagic.showbytes = True
%config BigqueryMagic.showtime = False
x = %bq SELECT true AS showbytes, false AS showtime
display(x)

%config BigqueryMagic.showtime = True
%config BigqueryMagic.quiet = True
x = %bq SELECT true AS showbytes, true AS showtime, true AS quiet
display(x)
Start query at 2024-01-12 15:31:17.086035
End query at 2024-01-12 15:31:19.910281 (Execution time: 0:00:02.824246)
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
showbytes
0 False
Processed: 0.0 GB
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
showbytes showtime
0 True False
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
showbytes showtime quiet
0 True True True


          

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

another-bigquery-magic-0.1.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

another_bigquery_magic-0.1.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file another-bigquery-magic-0.1.2.tar.gz.

File metadata

File hashes

Hashes for another-bigquery-magic-0.1.2.tar.gz
Algorithm Hash digest
SHA256 204f1cb099d784762151578fbc0c30102b4895afac0875cf060ad13daba49557
MD5 f36eb36bd2b1c623fefeafb74f53050f
BLAKE2b-256 afd3fe30e01cb89cb76eccc1b3f53f8960e1e62f3f7b350dbcba9d9269d19b15

See more details on using hashes here.

File details

Details for the file another_bigquery_magic-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for another_bigquery_magic-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d18603aa7373a2b59e1d9b98e838bc0913d3778be7835501a8e38f455ecc55d8
MD5 21e087a0f0866a4b8818d67b3221528f
BLAKE2b-256 69acf455c09fd4729820afd7dabc0a36cf7636b5bf44d9d1a9be742a24744eef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page