已弃用的BigQuery SQLAlchemy方言
项目描述
快速入门
为了使用这个库,您首先需要完成以下步骤:
安装
使用pip在virtualenv中安装此库。 virtualenv是一个用于创建隔离Python环境的工具。它解决的基本问题是依赖和版本,以及间接的权限。
使用virtualenv,您可以在不需要系统安装权限且不与已安装的系统依赖冲突的情况下安装此库。
支持的Python版本
Python >= 3.6
不支持的Python版本
Python <= 3.5。
Mac/Linux
pip install virtualenv
virtualenv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install pybigquery
Windows
pip install virtualenv
virtualenv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install pybigquery
用法
SQLAlchemy
from sqlalchemy import *
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import *
engine = create_engine('bigquery://project')
table = Table('dataset.table', MetaData(bind=engine), autoload=True)
print(select([func.count('*')], from_obj=table).scalar())
API客户端
from pybigquery.api import ApiClient
api_client = ApiClient()
print(api_client.dry_run_query(query=sqlstr).total_bytes_processed)
项目
project in bigquery://project用于使用特定项目ID实例化BigQuery客户端。要从环境中推断项目,请使用bigquery:// – 不包含project
身份验证
遵循Google Cloud库指南进行身份验证。或者,您可以在create_engine()中提供服务账户JSON文件的路径
engine = create_engine('bigquery://', credentials_path='/path/to/keyfile.json')
位置
要指定数据集的位置,请将location传递给create_engine()
engine = create_engine('bigquery://project', location="asia-northeast1")
表名
要从非默认项目或数据集中查询表,请使用以下格式作为SQLAlchemy模式名称:[project.]dataset,例如。
# If neither dataset nor project are the default
sample_table_1 = Table('natality', schema='bigquery-public-data.samples')
# If just dataset is not the default
sample_table_2 = Table('natality', schema='bigquery-public-data')
批量大小
默认情况下,arraysize设置为5000。 arraysize用于设置获取结果时的批量大小。要更改它,请将arraysize传递给create_engine()
engine = create_engine('bigquery://project', arraysize=1000)
数据集列表的页面大小
默认情况下,list_tables_page_size设置为1000。 list_tables_page_size用于设置dataset.list_tables操作的最大结果数。要更改它,请将list_tables_page_size传递给create_engine()
engine = create_engine('bigquery://project', list_tables_page_size=100)
添加默认数据集
如果您希望客户端使用默认数据集,请将其指定为连接字符串的“数据库”部分。
engine = create_engine('bigquery://project/dataset')
当使用默认数据集时,不要在表名中包含数据集名称,例如。
table = Table('table_name')
请注意,指定默认数据集不会限制使用原始查询执行查询时仅执行该特定数据集的查询,例如。
# Set default dataset to dataset_a
engine = create_engine('bigquery://project/dataset_a')
# This will still execute and return rows from dataset_b
engine.execute('SELECT * FROM dataset_b.table').fetchall()
连接字符串参数
有很多情况下您不能直接调用create_engine,例如在使用Flask SQLAlchemy等工具时。对于这种情况或您想要客户端有默认查询作业配置的情况,您可以在连接字符串的查询中传递许多参数。
credentials_path、credentials_info、location、arraysize和list_tables_page_size参数由该库使用,其余参数用于创建一个QueryJobConfig
请注意,如果您想使用查询字符串,使用三个斜杠会更可靠,因此'bigquery:///?a=b'将可靠地工作,但'bigquery://?a=b'可能被解释为有?a=b的“数据库”,这取决于用于解析连接字符串的系统。
以下是所有受支持的参数的示例。任何不存在的参数要么是针对旧版SQL(该库不支持),要么过于复杂而没有实现。
engine = create_engine(
'bigquery://some-project/some-dataset' '?'
'credentials_path=/some/path/to.json' '&'
'location=some-location' '&'
'arraysize=1000' '&'
'list_tables_page_size=100' '&'
'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')
项目详情
下载文件
下载您平台上的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。