如何使用Appengine和Python脚本从API流数据到googlecloudbigquery?

2024-09-26 22:51:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试从下面的api流式传输数据,但收效甚微。在

https://dev.socrata.com/foundry/data.cityofchicago.org/8v9j-bter

  • 用shell脚本实现datalab笔记本的自动化太难了
  • 用气流来协调也太费劲了
  • 下面的代码在datalab笔记本中工作,但不知道“Context”魔术命令是否可以在常规脚本中工作。在
  • 这在阿彭金有可能吗?在
  • 有人能提供正确运行所需的其他脚本的指导吗?在
  • 代码缩进可能关闭

在主.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')

在应用程序yaml脚本

^{pr2}$

在克罗恩·亚姆脚本

  cron:
     - description: "append traffic data"
      url: /.*
      target: main
      schedule: every 1 mins 
  retry_parameters:
     min_backoff_seconds: 2.5
     max_doublings: 5

在要求.txt在

   pandas==0.22.0
   sodapy==1.4.6
   datalab==1.1.2
   google-api-python-client

Tags: 代码fromorgimport脚本clientapidata
1条回答
网友
1楼 · 发布于 2024-09-26 22:51:37

和应用程序yaml您正在使用您将在App Engine Standard Enviroment中部署的。当您使用标准环境时,您可以通过将these built-in third party libraries添加到应用程序yaml或者您可以按照此link中的步骤使用任何其他第三方库。在

这里的问题是,正如previous link I shared中所述:

You can use third-party libraries that are pure Python code with no C extensions,

pandas库的部分代码是用C(https://pandas.pydata.org/#library-highlightsHow to solve import error for pandas?)编写的 ),因此,为了使用Pandas,您需要使用App Engine Flexible Environment。在

为了使部署运行,您需要对文件进行一些修改。按照以下链接调整文件以适应灵活的环境:

啊!pip安装sodapy使用魔法命令!,which is something exclusive from some Notebooks environments。不能将该行添加到主.py文件。此操作(pip install sodapy)将在应用程序部署期间运行,因为您正在将sodapy==1.4.6添加到要求.txt文件。在

而不是添加上下文.默认值().project_id,您应该能够将您的project_id添加为str。在App Engine内部运行时,Flex授权不应成为问题。如果要在本地运行它,请记住使用具有正确权限的a service account。在

相关问题 更多 >

    热门问题