![](/img/trans.png)
[英]GCP - Insert/Load Data to BigQuery from REST API using Google Cloud Function with Python
[英]How to stream data into Google Cloud BigQuery using Appengine with Python Script-flowing data from API?
我一直試圖從下面的api中流式傳輸數據,但收效甚微。
https://dev.socrata.com/foundry/data.cityofchicago.org/8v9j-bter
main.py腳本
#install main packages
!pip install sodapy
import pandas as pd
from sodapy import Socrata
from google.datalab import Context
#put into dataframe
client = Socrata("data.cityofchicago.org", None)
results = client.get("8v9j-bter", limit=2000)
results_df = pd.DataFrame.from_records(results)
#flow into BigQuery
results_df.to_gbq('chicago_traffic.demo_data', Context.default().project_id,
chunksize=2000, verbose=True, if_exists='append')
App.yaml腳本
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
cron.yaml腳本
cron:
- description: "append traffic data"
url: /.*
target: main
schedule: every 1 mins
retry_parameters:
min_backoff_seconds: 2.5
max_doublings: 5
requirements.txt
pandas==0.22.0
sodapy==1.4.6
datalab==1.1.2
google-api-python-client
使用app.yaml時,您將在App Engine標准環境中進行部署。 使用標准環境時,可以通過將任何內置的第三方庫添加到app.yaml文件中來使用它們,也可以按照此鏈接中的步驟使用任何其他第三方庫。
這里的問題是,正如我之前分享的鏈接中所述:
您可以使用不含C擴展名的純Python代碼的第三方庫,
pandas庫的部分代碼用C編寫( https://pandas.pydata.org/#library-highlights 如何解決pandas的導入錯誤? ),因此,要使用Pandas,您需要使用App Engine靈活的環境 。
您需要對文件進行一些修改才能運行部署。 單擊以下鏈接可以使文件適應靈活的環境:
!pip install sodapy使用了魔術命令!, 這是某些筆記本電腦環境中專有的命令。 您不能將該行添加到main.py文件中。 此操作(pip install sodapy)將在應用程序部署期間運行,因為您將sodapy == 1.4.6添加到了requirements.txt文件中。
不必添加Context.default()。project_id,您應該能夠僅將project_id添加為str。 在App Engine Flex中運行時,授權不應該成為問題。 如果要在本地運行,請記住使用具有正確權限的服務帳戶 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.