Created
March 17, 2019 05:59
-
-
Save gonzalovazquez/b0e0642a0b5f1da8e51087abcef02f52 to your computer and use it in GitHub Desktop.
Example read and write to BigQuery using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.cloud import bigquery | |
import datetime | |
def add_items_to_bigquery(name, description, in_cloud=True): | |
print("Adding items to BigQuery") | |
# Instantiates a client | |
bigquery_client = bigquery.Client() | |
# Prepares a reference to the dataset | |
dataset_ref = bigquery_client.dataset('universitas_library') | |
table_ref = dataset_ref.table('projects') | |
table = bigquery_client.get_table(table_ref) # API call | |
new_date = datetime.datetime.now() | |
rows_to_insert = [ | |
(new_date, name, description, True) | |
] | |
print("Adding the following item") | |
print(rows_to_insert) | |
errors = bigquery_client.insert_rows(table, rows_to_insert) # API request | |
print(errors) | |
assert errors == [] | |
add_items_to_bigquery('Test1', 'some description') | |
def fetch_items_from_bigquery(): | |
bigquery_client = bigquery.Client() | |
query = ( | |
''' | |
WITH MyTable AS ( | |
SELECT | |
* | |
FROM | |
`{}.{}.{}` | |
) | |
SELECT TO_JSON_STRING(t) AS json | |
FROM MyTable AS t; | |
''' | |
.format('memento-mori-universitas', 'universitas_library', 'projects')) | |
try: | |
query_job = bigquery_client.query(query) | |
data = query_job.result() | |
print(data) | |
# is_exist = len(data) >= 1 | |
print('Data exists') | |
return data | |
except Exception as e: | |
print("Error") | |
print(e) | |
return False | |
fetch_items_from_bigquery() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment