SQLALCHEMY LALECT for Bigquery
pybigquer的Python项目详细描述
bigquery的sqlalchemy方言和api客户端。
用法
Sqlachemy
fromsqlalchemyimport*fromsqlalchemy.engineimportcreate_enginefromsqlalchemy.schemaimport*engine=create_engine('bigquery://project')table=Table('dataset.table',MetaData(bind=engine),autoload=True)print(select([func.count('*')],from_obj=table).scalar())
API客户端
frompybigquery.apiimportApiClientapi_client=ApiClient()print(api_client.dry_run_query(query=sqlstr).total_bytes_processed)
项目
project在bigquery://project中用于实例化具有特定项目ID的bigquery客户端。若要从环境中推断项目,请使用bigquery://–而不使用project
认证
按照Google Cloud library guide进行身份验证。或者,您可以在create_engine():
中提供服务帐户json文件的路径。engine=create_engine('bigquery://',credentials_path='/path/to/keyfile.json')
位置
要指定数据集的位置,请将location传递到create_engine():
engine=create_engine('bigquery://project',location="asia-northeast1")
表名
要从非默认项目中查询表,请对表名使用以下格式:project.dataset.table,例如:
sample_table=Table('bigquery-public-data.samples.natality')
批量
默认情况下,arraysize设置为5000。arraysize用于设置获取结果的批大小。要更改它,请将arraysize传递到create_engine():
engine=create_engine('bigquery://project',arraysize=1000)
添加默认数据集
如果要让Client使用默认数据集,请将其指定为连接字符串的“数据库”部分。
engine=create_engine('bigquery://project/dataset')
使用默认数据集时,不要在表名中包含数据集名称,例如:
table=Table('table_name')
请注意,指定默认数据集不会在使用原始查询时限制对该特定数据集执行查询,例如:
# Set default dataset to dataset_aengine=create_engine('bigquery://project/dataset_a')# This will still execute and return rows from dataset_bengine.execute('SELECT * FROM dataset_b.table').fetchall()
连接字符串参数
在很多情况下,您不能直接调用create_engine,例如在使用Flask SQLAlchemy之类的工具时。对于这种情况,或者对于希望Client具有default_query_job_config的情况,可以在连接字符串的查询中传递许多参数。
此库使用credentials_path、location和arraysize参数,其余参数用于创建QueryJobConfig
注意,如果您想使用查询字符串,那么如果使用三个斜杠,那么'bigquery:///?a=b'将更可靠,但是'bigquery://?a=b'可能会被解释为具有?a=b的“数据库”,具体取决于用于分析连接字符串的系统。
以下是所有支持的参数的示例。任何不存在的都是针对遗留SQL(此库不支持该SQL),或者太复杂且未实现。
engine=create_engine('bigquery://some-project/some-dataset''?''credentials_path=/some/path/to.json''&''location=some-location''&''arraysize=1000''&''clustering_fields=a,b,c''&''create_disposition=CREATE_IF_NEEDED''&''destination=different-project.different-dataset.table''&''destination_encryption_configuration=some-configuration''&''dry_run=true''&''labels=a:b,c:d''&''maximum_bytes_billed=1000''&''priority=INTERACTIVE''&''schema_update_options=ALLOW_FIELD_ADDITION,ALLOW_FIELD_RELAXATION''&''use_query_cache=true''&''write_disposition=WRITE_APPEND')
创建表
向表中添加元数据:
table=Table('mytable',...,bigquery_description='my table description',bigquery_friendly_name='my table friendly name')
向列中添加元数据:
Column('mycolumn',doc='my column description')
要求
使用安装
- pip install pybigquery
测试
加载示例表:
./scripts/load_test_data.sh
这将创建一个数据集test_pybigquery,其中包含名为sample_one_row和sample的表。
设置环境并运行测试:
pyvenv .env source .env/bin/activate pip install -r dev_requirements.txt pytest