Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib
- import json
- import urllib.request
- import requests
- from google.cloud import bigquery
- def createdataset():
- bigquery_client = bigquery.Client()
- dataset_id = 'mydataset'
- dataset_ref = bigquery_client.dataset(dataset_id)
- dataset = bigquery.Dataset(dataset_ref)
- dataset = bigquery_client.create_dataset(dataset)
- print('Dataset {} created.'.format(dataset.dataset_id))
- def main():
- fetch_traffic_data()
- load_json("data.json")
- def fetch_traffic_data():
- URL = "https://data.cityofchicago.org/resource/8v9j-bter.json"
- def send_to_big_query(data):
- BIG_QUERY_URL = "https://www.googleapis.com/bigquery/v2/projects/bigquery-test-205619/datasets"
- for d in data:
- print(d)
- response = requests.post(BIG_QUERY_URL, data=json.dumps(d))
- print(response.text)
- #post each dictionary (JSON data point) to big query
- def load_json(source_file_name):
- bigquery_client = bigquery.Client()
- dataset = bigquery_client.dataset('mydataset')
- table = dataset.table('mytable')
- with open(source_file_name, 'rb') as source_file:
- job_config = bigquery.LoadJobConfig()
- job_config.source_format = 'NEWLINE_DELIMITED_JSON'
- job = bigquery_client.load_table_from_file(
- source_file, table, job_config=job_config)
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment