Guest User

Untitled

a guest
Jun 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. import urllib
  2. import json
  3. import urllib.request
  4. import requests
  5.  
  6. from google.cloud import bigquery
  7.  
  8. def createdataset():
  9.  
  10. bigquery_client = bigquery.Client()
  11.  
  12. dataset_id = 'mydataset'
  13.  
  14. dataset_ref = bigquery_client.dataset(dataset_id)
  15. dataset = bigquery.Dataset(dataset_ref)
  16.  
  17. dataset = bigquery_client.create_dataset(dataset)
  18.  
  19. print('Dataset {} created.'.format(dataset.dataset_id))
  20.  
  21. def main():
  22. fetch_traffic_data()
  23. load_json("data.json")
  24.  
  25. def fetch_traffic_data():
  26.  
  27. URL = "https://data.cityofchicago.org/resource/8v9j-bter.json"
  28.  
  29.  
  30. def send_to_big_query(data):
  31. BIG_QUERY_URL = "https://www.googleapis.com/bigquery/v2/projects/bigquery-test-205619/datasets"
  32. for d in data:
  33.  
  34. print(d)
  35. response = requests.post(BIG_QUERY_URL, data=json.dumps(d))
  36. print(response.text)
  37. #post each dictionary (JSON data point) to big query
  38.  
  39. def load_json(source_file_name):
  40. bigquery_client = bigquery.Client()
  41. dataset = bigquery_client.dataset('mydataset')
  42. table = dataset.table('mytable')
  43.  
  44. with open(source_file_name, 'rb') as source_file:
  45.  
  46. job_config = bigquery.LoadJobConfig()
  47. job_config.source_format = 'NEWLINE_DELIMITED_JSON'
  48. job = bigquery_client.load_table_from_file(
  49. source_file, table, job_config=job_config)
  50.  
  51.  
  52. if __name__ == "__main__":
  53. main()
Add Comment
Please, Sign In to add comment