自以为是的轻量级ETL管道框架
mara-google-sheet-downloader的Python项目详细描述
谷歌表格下载器
此软件包包含一个google sheet downloader,用于mara ETL框架:
- 将Google工作表下载到数据库表中
- 可以在下载期间验证和格式化单元格
在
安装
要直接使用库,请使用pip:
pip install mara-google-sheet-downloader
或者
^{pr2}$在
示例
这里有一个管道“gs_demo”,可以下载到一个表中。这假设您在 网址https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/edit。此工作表必须与 为其配置凭据的电子邮件地址(请参阅下文)。在
电子表格包含一个工作表“test”,其中包含以下列:
city | some_date | amount | already paid? | price | comments |
---|---|---|---|---|---|
Berlin | 01.01.2020 | 3 | ja | 2.3 | added by JK on 2020-03-12 |
请注意,它的开头不包含id列!在
frommara_pipelines.pipelinesimportPipeline,Taskfrommara_pipelines.commands.sqlimportExecuteSQLfrommara_google_sheet_downloader.mara_integrationimportDownloadGoogleSpreadsheetpipeline=Pipeline(id='gs_demo',description='A small pipeline that demonstrates the a google sheet download')pipeline.add(Task(id='download_',description='Download a google sheet',commands=[ExecuteSQL(sql_statement=f"""-- Creates the table where the google sheet data should end up inDROP TABLE IF EXISTS public.gs_test;CREATE TABLE public.gs_test (id BIGINT PRIMARY KEY,city TEXT,some_date DATE,amount INTEGER,is_paid BOOLEAN,price DOUBLE PRECISION,comments TEXT)""",echo_queries=False,),DownloadGoogleSpreadsheet(spreadsheet_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',# from the URLworksheet_name='test',# worksheet# Validators:# c: counter (additional column, will start at 1 and count each row),# s: string,# d(in_fmt=%d.%m.%Y): date in YYYY.mm.dd format,# i: integer,# b(true:ja,false=nein): boolean with ja/nein interpreted as True/False,# f: float,# s: string# any cell which does not confirm to this spec will fail the import!columns_definition='csd(in_fmt=%d.%m.%Y)ib(true:ja,false=nein)fs',target_table_name='public.gs_test',# table where the data should end uptarget_db_alias='dwh',# alias of the DB where the data should end upskip_rows=1),# how many rows at the top should be skipped]),)
配置
下载程序需要OAuth2凭据,可以使用服务帐户或用户帐户。在
- 有关服务帐户,请参阅https://gspread.readthedocs.io/en/latest/oauth2.html。所有必需的信息都在 已下载json文件。在
- 有关用户帐户凭据,请参阅https://developers.google.com/sheets/api/quickstart/python,步骤1。
要获取初始刷新令牌,可以使用
flask mara_google_sheet_downloader.generate-user-refresh-token /path/to/downloaded/credential.json
凭据将需要作用域'https://www.googleapis.com/auth/spreadsheets.readonly', 'https://www.googleapis.com/auth/drive.readonly'
。在
用户帐户的OAuth2凭据示例:
frommara_app.monkey_patchimportpatchimportmara_google_sheet_downloader.configpatch(mara_google_sheet_downloader.config.gs_user_account_client_id)(lambda:"....client_id...")patch(mara_google_sheet_downloader.config.gs_user_account_client_secret)(lambda:"...client_secret...")patch(mara_google_sheet_downloader.config.gs_user_account_refresh_token)(lambda:"...initial_refresh_token...")
设置对要下载的图纸的访问权限
下载程序应该访问的所有工作表必须与与这些表相关联的电子邮件地址共享 资格证书。此电子邮件地址是:
- 对于用户帐户凭据:创建凭据的用户的电子邮件地址。在
- 对于服务帐户:服务帐户本身的电子邮件地址(例如“@”。iamcea.com网站"). 例如,该电子邮件地址包含在您可以下载的json文件中。在
命令行接口
这个软件包包含一个小型的cli应用程序,可以下载google工作表并将其输出为csv。在
您可以单独使用它,请参见python -m mara_google_sheet_downloader --help
了解如何使用它。在
- 项目
标签: