使用Cloud Function生成的新表创建/替换大查询表

时间:2019-03-14 12:39:15

标签: python google-cloud-platform google-bigquery google-cloud-functions

我正在尝试从BQ获取一张表,将其转换为df,对其进行修改,然后将其上传回BQ,以替换以前的版本。

此功能旨在作为Google Cloud Function运行。 GCS使用的服务帐户具有访问BQ所需的所有授权。

到目前为止,我一直在使用它,但是它不起作用:

从BQ提取DFP广告管理系统表

import google.cloud.bigquery as bigquery
import pandas_gbq as pd

def execute_function(request):

    #Defines Client access & Project to be accessed
    client = bigquery.Client()

    SQL = """
    SELECT LINE_ITEM_NAME, TOTAL_LINE_ITEM_LEVEL_CLICKS
    FROM `DATASET.table_imported` 
    WHERE DATE = DATE_ADD(CURRENT_DATE(), INTERVAL -1 DAY)
    AND LINE_ITEM_TYPE = 'PRICE_PRIORITY'
    ORDER BY TOTAL_LINE_ITEM_LEVEL_CLICKS  DESC
    LIMIT 1000"""

    query_job = client.query(SQL)

    dfp = query_job.result()

    ### CREATE WRONG NAMING TABLE SORTED BY CLICKS DESCENDING

    dfp = dfp.to_dataframe()

    dfp.columns = ['li_name', 'clicks_number']

    dfp.set_index('li_name', drop = False, inplace = True)

    wrong_naming = pd.DataFrame()
    li_names = []
    clicks_numbers = []

    #Adds line items that have names with number of fields not equal to 7 only if not already present
    for row in range(len(dfp)):
        if len(dfp.iloc[row, 0].split('_')) != 7 and dfp.iloc[row, 0] not in li_names:
            li_names.append(dfp.iloc[row, 0])
            clicks_numbers.append(dfp.iloc[row, 1])

    #Adds created lists to wrong_naming dataframe
    wrong_naming['li_name'] = li_names
    wrong_naming['clicks_number'] = clicks_numbers

    ### EXPORT / SAVE TABLE TO GBQ

    wrong_naming.to_gbq('DATA_LAKE_MODELING_US.wrong_naming', 
                        if_exists='replace')

但是,尽管当我在本地系统上运行代码时它可以工作,但在云上却没有。

这是我收到的错误消息:“错误:无法处理请求”

有什么办法可以解决这个问题吗?

0 个答案:

没有答案