Thursday, December 24, 2015

Fusion Table API Insert Data Python

Me and a buddy have been hacking at this Google Fusion API for a couple days now trying to figure it out. We finally had a break through. He sent me his sample code with the authentication piece and the SELECT statement and I started trying an INSERT. After about 10 rounds of fail finally got something to work and wanted to post it. We struggled to find a simple sample on how to do this, I am sure there is a better way, but at least its working.

Python Modules Needed


You will need to install some Python modules if you have not authenticated to the Google APIs before. Here is a list of modules I installed. 

  • requests
  • urllib3
  • apiclient
  • gspread
  • PyOpenSSL
  • oauth2client
  • google-api-python-client (used easy_install)
Not all of these are needed for the script. I am working from a new laptop so I had install them fresh. All of these were installed using pip except the Google API Python client. For some reason pip did not install that so we had to use easy_install.


Authentication


We used the same method detailed in this earlier post using the gspread module. You will want to create the JSON file with your authentication key in it so you can authenticate to the Fusion Table API.

Make sure you grant your client email address access to edit the Fusion table. You can do this using the Fusion Table Share feature and then add the email from the JSON file you downloaded when  you built your key   XXXXXXXXXXXXXXX@developer.gserviceaccount.com



Script


I am using the USGS Earthquake JSON feed I blogged about earlier today to import Earthquake data in to a Fusion Table. Here is the script.


Basically we just setup a loop to parse the Earthquake JSON data and on each loop we execute an INSERT statement in Fusion. Again there is probably a better way than the line by line method detailed here. But again at least we can import data into Fusion. This is not going to be practical for thousands of rows but is fine for our purpose here.

0 comments:

Post a Comment