如何根据自定义参数在 table 名称中使用 python 类字符串格式查询 google 数据工作室中的 bigquery tables?
How to query bigquery tables in google data studio with python-like string formatting in table names based on custom parameters?
所以我每年每个产品都有几个 tables,tables 是这样的:
2020product5, 2019product5, 2018product6
等等。我在 google data studio 中添加了两个自定义参数,命名为 year 和 product_id,但不能在 table 名称中使用它们。我以前使用过参数化查询,但在 where product_id = @product_id
之类的条件下,但此设置仅在所有数据都在相同 table 时才有效,这不是我当前的情况。在 python 中,我使用像 f"{year}product{product_id}"
这样的字符串格式化程序,但在这种情况下显然不起作用......
使用 Bigquery 默认 CONCAT & FORMAT 函数无济于事,因为两者都会抛出以下验证错误:Table-valued function not found: CONCAT at [1:15]
那么我如何在 google 数据工作室中使用 table 名称中的 python 类字符串格式查询 bigquery tables基于自定义参数?
经过大量研究,我(有点)整理出来了。事实证明,这是一个数据库级的功能,可以查询 schema-level 个实体,例如table 动态命名。 BigQuery 不支持 table 名称内的格式设置,如所讨论的 tables(例如 2020product5
、2019product5
、2018product6
)无法直接查询。但是,它确实有一个 TABLE_SUFFIX 功能,允许您动态访问 table,因为 table 名称的更改位于 table 的末尾。 (此功能还允许 dateweise 分区和许多使用 BQ 作为数据接收器的工具,利用它。因此,如果您使用 BQ 作为数据接收器,那么您的原始数据源很可能已经这样做了)。因此,table 名称(product52020
、product52019
、product62018
)也可以动态访问,当然也可以使用以下方法从数据工作室访问:
SELECT * FROM `project_salsa_101.dashboards.product*` WHERE _table_Suffix = CONCAT(@product_id,@year)
P.S.: 使用 python 创建一个循环遍历产品和 tables 并复制并创建新脚本的脏脚本,如下所示:(添加带格式化的脚本字符串,所以它可能对任何有这种情况的人都有用,只是名义上的努力)
import itertools
credentials = service_account.Credentials.from_service_account_file(
'project_salsa_101-bq-admin.json')
project_id = 'project_salsa_101'
schema = 'dashboards'
client = bigquery.Client(credentials= credentials,project=project_id)
for product_id, year in in itertools.product(product_ids, years):
df = client.query(f"""
SELECT * FROM `{project_id}.{schema}.{year}product{product_id}`
""").result().to_dataframe()
df.to_gbq(project_id = project_id,
destination_table = f'{schema}.product{product_id}{year}',
credentials = service_account.Credentials.from_service_account_file(
'credentials.json'),
if_exists = 'replace')
client.query(f"""
DROP TABLE `{project_id}.{schema}.{year}product{product_id}`""").result()
所以我每年每个产品都有几个 tables,tables 是这样的:
2020product5, 2019product5, 2018product6
等等。我在 google data studio 中添加了两个自定义参数,命名为 year 和 product_id,但不能在 table 名称中使用它们。我以前使用过参数化查询,但在 where product_id = @product_id
之类的条件下,但此设置仅在所有数据都在相同 table 时才有效,这不是我当前的情况。在 python 中,我使用像 f"{year}product{product_id}"
这样的字符串格式化程序,但在这种情况下显然不起作用......
使用 Bigquery 默认 CONCAT & FORMAT 函数无济于事,因为两者都会抛出以下验证错误:Table-valued function not found: CONCAT at [1:15]
那么我如何在 google 数据工作室中使用 table 名称中的 python 类字符串格式查询 bigquery tables基于自定义参数?
经过大量研究,我(有点)整理出来了。事实证明,这是一个数据库级的功能,可以查询 schema-level 个实体,例如table 动态命名。 BigQuery 不支持 table 名称内的格式设置,如所讨论的 tables(例如 2020product5
、2019product5
、2018product6
)无法直接查询。但是,它确实有一个 TABLE_SUFFIX 功能,允许您动态访问 table,因为 table 名称的更改位于 table 的末尾。 (此功能还允许 dateweise 分区和许多使用 BQ 作为数据接收器的工具,利用它。因此,如果您使用 BQ 作为数据接收器,那么您的原始数据源很可能已经这样做了)。因此,table 名称(product52020
、product52019
、product62018
)也可以动态访问,当然也可以使用以下方法从数据工作室访问:
SELECT * FROM `project_salsa_101.dashboards.product*` WHERE _table_Suffix = CONCAT(@product_id,@year)
P.S.: 使用 python 创建一个循环遍历产品和 tables 并复制并创建新脚本的脏脚本,如下所示:(添加带格式化的脚本字符串,所以它可能对任何有这种情况的人都有用,只是名义上的努力)
import itertools
credentials = service_account.Credentials.from_service_account_file(
'project_salsa_101-bq-admin.json')
project_id = 'project_salsa_101'
schema = 'dashboards'
client = bigquery.Client(credentials= credentials,project=project_id)
for product_id, year in in itertools.product(product_ids, years):
df = client.query(f"""
SELECT * FROM `{project_id}.{schema}.{year}product{product_id}`
""").result().to_dataframe()
df.to_gbq(project_id = project_id,
destination_table = f'{schema}.product{product_id}{year}',
credentials = service_account.Credentials.from_service_account_file(
'credentials.json'),
if_exists = 'replace')
client.query(f"""
DROP TABLE `{project_id}.{schema}.{year}product{product_id}`""").result()